Chapter 24

Mac Automation

When you need a new feature for your Mac, you usually go out and find the software that will do the job. Indeed, most of this book is about finding the right application. But what about making your own applications or enhancing the features of your existing applications?

Normally, creating an app or adding features to an application requires that you write your own software. Programming is not easy; it takes years of training and experience to do well. For those of us without the time and patience to learn how to program, Apple has created several tools to help us automate our Macs. These automation tools let you create a type of mini-program that performs specialized tasks and let you connect existing applications to make them work in new and better ways. This chapter covers the two primary automation tools on the Mac: Automator and AppleScript.

Automator

877005-ma2401.tif Automator is installed on all Macs for one purpose: to simplify repetitive tasks. Consider it your own personal robot. (Indeed, the Automator icon is a robot named Otto.) The idea behind Automator is to allow anyone, without a lick of programming experience, to create his or her Automator actions. Creating Automator workflows requires no computer programming whatsoever. With its drag-and-drop functionality, Automator lets you quickly create actions and start saving time. So let’s create an Automator action.

Resizing images

Imagine you just returned from your company’s latest retreat and your boss walks into your office and hands you a disc full of pictures that she wants posted on the company Web site before the day is over. You open the disk on your Mac to discover there are 638 pictures and they are all 3,888 by 2,592 pixels — much too large for the Web. So you have one hour to resize 638 images. No problem! Automator to the rescue.

The secret is to build a workflow in Automator to resize those photos. First open Automator (found in the Applications folder). Upon launch, Automator gives you an option window to choose the type of workflow you are going to build (shown in Figure 24-1).

Figure 24-1

Types of Automator workflows

877005-fg2401.tif 

There are several types of Automator workflows:

For the Web-resizing example, choose Workflow.

The left pane in the main Automator window, shown in Figure 24-2, holds a list of Automator actions. Apple has assembled an exhaustive set of actions for a variety of contexts, such as managing contacts, changing file names, and using iTunes. In addition to the Automator actions supplied by Apple, third-party developers have created actions you can install in Automator. Microsoft Office, for example, automatically installs several Automator actions that let you automate actions in the Microsoft Office applications.

Figure 24-2

Automator’s main window

877005-fg2402.tif 

The right side of the screen is where you build your Automator routines. Using Automator reminds me of playing with Lego blocks. The left side of the Automator screen is like the bucket of blocks and the right side is like your building mat. Creating an Automator workflow is no more difficult than finding the correct blocks on the left side and then stacking them in the correct order on the right side.

Creating an action

So let’s get back to the images. Create a new Automator project by choosing File New or pressing Command+N. Then select Workflow from the settings sheet.

Before Automator can resize anything, it needs to first find the images. Automator actions that deal with files are found in the Files & Folders list in the left pane. Rather than digging through the lists of potential actions, you can search them out, if you know all or part of the action’s name: Click on the search window, indicated in Figure 24-2, and type Get Selected Finder Items. Automator will display the Get Selected Finder Items action for you. Click and drag the action onto the right side of the screen. You’ve just laid your first block.

Once you have the Get Selected Finder Items Action in your workflow, click Description so Automator opens a description of what the action does. In this case, it simply takes the selected Finder items and passes them to the next action.

So now that you have selected the items, what next? It’s time to resize. Type the word scale in the Automator search bar; the Scale Images action should appear in the left pane. Drag it onto your workflow. The screen should now look like that in Figure 24-3. The Scale Images action gives you the option to set the desired width of the images. Because these images will be published on the Web, set it to 400 pixels.

Figure 24-3

Scaling images with Automator

877005-fg2403.tif 

At this point, the Automator workflow has selected and resized the images. Just so there’s no confusion as to the fact that the images have been resized for the Web and are not the original versions, you should rename the resized images; here, add the words for Web at the end. To do so, type rename in the Automator search bar and drag the Rename Finder Items action to the workflow. This action gives you several options: You can add a date or time, change the case, make file names sequentially numbered, replace text, name a single item, or add text. In this case, let’s add text at the end of the resized image: Select Add Text and type the words for Web in the Add field. Finally, make certain the action knows to add the text after (as opposed to before) the file name by selecting After Name in the Automator action. The final Automator workflow should look like that in Figure 24-4.

Figure 24-4

The completed image-resizing Automator workflow

877005-fg2404.tif 

Now you’re finished. Open the folder containing all the image files. Select all the images with the mouse (or press Command+A) and then switch to Automator. Press the Run iconic button in the toolbar and Automator will resize your images, lickety-split. When you’re done, you’ll have a folder full of resized, 400-pixel-wide images ready for publication to the Web.

Now let’s build an Automator service.

