How create an OS X service that passes a file's path to a shell script?

3

2

I have a shell script that takes two arguments:

  • full file path
  • file name

How can I use Automator to add a context menu entry to Finder to run the shell script with a chosen file path and file name as arguments?

Milen

Posted 2011-12-31T10:12:20.730

Reputation: 189

Answers

7

Select to create a Service in Automator that receives selected files and folders as input in Finder only. Add the Run Shell Script action and pass input as arguments.

The arguments you receive are the full Unix paths of the selected files and folders. Using growlnotify, part of Growl for demonstration purposes:

enter image description here

Growl message as a result of running it on a file:

enter image description here

The command appears in the context menu of a file or folder in Finder. If there are too many applicable Services, they are grouped into a submenu Services.

enter image description here


If your script requires both the full file path, and the file name, you can do something like the following, first extracting the file name from the full path:

for f in "$@"
do
    name="$( basename $f )"
    /usr/local/bin/growlnotify "$name" -m "$f"
done

You can see that the file name is used as title, while the path is used as message in Growl:

enter image description here


If you need to query for additional input, you can execute a short AppleScript to do that. The following is a complete shell script (like the growlnotify above) that queries for input, and renames the selected file to that new name. I did not include error handling and such, e.g. adding colons and slashes to the new file name will likely break the script.

    # repeat for every file in selection
for f in "$@"
do
    # capture input of a simple dialog in AppleScript
    OUT=$( osascript -e "tell application \"System Events\" to text returned of (display dialog \"New Name of $f:\" default answer \"\")" )
    # if the user canceled, skip to the next file
    [[ $? -eq 0 ]] || continue
    # old file name is the loop variable
    OLD="$f"
    # new file name is the same directory, plus the user's input
    NEW="$( dirname "$OLD" )/$OUT"
    # print a message announcing the rename
    /usr/local/bin/growlnotify "Renaming…" -m "$OLD to $NEW"
    # perform the actual rename
    mv "$OLD" "$NEW"
done

Screenshot of a sample rename action announced via growlnotify:

enter image description here

Daniel Beck

Posted 2011-12-31T10:12:20.730

Reputation: 98 421

It worked like magic. thanks!!! Also, I use growlnotifier for displaying results of the operation. One question, where are the services stored? It didn't prompt a location. It is not there in ~/Library – Milen – 2011-12-31T20:53:00.693

@Milen ~/Library/Services, and you can always Command+Click a document's name (in this case the service in Automator) to show the full path in a popup menu. – Daniel Beck – 2012-01-01T00:01:00.133