Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer
    
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 Dec 31 '11 at 20:53
    
@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 Jan 1 '12 at 0:01

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.