Creating a service

A lot of Mac workers have more reading materials than they have time: You are flooded with documents and extended e-mails that require reading. Let’s use Automator to take written text and convert it into an audio file so you can listen to it on your iPod or iPhone during your commute.

This time, rather than make a workflow, you are going to create a service. Services let you to create a custom Automator script that resides in the contextual menu (activated when you Control+click or right-click an object). A service is always available, even when Automator is not running.

Create a new Automator project by choosing File New or pressing Command+N. This time, instead of selecting Workflow in the settings sheet, select Service. The Automator window opens and should look like that in Figure 24-5.

Figure 24-5

Creating a text-based service in Automator

877005-fg2405.tif 

This service is going to work on blocks of selected text, so Automator should be instructed that the service receives selected text from any application, as shown in Figure 24-5.

The next step is to convert the selected text into an audio file. Automator has an action for this called, not surprisingly, Text to Audio File. You can find this action using the Automator search bar. Locate the action and drag it onto your Automator service. Your screen should now look like that in Figure 24-6.

You have several options when converting text to an audio file. First, you choose a voice. Mac OS X has several built-in computerized voices; the most natural-sounding voice is Alex. Several of the other voices, however, are entertaining; I particularly like Bruce and Zarvox. You can select various voices and press the Play button to get an example of each.

Once you’ve chosen a voice, create a name for the file. In this case, call it text2speech -. Include the hyphen at the end because the service will later add a time stamp (to avoid duplicate file names). You can save the file anywhere on your Mac. In the example, I’m putting it on the desktop. Your Automator window should now look like that in Figure 24-6.

Figure 24-6

The Text to Audio action in Automator

877005-fg2406.tif 

At this stage, you have selected text, converted it to an audio file, and saved it to the desktop. Next you need to add a date and time stamp. To do this, use two instances of the Rename Finder Items action covered in the previous section’s example of an image-resizing workflow. The first instance adds the date, and the second instance adds the time.

Next, use the Import Audio Files action to convert the audio file to the standard iTunes format, AAC. Search out the action and drag it onto your service. Finally, import the AAC audio file into iTunes, using the Import Files into iTunes action. You can import the audio file into an existing playlist or create a new one. (The example creates a new playlist called Text to Speech.) The completed service is shown in Figure 24-7.

Figure 24-7

The completed Text to Speech Automator service

877005-fg2407.tif 

The service is completed. Now you just need to save it. Choose File Save or press Command+S. Automator prompts you to provide a name for your new service; use Text to Speech.

Now, test it. Open your e-mail application or Web browser and find a large block of text. Select the text with your mouse, then Control+click or right-click the selected text to open the contextual menu. In that menu, mouse down to the Services directory and choose the Text to Speech suboption.

As soon as you activate the service, Automator goes to work: It takes the selected text, converts it to an audio file, renames it, converts it to AAC format, and brings it into iTunes in a matter of seconds. You can then synchronize that playlist to your iPod, iPhone, or iPad and listen to it on your ride home. I spend an hour a day commuting and get a lot of my “reading” done while behind the wheel.

If you don’t want to fiddle with Automator but still want the benefit of using services, there are many services available for download and installation on your Mac. The best resource is www.macosxautomation.com/services.

Automator is amazing. You can, in essence, program your Mac without any programming knowledge. It does, however, have its limits: Automator is linear. When creating more sophisticated computer programs, you can instruct the program to jump around and make decisions for you. Automator doesn’t work that way. It is more like an assembly line: You put a file in one end, and something else comes out the other.

Automator is also limited by its available actions. Although third-party software developers and Apple are always adding actions to Automator, if your workflow needs a step for which there is no action, you’re probably out of luck. There is a way to create your own custom actions, but doing so requires programming knowledge of AppleScript (covered later) or one of the Unix shell scripting languages (such as Bash, Perl, Ruby, and Python), which goes beyond the scope of this book.

AppleScript

So what if you hit the wall of Automator’s limitations? In that case, it is time to step up your game and learn AppleScript. AppleScript is a scripting language designed by Apple for Mac OS X. Unlike most programming languages, AppleScript tries to use a natural-language model so commands are easier to understand.

You won’t learn AppleScript in an afternoon and a few pages of this book. Despite its simplicity, AppleScript is still a computer language, after all, and several lengthy books are published on mastering it. Nevertheless, let me give you a taste of AppleScript through a few AppleScript projects.

AppleScript Editor

877005-ma2402.tif AppleScript Editor, found in the Utilities folder of the Application folder on your Mac, is the built-in Mac OS X application for writing and executing scripts. Shown in Figure 24-8, the AppleScript editor has two panes. The top pane is where you type your AppleScript code. The bottom pane has two subpanes where you can display either a description of your AppleScript or an event log showing the results of your AppleScript.

