Mac Automation: Using variables in Automator
Last week I showed you how to use Automator to make a workflow that took photos from your Mac with an iSight and sent them to your Apple TV. That how-to featured one way of using a variable in Automator to let the user name the photo themselves. In this week's Mac Automation post, I want to go into a little more detail on what variables are and what they can do for your Automator workflows. Continue reading to learn more about using variables in Automator.
What's a variable?
Think of a variable as a place holder. These placeholders can be used wherever you want to insert some type of data that the user inputs beforehand. For instance, if you want to save an image with a certain filename then you could have the user type in some text that in turn sets a variable's value. That data can then be input into the Save As on a different action (i.e. the "Take Video Snapshot" action from the previously mentioned workflow).
Where can variables be used?
Variables can be used anywhere data can be input. These variables are commonly used in save, text, and number fields.
How do I create a variable?
To create a variable, just click on the variables icon at the bottom of the Automator window (or click View > Variables). The variables panel will then be displayed. Right-clicking anywhere in the panel and selecting "New Variable" will create a token-looking item in the variables list. Type in a unique name for the variable and click "Done." There is no need to specify the value of the variable since you will ask the user for the information. However, if you don't wish to ask the user for the variable's value you can specify it before clicking "Done."

How do I fill in the variable's value in the workflow?
At some point in the workflow, you'll need to set the variable's value. This can be done by using two Automator actions together. First, use the "Ask for Text" action to get some type of input from the user (example: name, text, number, etc.). After the "Ask for Text" action, you'll want to add the "Set Value of Variable" action to the workflow. Select your variable in the drop-down menu labeled "Variable." When the workflow runs, the user will be asked to supply information which will then be stored as that variable's value.
Now what I do with the inputted value?
You can do numerous things with the information depending what type of data is stored in the variable's value field. To use the stored value, you'll need to drag the variable's "token" from the variables field in the Variables panel to the desired input location.

In the example workflow above, you can see that we used the "Ask for Text" action to ask for user input that would set the variable's value. We then drag the variable's token from the Variables panel to the "Save as:" field in the "Take Video Snapshot" action. This token acts as a place holder for the text that was inputted by the user.
When the workflow is run, the token (or "placeholder") will be replaced with the value of the variable.
But how does this benefit me?
Variables can benefit you in many ways. For instance, if you have large workflows with multiple actions, then you can use a variable to fill in information in many different locations, but you only have to supply the information to Automator once. This can often save time wasted from inputting text.
If this is your first time using variables in Automator, don't be scared. I will be providing many walk through tutorials similar to the "Mac Automation: take and sync pictures to Apple TV" tutorial that I showed you.
Many more tutorials just like these can be found right here on TUAW. New Automator and AppleScript tutorials are posted weekly.
Share
Last week I showed you how to use Automator to make a workflow that took photos from your Mac with an iSight and sent them to your Apple...
Add a Comment
I've wanted to know how to do things like this for quite a while. Thanks a lot Cory!
June 12 2008 at 5:34 AM Report abuse Permalink rate up rate down ReplyIs this a version-dependent feature of Automator?
June 11 2008 at 1:55 PM Report abuse Permalink rate up rate down ReplyYeah, variables is only in Leopard.... new feature...
June 22 2008 at 10:43 AM Report abuse Permalink rate up rate down ReplyThis information couldn't be more prescient for me. I've been struggling with how to store multiple items from a numbers file and paste them somewhere else later. I can set the variables from the clipboard just fine. I'm having trouble with the paste part, in that I can't get Automator to push the variable to the clipboard. My sequence looks like this:
Get Value of Variable "Address"
Copy to Clipboard
Get Contents of Clipboard (also tried CMD+V)
The clipboard seems to contain the latest item copied in my Set Value of Variable sequence.
If anyone has any advice, it would be greatly appreciated.
strange, i already typed this entry once, never got a mail...ill repeat myself:
Automator seems to be very powerful, but it's not as easy to use as it seems.
I wanted to program a alarm clock once, that boots my mac and plays a song in itunes at a given time.
I never found a command to boot your mac, but in system prefs you can set a time for booting and one for shutting down, so there has to be a way to let your mac start itself even if it is completely powered down.
The next task was a time .... is there a possibility to use a given time for a command?
Since all the other alarm clock apps like aurora also don't offer the possibility to boot your mac, i guess it's impossible....but why?
The easy way to run a Automator action at a certain time is to use iCal.
Open iCal and create a new event for the time and date you want. Since it's an alarm set it to repeat.
Now click the alarm drop down, you can run a script or open a file, a Automator application perhaps.
The hard way is to do use the terminal, along with cron, but that doesn't gain you much besides a new geek merit badge.
Rather then cron, you should use launchd:
http://developer.apple.com/macosx/launchd.html
Hot Apps on TUAW
Deals of the Day
more deals- Altec Lansing Octiv Duo iDock for $48 + free shipping
- Used Apple iMac 17" Core Duo 1.83GHz for $430 + $28 s&h
- Lounge Deluxe Stand for iPhone / iPod touch for $28 + $8 s&h
- Brookstone Surround-Sound Earbuds for $14 + $7 s&h
- Refurbished Skullcandy Tokidoki Smokin' Buds Mic'd Headset for $5 + $2 s&h
- Stitchway Backup Battery for iPod / iPhone for $5 + free shipping
Software Updates
more updates- EFI Firmware Update brings Lion Internet Recovery to 2010-model Macs
- OS X Lion 10.7.3 released with Safari 5.1.3, Wi-Fi bug fix
- Aperture updated to 3.2.2, addresses Photo Stream issue
- Apple updates Keynote to address Lion issues
- Google Search app gets new look on iPad
- Apple releases Apple TV Software Update 4.4.3



7 Comments