Writing Your Own Simple Scripts (MacBook)

Using someone else’s scripts is fun and all, but the real joy of AppleScript comes when you create your own. Not only can you customize a script to your own needs and desires, but saving all those keystrokes can really produce a feeling of euphoria. (Okay, perhaps just Snow Leopard power users will actually experience a heightened sense of existence . . . you’ll be there soon.)

Create a script without touching a key

You needn’t wear a pocket protector or tape the bridge of your glasses to become proficient with AppleScript. In fact, the AppleScript Editor can get you up and running with AppleScript in no time at all. The secret weapon of the AppleScript author is the Record function of the AppleScript Editor. You click the Record button, perform one or more actions in a recordable application, and then return to the AppleScript Editor, where you click the Stop button. The AppleScript Editor stores each of your actions and compiles the whole list into an AppleScript.
In theory, this is how it should all work, but in reality, finding recordable Macintosh applications isn’t always so easy. The Finder is, perhaps, the most recordable application on the MacBook. Although some other applications support recording, so few do that the Finder could be the only recordable application most Mac users ever see.

To try it yourself, take the following steps to automate actions in the Finder:

1. Bring AppleScript Editor to the foreground.
If AppleScript Editor isn’t currently running, double-click its icon in a Finder window. If it is running, click its icon in the Dock.
2. Create a new script by pressing 96+N.
3. Click the Record button.
The Record button is one of four buttons positioned near the top left of a new script window. Refer to Figure 2-3.
4. Switch to the Finder and perform the actions that you want to automate.
When the Finder is active, you can select some icons on the Desktop and move them around, resize any open Finder windows, or navigate to your home directory. Any action that you perform in the Finder should be acceptable fodder for the AppleScript Editor. When you perform tasks in the Finder, the AppleScript Editor automatically generates a script that replicates your actions.
5. Return to the AppleScript Editor and click the Stop button.
To reactivate the AppleScript Editor, click its icon in the Dock. Click the Stop button to cease the recording of your script.
When you’re finished, you should be looking at a complete AppleScript. To test your work, return to the Finder and return any icons or windows that you might have moved or repositioned to their original locations. (You don’t want to run a script that doesn’t appear to have any effect.) Then return to the AppleScript Editor and click the Run button to watch your automated Finder tasks being performed.

Building your own scripts

An AppleScript novice can perform all kinds of amazing feats with the recording features of the AppleScript Editor. Because AppleScript uses a kind of pseudo-English language, it’s usually pretty easy to figure out what’s going on behind the scenes. Consider the following script for an example:
(By the way, in the preceding code, I bolded and italicized the commands you’ll be working with, but they don’t have to be bolded for the script to work.) Anyway, the first thing that you might notice about this script is the first line: the tell command, which indicates that this script relates to the Finder. This script activates the Finder, creates a list of open windows, and then examines the state of each window: Is it collapsed or not collapsed? (In Snow Leopard, a collapsed, or minimized, window appears in the Dock.)

One of two possible results occurs:

♦ If the window is already minimized and in the Dock, nothing happens and the script continues through the list of windows.
♦ If the window isn’t minimized, the script collapses it.
This continues until the script has examined all open windows. The end result? All open windows end up minimized in the Dock.
Another thing to note about this script is that it has two comments in it (save list and do nothing). Comments can help you remember what you were thinking months later when you open the script again. Although comments help us humans know what’s happening, they don’t really have any other function. An AppleScript comment begins with two dashes (–).
Here’s a big-time Maxim that every script author should remember:
Comments are your friend!

Next post:

Previous post: