Discovering Automator by Hanaan Rosenthal • Automator, A Mac OS X Tiger automation app for the rest of us
 
Chapter 3: Creating and Running Workflows
Now that we had a chance to get our feet wet a bit with Automator, we know that workflows are Automator’s documents. We also saw that each new workflow window contains a library listing all the applications, and a list of all of the available actions.
In this chapter you will create a more sophisticated workflow, introducing you to more features related to creating, running, and saving workflows.
The workflow you will create will be using iCal, so if you are not using iCal, you may want to open it and add a couple of events. You do that by double-clicking on any date and enter some dummy information.
The purpose of the workflow will be sending you an email containing a summary of next week’s events. For that, you will need to get events from iCal from the calendars you want included in the report, summarize them into a text file, and attach this text file to a message to yourself.
Creating the workflow
In the book’s spirit of doing, lets dig right in and start the workflow. While creating it, I will introduce you to some cool features.
1.    Start by creating and saving your workflow.
To create the workflow, launch Automator if it is not running yet. If it is, make sure that you have a clean workflow window. You can create a new window by choosing New from the File menu (Command-N).
To save the workflow, choose Save As from the File menu and save your workflow under the name “Next week’s events,” and put it on the desktop.
åNOTE: You should save the workflow right away because it gives the workflow window a title. This can help you remember which workflow does what when you work with multiple workflows later on.
2.    Next, add the “Get specified iCal items” action.
In order to add the action to your workflow you first have to locate it.
Click the Library column at the right side of your workflow window and then click the iCal item in the Library list.  Add the “Get specified iCal items” action to your workflow by double-clicking it.
3.    Configure your action.
Now you will add to the list in the action the calendars you want the action to summarize. Clicking the Plus sign button will bring up a list of your available iCal calendars. Choose the calendars whose events you want summarized. Pick any calendar for this exercise.
The calendars will now appear in the calendar list in the action’s pane.
The Action Pane
Since most of the “action” in Automator happens in the workflow area on the left of the workflow window, Automator gives you plenty of ways to customize the way the action’s pane looks. You can collapse, expand, disable, enable, and even rename an action pane in the workflow.
Most of these features are available through the action menu. The action menu is opened when you click and hold the mouse over the action’s number at the top left corner of the action’s pane, as seen in figure 1.
 
Figure 1: The action pane’s menu.
Lets try a couple of things:
1.    Rename the action.
To rename the action, choose Rename from the action’s options menu. After the name of the action becomes highlighted, type in the new name and press the Enter key. Figure 2 shows the name before a new name is typed in.
 
Figure 2: The action’s name is highlighted waiting to be altered.
Notice that the action’s name was not replaced by the new name you typed, but rather, the new name was tacked on to the original name, and was placed in parentheses. Figure 3 shows the title bar of the action’s pane after the name is changed to “Get calendars for report.”
 
Figure 3: The action’s name was changed to “Get calendars for report.” The new name is placed in parentheses after the original name.
Changing the name of the action will help you keep track of the specific role each action plays in your workflow.
To return the action’s name to the original name, choose ‘rename’ again from the action’s menu and delete the text in parentheses.
2.    Disabling and enabling actions in your workflow.
Disabling and enabling actions is important for a few reasons. For one, it allows you to test the workflow without  certain actions. Maybe you added an action, but you are not sure if maybe another action should have been used. Instead of removing the action altogether and then having to add it again, you can temporarily disable it.
You may also create workflows that include optional actions. You can disable and enable the actions before running the action to either include or exclude them from running this particular time.
To disable an action, choose disable from the Action’s options menu (click the action’s number). Figure 4 shows the action after being disabled.
 
Figure 4: The action after being disabled.
Notice that disabled actions lose their numbers; instead they get a dash. Comment from my OCD side: Could we please have this dash centered in the circle?!
To enable the action again, choose Enable from the action’s options menu.
3.    Collapsing actions in the workflow.
Perhaps one of the best things you can do after adding a few actions to the workflow is to collapse the ones that you’ve finished configuring. Collapsing the action pane of actions in your workflow will reduce the clutter and speed up your work.
To collapse an action, click the small disclosure triangle to the right of the action’s number at the left corner, as seen in figure 5.
 
