Automator Power: Variables and looping

The latest version of Automator (in OS X 10.5 Leopard) has several handy new features. Among the most useful: variables and looping. Unfortunately, unless you’re an experienced scripter, they’re also potentially the most confusing.

Until now, Automator workflows have been linear: each action passed information to the next in sequence. With variables, workflows can be much more dynamic: you can define the output of one action as a variable, and store it for later use by another action. Automator also includes dozens of built-in variables, such as dates, times, and system information. With looping, you can repeat actions or groups of actions a set number of times.

To illustrate how Automator’s variables and loops work, I’ve created two sample workflows. While I’d like to think these workflows are interesting enough in themselves, the main point here is to show how you can employ these two new features in workflows of your own.

Built-In Variables: Automator has its own set of predefined variables, including dates and times, for use in your workflows.

To build them for yourself, launch Automator (in /Applications), create a new custom workflow, then add the actions as indicated. Actions are identified by category, then name; click on the specified category on the left side of the Actions pane, find the action on the right, and drag it into the workflow pane. You can also download the actions.

Listen to the Daily Mac News

Automator’s built-in variables are probably the easiest to use. This workflow uses them (along with Leopard’s new RSS feed actions) to locate online news articles, convert their text to dated audio files, and add those files to a dated playlist in iTunes so you can listen to them every day.

1. Internet: Get Specified URLs By default, this action is configured with the URL Double-click on Apple in the Bookmark column, and change it to the name of the news source you’re using. Press tab and enter the source’s URL (be sure to include http://). In this example, I’ll use Macworld and, but you can of course use any news source you like.

2. Internet: Get Feeds From URLs This action gets any RSS feeds from the Web address or addresses you provided above.

3. Internet: Get Text From Articles In this step, Automator grabs the text of the site’s RSS feeds. If you want to focus on certain topics, you can insert an Internet: Filter Articles action right after this one.

4. Text: Text To Audio File This is where you use one of Automator’s new predefined variables. To differentiate between individual articles after they’ve been converted to audio and imported into iTunes, you can add the current date to the name of each audio file. To do so, click on Variables (a button above the Actions column). From the Date & Time category, drag the Today’s Date variable into the Save As field of the Text To Audio File action. After that variable, type Mac News. Then choose Downloads from the Where pop-up menu. When the workflow runs, Automator will insert the current date in front of each audio file’s name. When you’re done, click on the Actions button just above the Library list to go back to the Actions list.

5. Music: Import Audio Files In this action, you can specify an encoder if you wish. Select the Delete Source Files After Encoding option; otherwise, the audio files will remain in your Downloads folder after being imported into iTunes.

6. Music: Add Songs To Playlist One of the nice things about Automator’s new variables is that once you’ve defined one, you can use it repeatedly throughout a workflow. (A list of the variables in the workflow should appear after you insert one; if it doesn’t, select View: Variables.) That makes it easy, for example, to create a new playlist just for today’s news.

Once you drag this action to the workflow, select New Playlist from the menu to the left of the action. Drag the Today’s Date variable from the Variables list to the action’s text field. Type the text Mac News Headlines after the variable, and you’re done.

If you get an error when you try to add the Add Songs To Playlist action to your workflow, first make sure that iTunes is open and that the program has no open dialog boxes.

Send Slide-Show Greeting

This workflow uses variables that you define yourself, as well as loops. It adds a personal touch to e-mail by using your built-in iSight camera to string together a series of snapshots as a stop-motion slide show. It then attaches your video to a new Mail message.

1. Files & Folders: New Folder After dragging this action to your workflow, type the name Snapshots in the Name text field and leave the Where pop-up menu set to Desktop.

2. Utilities: Set Value Of Variable After adding this action, click on the Variables button above the Actions list. From the Locations category, drag New Path onto the action’s Variable pop-up menu. Again, a list of the variables used in your workflow should appear below your workflow. Double-click on the New Path variable that now appears in this list, and change its name to Snapshots.

3. Photos: Take Video Snapshot First click on the Actions button to reveal the actions again, and then drag this action into your workflow. Select the Take Picture Automatically option, and drag Snapshots from the variable list into the action’s Where pop-up menu.

4. Files & Folders: Rename Finder Items Whenever you insert this action, Automator asks you whether you’d like to insert a Copy Finder Items action first, to ensure that your original files aren’t renamed. This isn’t necessary, so make sure to click on Don’t Add. Choose Current from the Date/Time pop-up menu and Seconds From 12 M from the Format pop-up menu.

Looping Back: With the new looping action, you can tell Automator to repeat a sequence of actions as many times or for as long as you like.
5. Utilities: Loop Here’s where looping comes in. In this case, the Loop action will return Automator to the beginning of the workflow a specified number of times. Set the first pop-up menu to Loop Automatically. Set the next pop-up menu to Times and type 5 into the text field there. Finally, set the last pop-up menu to Use The Original Input.

6. Utilities: Get Value Of Variable After the loop finishes, this action will retrieve the name of the folder where you’re storing these snapshots and pass it to the next action for further processing. Choose the Snapshots folder from the Variable pop-up menu. To ensure that the output of the Loop action isn’t appended to this, control-click on the action’s title (Get Value Of Variable) and choose Ignore Input from the contextual menu.

7. Files & Folders: Get Folder Contents This action grabs a list of files from the Snapshots folder, and then passes that list to the next action.

8. Photos: New QuickTime Slideshow Type Slide-Show Greetings in the Save As text field and drag the Snapshots variable into the Where pop-up menu. Next, set the slide duration to 1 second per image. Leave everything else as is.

9. Mail: New Mail Message Enter a greeting in the Subject field, and whatever text you want in the Message field.

That’s it: when you save and then run the workflow, your iSight camera will take a series of photos, then create a new Mail message with the video attached.

More important, you should now have some inkling as to how powerful—and how simple—variables and looping can be. If you poke around some more in that Variables list and play around with looping, you’ll find plenty of ways to make new, more powerful Automator workflows.

[Ben Waldie is the author of Automator for Mac OS X 10.5 Leopard: Visual QuickStart Guide (Peachpit Press, 2007) and the president of Automated Workflows.]

Subscribe to the Create Newsletter