Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.4: Pass input as var to Automator's Run Shell Script Apps
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.]
    •    
  • Currently 3.67 / 5
  You rated: 3 / 5 (6 votes cast)
 
[36,589 views]  

10.4: Pass input as var to Automator's Run Shell Script | 4 comments | Create New Account
Click here to return to the '10.4: Pass input as var to Automator's Run Shell Script' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Pass input as var to Automator's Run Shell Script
Authored by: a¿ex on Apr 09, '07 01:00:42PM
If you like to add own scripts to your contextual menu, you may want to look at OnMyCommand http://free.abracode.com/cmworkshop/on_my_command.html (just in case you don't know that already).

[ Reply to This | # ]
10.4: Pass input as var to Automator's Run Shell Script
Authored by: gshenaut on Apr 10, '07 11:15:57AM
It might be worth mentioning that $* is another way to refer to all of a shell script's command line arguments, and that $@ and $* differ in one very important way: if the arguments need to be interpreted as a list of strings rather than as one all-encompassing string "$@" puts quotes around each separate argument and "$*" does not. For example, if you have the line
for x in "$*" ; do print -- $x ; done
you'll get all command line args printed on a single output line, but if you have
for x in "$@" ; do print -- $x; done
you'll get a separate line for each individual command line argument. In most cases, it doesn't matter, but in some cases it make a rather large difference.

[ Reply to This | # ]
10.4: Pass input as var to Automator's Run Shell Script
Authored by: TimBonnici on May 04, '08 03:55:06AM

I tried the exact example given in the hint and couldn't get it to work... until I changed the shell used by Automator from its default tcsh to bash. I don't know if this is a pecularity of my setup (unchanged from default settings AFAIK) but I'm posting it just in case someone else has a similar problem.



[ Reply to This | # ]
10.4: Pass input as var to Automator's Run Shell Script
Authored by: jstal on Aug 24, '10 06:45:44PM

Sweet hint, thanks. Makes it easy to use awk in a workflow, or any other tool that's not in Automator's list of interpreters.

#!/bin/bash
cat "$@" | awk -F , '{print $5","$4","$11","$9$10}' > /tmp/mytempfile.csv

---
Don't anthropomorphize computers, they hate it!



[ Reply to This | # ]