I've got a simple shell script that I would like to incorporate into an automator workflow. I just need to double click the application , pick a finder folder and run the command. At the moment it is running in my default user directory. I've found how to do this as a service, but I'd like it to work as an application. How can I get to run in the chosen folder? Optionally a drag and drop onto the application would be nice as well.
2 Answers
You can simply pass the selected folder to the shell script by selecting "as argument" from the "pass input:" dropdown-menu and then adding "$@"
at the point in your script where you want the folder path to be used.
To use drag&drop, just remove the "ask for finder items" action and let the "run shell script" receive the input from "application can receive files and folders as input".
-
1I've seen the "$@" command but wasn't sure how to use it. I simply put "cd $@" at the beginning of my script and it solved the problem. Thanks for your help. Commented Mar 1, 2011 at 21:27
-
1Actually, I'd recommend using
cd "$1"
instead.$@
expands to the list of all arguments passed to the script, and if there's more than onecd
will just gripe that it can't go more than one place;$1
expands to the first argument, so if you manage to pass it more than one it'll just go with the first. Also, make sure you use double-quotes around it, to keep the shell from getting confused by funny characters (like spaces) in the folder path. Commented May 7, 2011 at 15:42 -
1@Gordon In this case, with the "Allow Multiple Selection" checkbox unchecked, there will actually never be more than one argument passed to the shell script. Putting $@ in double quotes is indeed necessary (as I did in my original answer).– AsmusCommented May 10, 2011 at 10:44
The way I do this is define the path to a variable, then open up the workflow in a text editor, search for the variable, copy the string, then use $(string) anywhere in the workflow whatsoever (including shell scripts).