Development software

Automator overdrive

If you’ve taken Automator out for a spin, you know that Apple’s automation tool really does make it easy to simplify repetitive tasks. But after experimenting with its built-in actions and with actions culled from the Internet, you probably still wish you could do more. Luckily, lots of other automation tools work flawlessly with Automator. Put them together, and you’re ready to shift into high gear.

Mix Automator and AppleScript

Of course, AppleScript is a little geeky (it’s a programming language, after all), but it’s got more power than a Porsche—and that makes it a perfect complement to Automator. (To get up-to-speed with Automator basics, see Take Control of Customizing Tiger. For AppleScript basics, see Amazing AppleScript!.) Link the two by using Automator’s Run AppleScript action. That way, you can take advantage of the drag-and-drop ease of Automator for the simple stuff, and use down-and-dirty AppleScript code for the hard stuff.

Enjoy the Weather Here’s how you’d use these tools to put a current weather map of the United States on your desktop. Launch Automator (/Applications). Then drag the Automator: Run AppleScript action into the pane on the right of the Automator window.

Switch to the Finder. Go to the /Library/Scripts /URLs folder and double-click on the Download Weather Map.scpt file. Apple’s Script Editor application will open with this script’s code revealed. The script goes out to the Internet and downloads an up-to-date weather map. (Click on the Run button in Script Editor to see for yourself.) Select every line of code except the last one, which begins with

tell application "Finder"
. (That line opens the file. You want the image to fill your entire desktop.)

One-Button Workflow Combine QuicKeys and Automator to trigger complex workflows with one keystroke. If you’d like your shortcut to work only in certain programs, use the All Parts Of All Applications pop-up menu.

Copy the text and switch to Automator. Paste the text in place of the generic

(* Your Script Goes Here *)
line in the Run AppleScript action. Now run your workflow by clicking on Automator’s Run button. A file named weathermap.jpg will appear on your desktop. (This step is important—you must leave the weathermap.jpg file on your desktop so Automator can use it as your desktop picture.)

Drag the Finder: Get Specified Finder Items action to the bottom of your Automator workflow. Click on the plus-sign (+) button and navigate to the weathermap.jpg file on your desktop. Click on Open. (This tells Automator that you intend the rest of your workflow to operate on the weather map file.) Now drag the Preview: Pad Images action to the end of your Automator workflow. A dialog box will ask whether you want to make a copy of the image. Click on Don’t Add.

Fill in the width and height of your screen in the appropriate text boxes. (Find yours by opening the Displays preference pane and noting which item is selected in the Resolutions list.) Select the Scale Image Before Padding option to ensure that your image fits the screen just right.

Drag the Automator: Run AppleScript action to the end of your workflow. Now you’re going to incorporate another AppleScript—the code that sets your desktop picture. In the new Run AppleScript action, type the following in place of

(* Your Script Goes Here *) :

	tell application "Finder"
	select window of desktop -- in preparation for changing the desktop picture
	set desktop picture to POSIX file "/Library/Desktop Pictures/Solid Colors /Solid Gray.png" -- to make the desktop refresh itself
	set desktop picture to file (input as text) -- "input" is weathermap.jpg
	end tell

Now, whenever you run your workflow, your Mac will fetch the newest weather map from the Internet and—like magic—place it on your desktop. ( Download this workflow, and fill in the information for your system.)

Merge Automator with Office macros

Automator actions that control Microsoft Office 2004 ($399) are exceedingly scarce. But it’s easy to record a macro (shortcut) in an Office program and then link it to an Automator workflow. The trick is to use a short AppleScript as the mediator.

Create a Secure, E-mail-Ready Archive Using some tricks from the previous workflow, the following procedure produces a workflow that automatically password-protects and compresses your frontmost Microsoft Word document. First, launch Microsoft Word and open a document that you’d like to encrypt. You’ll password-protect this document once to teach Word how to automatically password-protect any document.

Choose Tools: Macro: Record New Macro, and type

in the Macro Name text field. Click on OK. Now Microsoft Word is recording everything you do. Choose Word: Preferences, and then click on Security in the list. Type the password you want to use in the Password To Open text field. Word will now require this password when anyone tries to open the document. Click on OK and then confirm the password. Choose File: Save, and then choose Tools: Macro: Stop Recording.

Switch to Automator, create a new workflow, and add the Automator: Run AppleScript action to the workflow. Replace all the text in the script box with the following code:

	on run {input, parameters}
	tell application "Microsoft Word"
	run VB macro macro name "Encrypt" -- run the macro
	return {(the path of the front document & ":" & the name of the front document) as alias} -- pass the name of the Word document on to the next action
	end tell
	end run

Add the Finder: Create Archive action to the end of the workflow. This action takes your newly encrypted Word file and compresses a copy for you. Type a generic name, such as

, into the Save As field. From the pop-up menu, select the location where you want the archives to appear.

To save your new workflow, select File: Save and give it a name. Select Workflow from the File Format menu. To make this work with Excel, just repeat the preceding steps, but record the macro in Excel and replace

in the AppleScript code with

Trigger workflows with QuicKeys

Startly Technologies’ QuicKeys (   ) has long been one of the most popular automation tools for the Mac. Now it’s more powerful than ever, with features that include Automator integration. (The tricks described here require the newest version of QuicKeys for Mac OS X. Get a 30-day demo.)

Make Workflow Magic As helpful as your last workflow is, it still requires that you switch out of Microsoft Word, launch Automator, and click on Run. A QuicKeys shortcut will save you time.

Open QuicKeys and choose Create: Scripting: Workflow. An Untitled Shortcut window will appear. Click on Select, and navigate to the Encrypter workflow you just saved. Click on Choose. Click on the plus-sign pop-up menu in the middle of the window on the right side, and choose QuicKeys: Message. When QuicKeys completes a workflow, it doesn’t let you know; if you want some feedback, select the Play Sound option here and pick a sound from the pop-up menu.

To assign your keyboard shortcut, click on the plus sign near the Scopes & Triggers header. By default, QuicKeys will assign this workflow the F3 shortcut if you haven’t created any other shortcuts with the program (see “One-Button Workflow”). If you’re already using that function key, change your new QuicKeys shortcut by clicking on the Hot Key pop-up menu and pressing another function key or key combination. Choose File: Save Shortcut. Give your shortcut a name (

, for example), and click on OK.

That’s it! Now, as long as QuicKeys is running, whenever you press your hot key, your Automator workflow will spring into action—encrypting and compressing the current Word document.

[ Adam Goldstein is the author of AppleScript: The Missing Manual (O’Reilly, 2005). ]

Subscribe to the Create Newsletter