Automator is an incredibly handy automation assistant for OS X. Instead of writing lines of code as in AppleScript, Automator allows you to string together preset actions through an easy to use drag-and-drop interface. The result is no-brainer automation on the fly.
This tutorial will cover a number of basic Automator features including how to work with application-specific actions, creating variables, embedding AppleScripts, and recording custom actions. Let’s get going!
Open Automator and create a new workflow. You should see a wizard (a helpful menu, not a guy with a pointy hat) like the one below.
When you select what kind of script you wish to build, Automator will start you off with an action you’re likely to use. However, we’ll be starting from scratch so just choose the “Custom” option.
When you start a new custom workflow, you’ll be brought to the main window (pictured above). The Automator interface has been streamlined so that even the most inexperienced users can create automated tasks with ease. The main window contains three columns.
The first of these columns is the action library. Actions are neatly categorized by function. The second column contains the actual actions. Without a category selected, this column will contain every action in your library. These results can be filtered by searching or selecting a category. For instance, if you wish to see only “Mail” related actions, simply click on the Mail category. When you find an action that sounds like it might be what you want, click on it and check out the action information in the bottom left of the main window.
This window shows you a brief description of the action along with what the action will accept as input and output as a result. Don’t worry if this doesn’t make much sense yet, it will be clearer when we start building our workflow. If the description sounds like what you want, click and drag the action into the area that says “Drag action or files here.” Each action, when placed in the workflow, has its own set of options and functions that you can customize to your own purposes.
Notice that if you drag multiple actions into the workflow they link together and are rearrangeable. Certain actions require an input of some kind, which can be obtained from a previous action. The link represents the output of one action becoming the input of the next action. To illustrate how all this works, we’ll create a workflow that automates a simple task.
As a freelancer, I frequently receive emails that contain something for me to do. I usually create events in iCal for these items so that I can view all my deadlines for the week at once. To facilitate this process, we’ll create a workflow that grabs the selected text in Mail and puts it into a new event in iCal. Creating an algorithm is the process of breaking down what we want to do into manageable steps:
- Show Mail
- Copy the selected text to the clipboard
- Put the text from the clipboard into a variable
- Create a new event in iCal containing the text of the variable
As you can see, our algorithm is fairly short and straightforward (meaning this should be easy). Let’s get started!
Step 1: Embedding AppleScript
Before we build the workflow you should know that there are more efficient methods than the way we’ll be doing it. The reason for this is that I want to show off a few specific features that you might miss otherwise. With that in mind, let’s proceed to the first two steps of our algorithm.
We need to activate Mail and copy the selected text. Chances are, if you have text selected in Mail, it will be activated. However, when you run the action, Mail will no longer be the frontmost app. Consequently, we need the action to switch the focus back to Mail. We are going to accomplish this through a simple AppleScript.
This is the part where you say “Hey I thought this was an Automator tutorial! You can’t make me write code, I won’t do it!” The truth is, since Automator uses prebuilt actions, you’ll find that you sometimes run into limitations. If there isn’t an action for what you want to do, you’re stuck. Unless of course you know how to embed AppleScript into your actions. This function gives you the freedom to only code those portions of your automation workflows that aren’t supported by Automator. Nifty no?
We’ll keep the code here incredibly simple but you should check out my Ultimate Beginner’s Guide to AppleScript anyway so you can learn to write your own snippets of code to insert into your workflows. The first order of business then becomes locating the action that allows us to embed an AppleScript. Click on the “Utilities” category in the first column to find all the system-related utility actions. About halfway down the column of actions you’ll see one titled “Run AppleScript.” Click on this action and look at the info window.
The description for this action reads “This action runs the specified AppleScript.” Since this is exactly what we want, click and drag the action into the drop zone of the workflow. You should see the following window in your workflow:
For our purposes we don’t need to worry about anything but the section that says “Your script goes here”. Naturally, this is where we will place our script. Place the following text in that section and hit “Enter” to compile (the one on the far right of the keyboard near the number pad):
1 2 3
tell application "Mail" activate end tell
The action should now look like this:
This script is pretty straightforward and will simply make Mail the active application. If you hit the “Run” button in the top right of the Automator window, the script should run and activate Mail. If this does not happen, double check for errors.
Well there you have it, you are now equipped to work around many of the limitations of Automator by embedding an AppleScript that will do the heavy lifting. Now let’s proceed to the second step where we learn another useful trick.
Step 2: Recording Actions
Automator allows users to perform a given task or set of tasks while it records the process into an action. Upon hearing this, many people have the tendency to think that all their problems have been solved and that they’ll never use another feature. After all, if you can simply record what you want to do with little effort, why go through all that other stuff?
The truth is, recording actions in either Automator or Script Editor can give you mixed results. Like the magic wand tool in Photoshop, you’ll use it less and less as you become more proficient. For now I recommend experimenting with it on your own to get a feel for what does and doesn’t work.
Before we record this step, go to Mail and select some text from a message, then switch back to Automator. To begin recording an action, just hit the big record button at the top right of the main window. Now switch back to Mail and hit “Cmd-C” to copy the text. Finally, hit the stop button on the floating Automator window.
Now switch back to Automator and look at your workflow. It should look something like this:
Automator has conveniently broken up your actions into steps. For our purposes, we only need the “Press Cmd-C” step. Delete any others you might have by clicking on them and hitting “delete.” Notice there are a couple of options associated with this action (Timeout and Playback Speed). Feel free to play with these, but since we don’t really need them changed I’ll just leave them where they are.
Step two is now complete! The point of this step was to show you that you can record long, complicated actions and keep only those steps that are important for the workflow.
Step 3: Getting Text from the Clipboard & Working with Variables
In order to put our copied text into a variable, we need to tell Automator to “get” it. To do this we’ll click on the “Utilities” category in the first column. In the second column, about five from the top, you should see an action titled “Get Contents of Clipboard.” Click on the action and read the description in the bottom left of the window.
Notice that the input requirement and result both say “text.” This means that the action will take the text (only text will work) from the clipboard and will spit that text out to the next action. Throw this action into your workflow under the “Watch Me Do” action.
Now click the little results button on the clipboard action. The action will extend and show a window that says “Run workflow to see results.” Making sure you still have text selected in Mail, run the workflow by clicking the play button at the top left of the window. You should see something similar to the following:
The text that you selected in Mail should pop up in the results window. This lets us know that everything we’ve done up to this point is working perfectly. Now we need to take this text and put it into a variable.
If you’re unsure of what variables are and how they’re used, I refer you again to my Ultimate Beginner’s Guide to AppleScript which explains variables in detail. To put it briefly, a variable is a way to store information to be called upon later.
Above the first column in the Automator window you should see two buttons: Actions and Variables. By default, the Actions button is selected. Click on the Variables button. You should see a list of the types of variables Automator supports. Since we know that our last action output text as a result, scroll down the list and find the “New Text” variable. Drag the new text variable into the workflow.
You should see an action pop up in the workflow (Get Value of Variable) and a list at the bottom of the workflow which holds all the variables in a given workflow (we only have one). In the list at the bottom, double click the “New Text” variable and rename it something useful like “clipboardContents.” That’s all there is to creating variables in Automator, now we can call up this bit of text wherever we need it!
Step 4: Creating an iCal Event Using the Text our Variable
With the Actions button selected for the first column, click on the Calendar category in Automator. Near the bottom of the list there’s an action titled “New iCal events.” Drag this to the end of the workflow. Now drag the clipboardContents variable from the list at the bottom into the “Name” field of the action. This takes the text stored in the variable and puts it into the title of the event. This will enable us to create any number of unique events based on the text in the clipboard.
Notice there are several other options in this action, like the start and stop time of the event, the date, and the calendar you wish to put it in. As you can imagine, if we’re creating events from emails, this information will need to be different every time we use the workflow.
Fortunately, Automator allows for user input on a given action. Click on the “Options” button along the bottom of the action. Now check the box that says “Show this action when the workflow runs.” Now when we run the action we’ll be presented with a window that allows us to set the information for the iCal event we are creating.
Our workflow is finished! Before we test it, make sure you’ve placed all the actions in their proper sequence. Here’s an ordered list for reference:
- Run Applescript (with the text above)
- Watch Me Do (containing only the “Cmd-C” item)
- Get Contents of Clipboard
- Get Value of Variable
- New iCal Events (With “show this action” checked)
Saving & Running
When you are finished with the action, you can save it as a workflow or an application. I recommend saving it as an application and throwing it in your dock. Now when you select text in an email you can click the application and it will automatically create an iCal event using the selected text!
If you’d like to take a look at the finished workflow, we have made it available as a download: Download Automator Script – Have fun!
You should now be adequately equipped to start creating your own workflows. I hope you’ve enjoyed this post and learned as much as I have from writing it. Let us know what you’re going to automate! You can find a bunch of third party actions to help you out at Apple’s Downloads Page.
Also, this is the second article in my series on automation and I’m looking for ideas on what to write next. Tell me about the mundane tasks that you’re forced to complete over and over again. I’ll see if I can take your input and create another tutorial using either AppleScript or Automator.
Feel free to contact me with your AppleScript/Automator questions using the comments below or by sending me a tweet: @secondfret.