Figure 5: The disclosure triangle allows you to collapse and expand the action’s pane.
Figure 6 shows the action’s pane after it has been collapsed.
 
Figure 6: The action after being collapsed.
Another way to use the action’s menu is to control-click anywhere in the action’s title bar.
Before moving on with the workflow, make sure that the action is enabled and collapsed. You can leave it with the new name you gave it if you choose.
Moving on with the workflow…
Next, I will add the “Event Summary” action to the workflow. To do that, make sure that iCal is still clicked in the Library column on the right, and that the search field is empty.
If there’s text in the search field at the top-left corner, it may limit the number of actions you see in a particular list. To clear the search field, click the X button to the right of the text. If there’s no X button, then the field is empty.
 
Locate the Event Summary action, but do not double-click it. Instead, drag it to the workflow area on the right.
Notice that you can drag the action above or below the existing action. As you drag it, the action that is already in the workflow shifts up or down, indicating the position it will take relative to the new action.
Let the action go to drop it below the existing action. After the action was added, change the “For:” popup menu in the action’s interface to “Next week.” This will tell the action to summarize all of next week’s events.
Figure 7 shows the workflow window the way it should be organized at this point.
 
Figure 7: The workflow window at this point.
Lets look at other ways to move actions in the workflow.
Open the action popup menu by clicking the number 2 at the top-left corner of the Event Summary action. Figure 8 shows the popup menu you should be getting.
 
F
igure 8: The action’s popup menu allows you to reorder the actions in the workflow.
Choose the first menu item titled “Move before ‘Get Specified iCal Items.”’ The action moves to the start of the workflow before the “Get Specified iCal Items” action.
Now click and hold the action popup menu again on the same action, but this time it should have the number 1 at the top-left corner. The menu item should be a bit different as well: It should be now titled “Move” after “Get Specified iCal Items.”
Select that menu item to move the action back to the end of the workflow.
The power of the Action Search Field
The purpose of the next action you will add to the workflow is to take the text summarized and passed on by the previous action, and save it to a text file.
Lets assume for a moment that we don’t know the name of this action, and that we’re not sure what application this action is classified under. We know however, that it has to do with a text file.
To locate this action, you will first select the Applications item at the top of the Library column at the left of the workflow window. This will force the Action column to its right to display all of the available actions.
To locate the action, make sure that the Action Search field is clear, and then type in it, one key at a time, the following text: “t-e-x-t – f-i-l-e” (without the dashes, of course).
As you type each character, look at the Action column and see that the selection becomes narrower with each character you type. Figure 9 shows the Action column after you finish typing.
 
Figure 9: The Action Search field and the Action column.
To finish the job, type the word “New” after the “text file” text already typed in the search field. This will leave only the action we want in the list.
Double-click the action in the “New Text File” Action in the Action column to add it to the end of the workflow.
After the action is added, type “Temp.txt” into the Save As field. Later on you will create an action that will delete this temp file.
To clean up, click the X button in the Action Search field. This will force all the actions to be displayed in the Action column.
Figure 10 shows the workflow as it should look at this point.
 
Figure 10: The workflow as it looks at this point.
More on running workflows
At this point in the workflow I will stop for a minute to look at different aspects of running workflows.
So far I explained that workflows are made out of different actions. Each action can be customized with specific settings. I also explained how each action that is executed can pass on its result to the following action in the workflow, and the following action can use that result while it runs. For example, one action can duplicate files and pass a reference to those files to the next action, which in turn, can change the names of the files passed to it.
This broad explanation of what happens when a workflow executes is true, but there are several ways to influence what exactly happens to actions when a workflow runs, regardless of what these actions were meant to do.
Run options
Run options allow you to determine if the action runs with or without user intervention.
To better understand what this means, imagine a workflow that takes a certain playlist from iTunes and burns it to a CD. This workflow can run the same exact way every time, however, what happens if, as the workflow’s designer, you want the workflow to process a different playlist every time the workflow runs?
Well, the workflow will most likely have an action that specifies the playlist the workflow is going to use. All you will do then is instruct Automator to pause when this action executes and allows the user to change the selected playlist. This will allow the user running the workflow to specify a different playlist every time.
To illustrate this feature we will use our workflow. To start, run the workflow in its current state. Three conditions will show that the workflow has completed successfully:
1.    The green checkboxes at the bottom-left corner of the action pane of each action in the workflow. Figure 11 shows the first action in the workflow after the workflow finished running.
 
