Chapter 5: Special Automator Actions
While most actions you will work with in Automator perform a function in another application, such as moving files, controlling iTunes, or manipulating iPhoto, some actions are designed to make the workflow itself run smoother and be more user-friendly.
Other actions control Automator itself, allowing you to run workflows automatically, or view the result produced by other actions.
This chapter will take aim at some of these ‘utility’ actions.
The basic workflow
To start, I will create a basic workflow that will take a specified folder, back it up, archive it, and rename the archive with the date.
Once the body of the workflow is complete, which means that I will have a workflow that does what it supposed to, we will add some utility actions.
The utility actions will perform certain formalities that will make the workflow nicer to use. One action will explain to the user what’s about to happen—very important when writing workflows for others to use. Another action will pause the workflow while the user is performing a task required to continue with the workflow.
The initial workflow I’ll create will look like this:
1. A file specified for archiving.
2. Archive the files to be backed up as a ZIP file.
3. Rename the ZIP file with today’s date.
Create the workflow
1. Create a new workflow.
2. Save the workflow as “Back up and archive.”
3. Add a file or folder for the workflow to process.
Eventually, the workflow will ask the user to select the files/folders he wants to back up. This user-interaction action will be added later on.
For now, just drag a temporary file right onto the workflow area on the Automator window. This will automatically add the “Get Specified Finder Items” action to the workflow with that file specified. Initially, during the workflow development stage, the workflow will be used to back up this file.
For this book, I created a TextEdit file called “Work.rtf” which I will use for this project.
4. Archive the files the action will be backing up.
Add the “Create Archive” action to the end of the workflow. The files specified by the previous action will be passed to it, and therefore archived.
In the Save As field, type “BU.” In the Where popup, either specify another destination disk, or just leave it on the desktop. While the desktop is not a good place to archive and store backup data, for our purposes it will do fine.
Figure 1 shows the workflow up to this point.
Figure 1: The workflow up to this point.
5. Rename the archive.
The ZIP file created by the Create Archive action has to have the date added to its name. Add the Rename Finder Items action to the end of the workflow. A dialogue box will appear warning you of the consequences of renaming files. Click the “Don’t Add” button. This will add the Rename action but not the other action proposed by Automator.
The Rename Finder Items action shows a warning since it may take a while to rename 100-200 items that were mistakenly renamed by this action.
Change the settings of the Rename action to match the settings shown in figure 2.
Figure 2: The configured “Rename Finder Items” action.
The format I use here for a date in a file name has a few considerations built into it:
The date item order is year, month, and then day. This makes sure that the files sort properly in a Finder window shown in list view.
The leading zeros ensure that the date is always the same number of digits.
There are no separators between the date components to conserve space.
An underscore separates the date from the name to allow the eye to identify it quickly.
After some practice, the number 19940128 will be easily recognizable as the date: January 28th, 1994.
Next I will do two things: First I will run the workflow to see if it works. The file shown in figure 3 verifies the workflow’s success.
Figure 3: The archive file resulting from the workflow.
After I am sure that the workflow works OK, I will collapse all the actions in the workflow to make up some room. When this is complete, my workflow window will look like the one in figure 4.
Figure 4: The workflow to this point with the actions collapsed.
Dressing up your workflow
So far we created an ordinary workflow. Next, I will show you how to add some user interaction actions to it to make it more palatable and easy for another person to use.
Here are the actions that can be added to this workflow:
1. Confirmation.
The “Ask for Confirmation” action will be used to inform the user of what the workflow is about to do.
2. Asking the user to select the file to be archived.
This can be achieved with the “Wait for User Action” action. The action will ask the user to select the files that have to be archived. The selected Finder items will later be passed to the next action for archiving.
3. Pause the workflow.
OK, so the workflow doesn’t really need to pause here, but for the purpose of exercise, we will add it.
4. Let the user know that the archive is complete.
This will be done with the Run AppleScript action. You will use AppleScript to parse-out the result and tell the user what the name and size of the final archive is.
To make our workflow complete, some actions have to be added, and some have to be replaced. Lets get to it.
Note that all the actions that we’re about to add to the workflow are found under the Automator category. Therefore, before starting, clear the Action Search Field and click the Automator category in Library to expose the eight actions under that category.
Out of the box, Automator comes with eight actions in the Automator category. This number is certain to grow. These actions are: Ask for Confirmation; Wait for User Action; Pause; View Results; Run AppleScript; Run Shell Script; Run Workflow; Run Web Service.
1. Replace the “Get Specified Finder Items” action with the “Get Selected Finder Items” action.
Click the X button at the top right corner of the action pane. This X button in the white circle on the title bar will remove that action from the workflow.
Instead of the action you removed, add the “Get Selected Finder Items” action. Make sure to place the newly added action at the start of the workflow. If you don’t make the switch every time the workflow runs, the same file or folder will be archived. This is not at all a bad idea under normal circumstances, but for now we will take a different route.
2. Add the “Ask for Confirmation” action to the beginning.
Drag the “Ask for Confirmation” action to the workflow and make sure that it gets the first spot. Customize the action based on figure 5.
The “Ask for Confirmation” action allows the workflow designer to let the user decide if they want to proceed with the workflow or not. Usually the best place for this action is at the start of the workflow. You can give a short description of what the workflow is about to do. There’s a place for a title, some description text, and the ability to customize the text (not the function, though) of the OK and Cancel buttons. The right-side OK button will proceed with the workflow execution, while the left-side cancel button will stop the workflow.
You can also pick one of two icons: You can have either the Otto icon or the Otto-warning icon with the yellow triangle. You switch between icons by clicking the icon-button.
3. Add the “Wait for User Action” action.
This action should go as the second action in the workflow. Also customize this one based on figure X1.
The “Wait for User Action” action is used when your workflow calls for a condition to exist which the user has to make happen. For instance, if you want to perform an action on the selected iTunes song or playlist, you can use this action to pause the workflow and instruct the user to select a song or a playlist. In our case here, we ask the user to select the files or folders he wants to archive.
The action has a place for a message. You can also time-out the action after a given number of minutes. To set the timer, check the “Close window and stop workflow after…” checkbook, and enter the number of minutes in the following text field.
4. Add the “Pause” action.
As simple as it gets: If you want the workflow to pause for a specified number of seconds, add this action and enter the number of seconds you want the workflow to pause for. No message, dialogue box, or further customization.
Add the Pause action to the end of the workflow and set the pause to 3 seconds.
Figure 5 shows the workflow up to this point.
Figure 5: The workflow up to this point.
5. Add the “Run AppleScript” action.
The Run AppleScript action is simple enough to use, but anything you write in it has to be in the AppleScript language. Knowing AppleScript, therefore, is sort of a requirement.
There are, however, a few simple AppleScript things you can do to enhance your workflows.
For this workflow, add the Run AppleScript action to the end of the workflow. Figure 6 shows the default Run AppleScript action interface.
Figure 6: The default Run AppleScript action.
To use the Run AppleScript action you have to insert the AppleScript statements instead of the line that now says: “(* Your script goes here *).”
Two built-in variables are ready for you to use: 1. Parameters and 2. Input. For now, other than in the note below, we will mostly ignore the parameters variable.
About the parameters variable… I will only say that you can see the value of the parameters variable by changing the line before last from “return input” to “return parameters.” If you do this and then add the “View Results” action after that, the “View Results” action will show what the parameters are.
The value of the input variable is the result of the previous action; you can tell what it is by placing the “View Results” action right after the “Run AppleScript” action.
In our case, the result is an AppleScript list with a single item: a file reference to the archive file. In the script we will get the information about the file using the “info for” command. Then, we will extract the name of the file from that, and also use and manipulate the file size of the archive. Finally, we will take all the information we gathered and display it to the user using the “display alert” command.
The complete AppleScript in the “Run AppleScript” action can be seen in figure 7.
Figure 7: The finished Run AppleScript action.
As you can tell from figure 7, the Run AppleScript action allows you to not only run, but also test your scripts. The buttons above the actual script area are: Run, Stop, and Compile. You can also drag the dot at the center, right below the script area, to reveal the Result area. The script result can help you debug the AppleScript portion.
The AppleScript I wrote for this workflow will display the alert shown in figure 8.
Figure 8: The alert displayed by the Run AppleScript action.
If you plan on using AppleScript in your workflows, you should probably write these scripts using Script Editor first, and then copy and paste them into Automator for the final part.
One way to mimic the Automator environment in Script Editor is to get the input that will be passed to the Run AppleScript action.
Do this:
Place the “View Result” action right before the Run AppleScript action.
Run the workflow.
Copy the content of the View Results window and paste it in a new script in Script Editor.
Assign the pasted value to the input variable, as shown in figure 9.
Figure 9: Mimicking the Automator environment in Script Editor is useful for writing scripts for the ‘Run AppleScript’ action.
More to do with the “View Result” action
The fact is, that for an action that does absolutely nothing, the “View Result” action is pretty cool. I say it and then I take it back, since also the TV in your living room doesn’t do anything, and I bet you think that it is pretty cool.
I didn’t mention TV simply to poke fun at your addictions (although I do take joy at it), but rather because the View Results action is very much like a TV: It’s an objective by-stander that shows you what’s up at different points in time.
As an exercise, place one instance of the View Results action between each one of the other actions in this workflow you’ve just created, and then run the workflow.
Notice how the information in the View Results action pane changes to show the result of each action, and therefore, also the input of the following action. The View Results action is one of your best workflow-debugging tools. We will discuss this subject later on…
Reusing customized actions
While Automator gives you cool and quick ways to locate any available action, the actions it locates are always the dry version with the default settings. What if you find yourself making the same changes to actions? For instance, if you use the Ask for Confirmation action and always use the same text you type in it. Or, what if you use the “Rename Finder Items” action a lot, but find yourself making the same changes to the interface to get to the way you like it to work?
What you need is to create your own ‘Favorites’ set of actions. These actions will be the actions you often use, already customized the way you like them. Here’s how this can be done:
1. Create a new workflow.
2. Add some actions to this workflow. For now we’ll just use two actions as examples, but eventually you will use your own.
Add the “Ask for Text” action from the TextEdit category.
Add another action, this time the Rename Finder Items action. Customize it to rename with dates in the Year/Month/Day format with leading zeros.
3. Save the workflow.
Save the workflow with the name “Favorites” in the “Workflows” folder in your user’s Library folder. This will ensure that it’s available from the My Workflows folder in the Library column in Automator.
4. Close the workflow window.
Your ‘Favorites’ section is ready. To use it, first create a new workflow. Now, scroll down to the end of the Library column and expand the My Workflows folder if needed. Figure 10 shows the expanded My Workflows folder in the Library column to the left of the Automator window.
One of the items should be ‘favorites.’ Click it to reveal the customized actions under it.
Drag the actions to the new workflow and see that your changes are there.
Figure 10: The new Favorites workflow has your customized actions.
To add items to the Favorites workflow, double-click it in My Workflows, add actions to it, save and close it.
Automating Automator
OK, sit down, take a deep breath, and get ready for some heavy-duty lifting. In the following section we will look at the scary possibility of a workflow running another workflow. And as if this wasn’t enough, imagine the possibility of a workflow running… itself! This is the stuff which sci-fi movies are based on.
Running Automator workflows from an Automator workflow is done with the help of the “Run Workflow” action.
Using the “Run Workflow” action
To start, lets look at the Run Workflow action by itself and see how it works.
The “Run Workflow” action has a single popup menu which allows you to pick the workflow you want to run. This popup lists all the workflows from two sources: The Example Workflow folder in the Library column, and any workflows saved in the Workflows folder in your user’s Library folder.
If the workflow you want to run is not in the list, choose the last item in the popup menu: Other. Doing this will bring up a dialogue box allowing you to select any workflow file you want. Beware, however, since the dialogue box will allow you to pick any file whatsoever. You have to make sure that you pick an actual workflow file, or the workflow will fail.
The other element in the action’s interface is an “Open” button. Pressing this button will open the workflow you chose in the “Workflow” popup menu. The workflow will open in a new window. You can make changes to it if you want.
Library folders exist in different places on your Mac. One is the folder named System on your startup disk. It is a good idea to pretend it isn’t there, since changing it may mess up your Mac’s operations. The other Library folder, which you may use, is found directly in the startup disk. Items in this Library folder are available to all users of this Mac. The final Library folder, the one that contains the Workflows folder we can save workflows to, is found in your user’s folder.
Create a new workflow and add the “Run Workflow” action.
Figure 11 shows the interface of the action.
Putting the Run Workflow action to use
Next we will create a workflow, save it, and then run it using the Run Workflow action.
1. Close the previous workflow window we created without saving.
2. Create a new workflow.
3. Add the “Get Current Web Page” action.
This action is categorized under the Safari category in the Library section.
4. Add the “Download URL” action.
Leave the Where popup menu set to Desktop. This way we’ll be able to see the downloaded file after it is created.
5. Save this workflow.
Name the workflow “Download Current Web Page” and save it in the Workflows folder in your user’s Library folder.
The workflow to this point should look like the workflow shown in figure 12.
Figure 12: The workflow up to this point.
6. Test the workflow to make sure it works.
7. Close this workflow.
This is important since you want to make sure that your next workflow, which will run this workflow, will run a working workflow.
Uncheck the Close window… check box.
Take a break and say the following five times fast:
“Just how slow would the workflow flow if the workflow would flow slow?”
You’ve just successfully created and saved the “Download Current Web Page” workflow. Now, you will create a workflow that will run that workflow.
1. Create a new workflow.
2. Add the “Wait for User Action” action.
In the action’s text field type: Go to the site you want to download and then click “Continue.”
3. Add the “Run Workflow” action.
Both actions should be under the Automator category.
From the Workflow popup menu, choose the workflow you created earlier: “Download Current Web Page.”
The workflow to this point should look like the workflow shown in figure 13.
Figure 13: The workflow up to this point.
4. Run this new workflow.
See how it calls the “Download Current Web Page” workflow and runs it.
Running a workflow until you drop
Next, you will create what is known in the programming field as a repeat loop. You will make this workflow run again and again, until the user clicks “Stop.”
Figure 14 shows a flow chart of the workflow you’ll create.
Figure 14: The workflows you will create will call each other to form a loop.
1. Close all workflow windows, with or without saving them.
You will start the workflow from the beginning, so might as well not have any clutter.
2. Create a new workflow.
3. From the TextEdit category, add the “Ask for Text” action.
This is a unique action since it will allow us to ask the user for the URL he wants to download, and at the same time we can let him cancel-out of the process.
In the “Question” field, enter “Type in the URL you want to download.”
For the buttons, type “Cancel” and “Continue.”
If the user clicks the Stop button, the entire workflow will stop. This allows us to prompt the user again and again for a URL to download, until he presses the Stop button. As soon as the Stop button is pressed, the process is over.
The text the user types in this action will be passed as text to the following action. While the input of the next action is URL, Automator will convert the text into a URL automatically.
4. Add the “Download URL” action.
This action will download the URL passed as text from the previous action.
5. Save the workflow.
Name the workflow “URL to File” and save it in the User -> Library -> Workflows folder.
You have to save the workflow now because your next step will be adding the Run Workflow action and specifying this very workflow to run.
6. Add the “Run Workflow” action.
After adding the action, specify this workflow as the workflow to run. This workflow, named “URL to File” (or whatever name you gave it), should appear in the workflows popup menu. If it doesn’t, choose “Other…” and locate the workflow using the Open dialogue box that will appear.
The idea behind running a workflow at the end of itself is that it causes the workflow to loop. Imagine—as soon as the workflow gets to the end, the final action tells it to run again.
As the Wizard’s Apprentice knows, anytime you start a repeating process, you better have a way out! In our case, when the user clicks the Stop button in the “Ask for Text” action, the process will stop.
If you don’t build in a plan to get out, and the workflow continues on and on, you can always cancel out by clicking Command-period.
7. Save the workflow again.
If you have followed the steps correctly, your workflow will appear the same as the workflow shown in figure 15.
Figure 15: The completed “Looping” workflow.
Now, take a deep breath again, and run the workflow.
If all went well, the workflow should run again and again, until one of these two things happens: You pressed the Stop button; or, you entered a bad URL and the Download URL to File action errored-out the workflow.
Cool, ha?
In this chapter you took a good look at the more obscure, yet very useful actions Automator has to offer. Most of the actions we looked at don’t really do anything on their own, but they can spice-up your workflows, and make running them more intuitive and fun.
In the next chapter we’re going to dive into some actions and workflows that are geared towards system administration and related tasks. Read on, you can always sleep tomorrow!