Figure 24-8

AppleScript Editor’s main window

877005-fg2408.tif 

Some simple scripts

The idea behind AppleScript is to make it as comprehensible to nonprogrammers as possible. As a result, the syntax works largely with natural (English) language. For example, programming your computer to talk is normally a pretty complicated task. Not so with AppleScript. Type the following in the script editor:

say “I can be used for work”

Click the Compile iconic button in the toolbar. Then make sure the volume is turned up on your Mac and click the Run iconic button in the toolbar.

Figure 24-9

The AppleScript Say command

877005-fg2409.tif 

Guess what? You just wrote your first AppleScript. Let’s change the computer voice now. Type the following in the AppleScript Editor window, as shown in Figure 24-9.

say “shall we play a game?” using “Zarvox”

Now compile and run the script.

As you can see, AppleScript is more accessible to nonprogrammers than more traditional computing languages.

As another example, if you want AppleScript to open your startup disk, type the following script.

tell application “Finder” to open the startup disk

Here is a more complex example:

set text_entry to “I like to use my Mac at work!”

display dialog “Ready to Script?”

tell application “TextEdit”

activate

make new document with properties {text:text_entry}

end tell

Taking the above example, let’s look at each line of AppleScript code to see what it does.

set text_entry to “I like to use my Mac at work!”

First, the script sets a variable called text_entry and fills it with the words “I like to use my Mac at Work!”

display dialog “Ready to Script?”

Next, the script creates a dialog box.

tell application “TextEdit”

The tell command is how AppleScript works with specific applications. In this instance, AppleScript is telling TextEdit, “I’m about to give you some work.”

Activate

Activate is the AppleScript command to start the application. TextEdit will launch upon this command.

make new document with properties {text:text_entry}

This is an example of AppleScript’s plain-language scripting. To make a new document, the command is make new document. The rest of this command adds the text from the text_entry variable to the new document.

Finally, we tell TextEdit we are done working with it:

end tell

Although these examples are admittedly simple, they do show the utility of AppleScript. Many Mac OS X software developers include AppleScript support in their software code, meaning that they include AppleScript commands in their software that AppleScript can address and thus you can access from an AppleScript you write. Apple Mail, for example, has an AppleScript command to pull an e-mail address from a mail message. (The command is Extract Address From.) This command is useful if you have 700 e-mails and want to pull their e-mail addresses and add them to an Address Book contact group.

AppleScript includes a dictionary that lists all the available commands for your installed applications. You can access the dictionary in the AppleScript Editor by choosing File Open Dictionary. Figure 24-10, shows the Extract Address From entry in Mail’s AppleScript dictionary.

Figure 24-10

The AppleScript dictionary

877005-fg2410.tif 

Once you become proficient in AppleScript, you can use the dictionary terms to string together AppleScript commands on your Mac. Because many software developers implement AppleScript commands, you can have applications talk to one another. For example, you can have Preview modify a photograph and then drop that image into a Microsoft Word document. Experienced scripters can perform magic on their Macs with AppleScript.

You can even save bits of AppleScript code and install them as actions in Automator workflows. For example, to add voice prompts to your Automator workflows, use the Run AppleScript action in Automator, as shown in Figure 24-11. This is a useful script because there is no built-in Automator action to speak.

Figure 24-11

Running an AppleScript in Automator

877005-fg2411.tif 

If you want the benefit of AppleScript but don’t want to be bothered to learn how, there are several useful scripts available on the Web that you can download and run in AppleScript Editor. Two of my favorite AppleScript and Automator Web sites are www.macosxautomation.com and www.macscripter.net. Both sites have a rich assortment of downloadable AppleScripts and Automator Workflows. MacScripter also has an active user community, so you can get advice and help with your automation.

Script Debugger

877005-ma2403.tif AppleScript Editor is a great tool to learn AppleScript, and it comes free with every new Mac. But if you really enjoy working with AppleScript, upgrade to a more advanced AppleScript editing application. The best one is Script Debugger ($200; www.latenightsw.com). Script Debugger includes tools not available in AppleScript Editor. Script Debugger’s dictionary support, for example, runs circles around the standard AppleScript Editor dictionary, providing more detailed information about the available commands in each of your applications, such as usage examples and syntax.

Script Debugger also lets you run your AppleScript code one step at a time so you know exactly what is going right (and wrong) when debugging. Script Debugger is a necessary tool for any power scripter.

Using Automator and AppleScript, you’ll be surprised how much more efficient you can be on your Mac. Good luck!