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?
3
2
I have a shell script that takes two arguments:
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?
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:
Growl message as a result of running it on a file:
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.
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:
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
:
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 alwaysCommand+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