Passing Multiple Automator Variables to a Shell Script

Automator is incredibly powerful, but at the same time the most useless and semi-functional piece software I've ever encountered. I recently built my first Automator Workflow to watermark a PDF. I wanted to load selected PDFs from the Finder, and for each PDF create a file in the same directory with a -watermark suffix appended. file1.pdf and file2.pdf would yield file1-watermark.pdf and file2-watermark.pdf.

It took me much too long to figure out that it wasn't easy (or maybe even possible) with Automator, even though it's simple to do from the command line.

Each Automator action returns a result. That result is passed to the next item in the chain, assuming the item is accepting input. The key is to chain a series of Get Value of Variable calls together, which are passed to a shell script as $1-$n and the special $@ variable. I've written a short primer on using Arrays in Bash that may be helpful.

A crappy picture is worth at least 68 words, so here is what a sample workflow looks like:

You can download the sample workflow here.

If you want to loop through selected Finder items one-at-a-time, the best way is to use Nyhthawk Productions's excellent Dispense Items Incrementally action.

 

Tags: , , ,

4 Responses to “Passing Multiple Automator Variables to a Shell Script”

  1. Eric Says:

    Hi Corey–

    A very helpful tutorial. I'm trying to put an automator flow together and this was just what I was looking for. Just thought I'd mention that the the sample download doesn't have the correct permissions set. I get an access denied error. Thanks!

  2. Tamer Says:

    This is indeed helpful, but I am trying to use two file names in the same line of the script:

    pdfdiff.py -d opendiff A.pdf B.pdf

    It is not clear how to do that. Should I be doing:

    pdfdiff.py -d opendiff "$1" "$2"?

    Also, the sample download does not work for me either as the permissions are not set.

    Thanks much.

  3. Fabiosan Says:

    Very helpful. I am now able to set the input to the path of a folder and then its file list.

  4. dannystaple Says:

    @tamer Yes – $1 and $2 will represent the first two variables presented.
    Quoting them ensures that they are kept each as a single argument even if they contain spaces.
    Anyway – this was quite helpful. It is a bit of a shame that automator variables are not simply exposed as environment variables for the scripts (rather like the parameters in jenkins) – that would be kind of awesome.

Leave a Reply


© 2007-2012, Corey Gilmore | Posts RSS Feed | Comments RSS Feed | Contact

 

The views expressed on these pages are mine alone and not those of any past or present employer. All information presented on this site was obtained lawfully and not through disclosure under the terms of an NDA.