Figure 11: The green checkmark indicates that the action in the workflow completed its task successfully.
2.    The Temp.txt file that was created on your desktop.
The difference between item 1 and item 2 is that while every workflow you run should have all actions marked with green checkmarks at the end, the text file Temp.txt is unique to this workflow.
3.    A chime will sound, telling you that the workflow has completed.
Show Action When Run
Notice that when the workflow ran, all actions completed in succession without stopping. Next, we will set up the workflow to allow the user to choose any calendars he or she wants to summarize while the workflow is running.
To do that, click on the disclosure triangle next to the word “Options” at the bottom left corner of the action pane, as seen in Figure 12.
 
Figure 12: Clicking the “Options” disclosure triangle reveals the action’s run options.
The Run Options for this action consist of a single check box: Show Action When Run.
The wording on this checkbox suggests that the Automator team mulled quite a bit exactly how to convey this idea in words. What checking this checkbox means is that when it will be this action’s turn to run, after the workflow is executed, a dialogue box will show the options in this action’s interface, allowing the user to make changes ‘on the fly.’
To test this idea, check the “Show Options When Run” checkbox, and run the workflow again.
This time, the workflow stops and presents you (the user, in this case) with the options from the first action. Figure 13 shows the dialogue box with this action’s run options.
 
Figure 13: The interface of the first action is shown in a dialogue box presented to the user running the workflow.
Look at figures 3-12 and 3-13 and see that they present the same options in a slightly different package.  Figure 12 shows the options before the workflow runs and figure 13 shows the same options while the workflow runs.
After the settings in figure 13 are finalized, the user can click “Continue” to proceed with the workflow, or “Cancel” to stop it.
Once complete, uncheck the “Show Action When Run” checkbox in preparation for your next step.
More options
So far I showed you how to have the action’s interface display while the workflow executes. Next I will show you how some actions allow you to pick and choose exactly which portions of their workflow you want the user to be able to manipulate during execution.
Start by expanding the “Options” disclosure triangle at the bottom left corner of the last action in your workflow, the New Text File action.
As you can see in figure 14, you have the ability to choose to show only one of the two interface elements from the workflow. The only thing is, that this action happens to be a horrible example! The text file that this action creates is a temporary file that a later action will have to delete. If we let the user change the name or location of this temporary file, the final step of the workflow will surely fail.
Due to this reason, we will practice the options selection on the workflow’s next action.
Before adding more actions, lets clean house a bit. Collapse all three actions by clicking the disclosure triangle at the top left corner. This will collapse both the action pane itself, and the Options area, if visible. After that, your workflow will look like the one in figure 14.
 
Figure 14: The workflow at this point.
Now you’re ready to add the next action. Under the Mail category in the Library column, locate and add the “New Mail Message” action to the end of the workflow. This can be done by double-clicking the action in the Action column.
After the action has been added, click the Options disclosure triangle to expand the options. Notice how each item in the action’s interface has a corresponding item in the Options box.
In this case, we want the user to be able to change the address to which the alert email will go to, and the subject of the message.
1.    Check the “Show Action When Run” checkbox.
This will tell Automator that you want to show options for this action during execution.
2.    Select the “Show Selected Items” radio button.
This will tell Automator to show only the selected options rather than all of them.
3.    Check the “Subject” and “To” checkboxes.
Checking these two will indicate to Automator exactly which items from the action’s interface you want shown.
Figure 15 shows the “New Mail Message” action ready to go with the selections already made.
 
