10.4: Pass input as var to Automator's Run Shell Script

Apr 09, '07 07:30:00AM

Contributed by: ocdinsomniac

I just figured this out recently when trying to craft my own contextual menu for file creation via Automator. To pass output from a previous action or a right-click to a "Run Shell Script" action in Automator, select 'as arguments' from the 'Pass input:' field in the Run Shell Script Action. Automator will generate a simple for loop script, but you don't have to use this.

What's important here is that the variable assigned to the input from a previous action (or a right-click in the case of a Finder plug-in) is $@. For example, if you right-clicked a folder Some Folder, and selected an Automator workflow that ran a shell script, the folder you right-clicked (Some Folder) would be referred to as $@ in your shell script. If your shell script contained this:

touch "$@"/NewFile.txt
Then it would then create a file called NewFile.txt inside Some Folder. This could be immensely useful for creating a variety of simple contextual menus via Automator. I was pretty excited to discover it. I've posted more details and exmaples on my blog.

[robg adds: If you're new to shell scripting in Automator, the usage of the $@ parameter may not have been obvious. I know I found it a bit confusing at first.]

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20070331120036768