Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I have a php script that takes a filename as an argument. When I run it from a terminal window I type

./my_script.php file_to_process.txt

It works great!

What I would like to be able to do is to have an icon on my dock that when I drop a file onto it, it would excute the php script and supply the complete path to the dropped file as an argument to that script.

Is this possible? Can you please provide step by step instructions?

share|improve this question
add comment

3 Answers

up vote 2 down vote accepted

Yes, this should be possible using Automator. I haven't worked with php from the command line, so I'm unsure exactly how that works, but this is a general instruction.

  1. Open Automator (It's in the Applications folder).
  2. Choose to create an Application. Select the Application template
  3. Select Utilities under Library and choose Run Shell Script and drag it to the area on the right. Select Utilities, Run Shell Script
  4. In the text area for the run shell script action, remove 'cat' and write the full path to the script you wish to run.
  5. Select to Pass input to stdin.
  6. Save the Application somewhere and drag it to your dock.
  7. Try it out by dragging a file to it.
share|improve this answer
    
I followed your directions. When I run this inside of automator, and add a finder action to "Get Specified Finder Items" it works fine. I then remove the "Get Specified Finder Items" and save it, I then try dropping a file on it and it does not work - any ideas? –  Scott Jan 10 '12 at 18:49
    
it turns out, step 5 above, should be set to STDIN, and then STDIN should be read. Instead of being the file stream, STDIN contains the file name that was dropped. –  Scott Jan 11 '12 at 3:17
    
@Scott, OK, cool, I'll change the instructions then. Glad you got it to work! –  Lizzan Jan 11 '12 at 6:49
add comment

You need to use Automator.app

Create an Application and add a Utilities->Run Shell Script to include your my_script.php

share|improve this answer
add comment

You can also do this with AppleScript.

The script below open recursively all files/folder dropped on it. For each files, execute the PHP script defined by phpScriptPath passing the file path as argument.

To create the application :

1. Open AppleScript Editor

2. Paste following code:

property phpScriptPath : "/myPHP\ Scripts/my_script.php" -- Absolute path to your PHP script

on open droppedItems
    processItems(droppedItems)
end open

on processItems(theItems)
    repeat with theItem in theItems
        if isDirectory(theItem) then
            processItems(theItem)
        else
            do shell script phpScriptPath & " " & quoted form of (POSIX path of (theItem as alias))
        end if
    end repeat
end processItems

-- This function comes from here :
-- http://stackoverflow.com/questions/6872007/applescript-how-to-check-if-something-is-a-directory-or-a-file
on isDirectory(someItem) -- someItem is a file reference
    set filePosixPath to quoted form of (POSIX path of (someItem as alias))
    set fileType to (do shell script "file -b " & filePosixPath)
    if fileType ends with "directory" then return true
    return false
end isDirectory

3. Set absolute path of the PHPscript

Edit the first property line of the script

4. Export as application

File menu > Export :

  • Choose app name & destination
  • Select to export as Application
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.