Figure 15: The “New Mail Message” action in the interface. When run, the user will be able to change the ‘To’ address and the subject of the message.
Another thing you can see in figure 15 is that you can click items in the list below the “Show Selected Items” radio button, and the corresponding interface element will be highlighted with a rectangle. In this example, I clicked the Subject item.
After you made sure that the action in your workflow is configured the same as the one shown in figure 15, go ahead and run the workflow. Notice  that when the workflow stops for the New Mail Message action, the only items available for modification are the “To” and “Subject” fields, as requested.
In Figure 16 you can see the dialogue box displayed when the New Mail Message action runs. After the workflow is done executing, check to see if there’s a new message in Mail with the subject, address, and message you specified.
 
Figure 16: The dialogue box you will see when the New Mail Message action you configured runs.
As a final step you will go through the workflow and uncheck all “Show Action When Run” checkboxes. You should do this since this particular workflow doesn’t and will run from start to end without stopping.
Finally, collapse the New Mail Message action to make some room. Your workflow should now have four actions, all collapsed.
Input, Output, and ordering actions in the workflow
Lets take a look at the workflow and try to understand a few things about the ways actions are linked to each other.
When you examine the connection between the third and fourth actions you see that they’re pretty happy together: The “New Text File” action’s output is Files/Folders is the same as the input for the following action New Mail Message.” Automator tells us that the fourth action “New Mail Message” uses the files or folder passed from the previous action “New Text File.” This is not entirely true, in fact, since the “New Mail Message” action returns a reference to a mail message, the output of the “New Text File” action will be promptly ignored. Figure 17 shows the last two actions of the workflow, as it is so far, with the link between them.
 
Figure 17: The third and fourth actions in the workflow.
So, just how do we arrange the actions in our workflow?  For starters, you need to realize that while Automator is very helpful in showing you how one action’s output relates to the following action’s input, the system is not perfect. Here and there, as you can see in our example, you need to take care of yourself and figure out how to order the actions in the workflow so they work.
Back to our workflow…
The way we want our workflow to be arranged starts from the end. The result of the “New Text File” action has to be passed directly to the “Add Attachment” action which will follow it. This means that the “New Mail Message” has to move. Moving it to the beginning of the workflow will do the trick: The workflow will start out by creating the new mail message, and the final action will attach the text file to the front mail message.
1.    Move the New Mail Message action to the start
Click and hold the number at the top left of the “New Mail Message” action and choose “Move Before ‘Get Specified iCal Items,’” as shown in figure 18.
 
Figure 18: Move the “New Mail Message” action to the beginning. Here I control-clicked the title-bar to get the popup menu.
2.    Add the “Add Attachment to Front Message” action to the end.
Locate the “Add Attachment to Front Message” action under the Mail category in the Library, and add it to the end of the workflow.
Notice that the “Add Attachment to Front Message” links well with the “New Text File” action before it. The text file passed from that action will be attached to the front message.
3.    Add the “Send Outgoing Messages.”
This action, also located under the Mail category, will send all of the outgoing messages, unless the previous action passes it a mail message, as it is in our case. If a message is passed to it, then only this message will be sent.
After completing the three steps above, your workflow should look like the one shown in figure 19.
 
