Mac Automation: Using variables in Automator

Cory Bohon
C. Bohon|06.11.08

Sponsored Links

Cory Bohon
June 11, 2008 11:00 AM
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.
All products recommended by Engadget are selected by our editorial team, independent of our parent company. Some of our stories include affiliate links. If you buy something through one of these links, we may earn an affiliate commission.
Popular on Engadget