Figure 19: The workflow at this point.
House cleaning
Many workflows we create have the potential of leaving some mess behind. What we have to make sure is to add to the workflow the proper steps that will clean up any mess and return the host-Mac to its prior state.
In this case, for instance, we create a temporary text file called “Temp.txt” right on the desktop. The final step of our workflow will move this file to the trash.
Move to the trash? What happened to deleting files?
One thing you want to try and avoid is deleting files. Why delete it if you can simply move it to the trash? This way, the user always has a way to get the file back. This is the reason that the action we use to “delete” files is called “Move to Trash.”
So we decided that the “Move to Trash” action has to be added to the workflow. This action, however, will only move to the trash the files/folders that were passed to it from the previous action.
To specify a file in Automator we can simply drag that file to the workflow area. This is a cool trick you will use to ‘specify’ the “Temp.txt” file before we have Automator delete, oops… I mean, move it to the trash.
1.    Add the Get Specified Finder Items action.
Press F11 key to invoke Exposé and show the desktop. Click and hold the “Temp.txt” file from the desktop, which was created the last time the workflow ran. Now, while holding the mouse button, click command-tab until Automator is activated again, and drop the file at the free space at the end of the workflow.
This will do two steps at once: It will add the “Get Specified Finder Item” action, and the file specified in it will be our Temp.txt file.
2.    Add the Move to Trash action.
This really is the final action in the workflow. At this point, your workflow should look like the one in figure 20.
 
Figure 20: The action at its final stage.
Testing your workflow
All that is left now to do is run the workflow right from Automator to see if it all works. If you run into problems, try to follow the steps again and look at different screen shots to figure out what went wrong.
Saving and using the workflow
Now that the workflow is complete, we will save it. This is not a big deal since the workflow was already saved previously.
What you will do here that’s different is make the workflow into an iCal alarm that will be executed when an iCal event’s time arrives.
1.    Create the iCal repeating event.
To start we will create an event in iCal that will repeat every Friday.
Go to iCal and create a new calendar called “Automator.” You will use a special calendar for this event, even though it isn’t required. You can create a new calendar by choosing New Calendar from the File menu, and then naming it.
After adding the calendar, choose New Event from the File menu to create the event we will need. Name the event “Auto event reminder.” Make sure that the event belongs to the Automator calendar you created.
From “Repeat” choose “every week” to make the event repeat every Friday. The event’s settings should look like the settings shown in figure 21.
 
Figure 21: The calendar event settings in iCal.
2.    Save the workflow as an iCal event.
Next, go back to Automator and choose “Save As Plug-in” from the File menu.
Name the plug-in “Next Week’s Events,” and set the Plug-in for to iCal Alarm, as seen in figure 22.
 
Figure 22: Save the workflow as an iCal Alarm plug-in.
After you save the plug-in, go back to iCal and see that the alarm for the event is set to Run Script and that your workflow is selected to run.
Creating applications
Another cool way to save Automator workflows is to save them as applications. Saving a workflow as an application requires special considerations, such as making sure that the user is aware of exactly what the workflow is about to do.
Saving a workflow as an application does mean that this application can be distributed to any other computer. But, any computer running that workflow application has to have Automator and all the actions that the workflow application uses. Like any Automator workflow, the applications Automator creates do not contain the actions themselves, but rather pointers to them, which it uses to locate the actual action on the computer and execute it.
Get the user in the loop
Next you will convert the workflow you created earlier into an application-worthy workflow.
The first thing you will do is open the original workflow so that you can work on it.
Now, we will add a little dialogue box to the start of the workflow, explaining to the user what the application is about to do, and confirming that the user indeed wants to run this workflow.
To do that, add the “Ask for Confirmation” action to the start of the workflow. The “Ask for Confirmation” action is found under the Automator category.
Once you added it to the start of the workflow, customize it to look like the one in figure 23.
Tip: You can click on the Otto icon at the left of the dialogue box in the action pane to toggle between two possible icons.
 
Figure 23: The “Ask for Confirmation” action customized and ready to go.
Save the application
Now we’re ready to save the workflow as an application.
1.    Choose “Save As” from the File menu.
2.    Fill the Save As field and where popup menu as indicated in figure X.
3.    Choose application from the Format popup menu.
4.    Click Save.
 
Figure 24: The Save As dialog box set to saving an application.
To run the Automator workflow application, double-click it like you would any other application. The icon of Automator workflow applications has Otto on it, as shown in figure 25.
 
Figure 25: The Automator workflow application icon on the desktop.
The first thing the user sees right after launching the application is the dialogue box shown in figure 26. This allows the user to back out and cancel the operation.