Running a Script (MacBook)

The easiest way to get started with AppleScript is to use some scripts that others have written already. Scripts are small files that contain a list of commands; this list of commands tells your Mac what function to perform and when to perform it. Fortunately, Apple is kind enough to provide you with several completed scripts with your installation of Snow Leopard. You can find a large cache of scripts in the scripts folder, found in the Library folder, under Scripts.
Many scripts (but not all) end with the extension .scpt. Before you get started running scripts, however, you should know a few things.

Identifying scripts in the field

Each script that you encounter will be in one of these three formats:

♦ Script application: Some AppleScripts act much like an application. To use one, simply double-click it in the Finder, and off it goes to perform whatever tasks it was meant to do. Depending on an internal setting of the script, it might quit when it’s finished doing its thing. Most often, the script completes its mission and quits. Scripts are typically identified by the icon that you see in Figure 2-1.
♦ Compiled script: You might also encounter AppleScripts that won’t run without the aid of another application. Apple calls these compiled scripts. Although they can’t execute on their own, they do have the abilities of a script built in. They just require a host application to use them.
A gaggle of typical script icons caught by the camera.
Figure 2-1:
A gaggle of typical script icons caught by the camera.
♦ Text file: In addition to compiled scripts and those that act like applications, a third category of AppleScript that you might encounter is a script stored in a text file. Scripts that are stored in a text file also need a host application before they’ll do anything. The main difference between a text file script and a compiled script is that you can read a text file script with any application that can open a text file.


The AppleScript Editor application

Two of the three possible script types (compiled scripts and text files) require some sort of host application before they’ll perform any action. Luckily, Snow Leopard provides you with just such a host: the AppleScript Editor application, which comes with Mac OS X and can execute any AppleScript with ease. With the AppleScript Editor, you can also do much more, including
♦ View or modify an AppleScript
♦ Create a new AppleScript
♦ Check an AppleScript for errors
♦ Save scripts in one of the three possible formats
To launch the AppleScript Editor application, navigate to the Utilities folder (open your Applications folder and then open the Utilities folder) and double-click the AppleScript Editor icon. The AppleScript Editor application displays an empty, script-editing window, as shown in Figure 2-2.

Executing a script

After you have the AppleScript Editor application running, you can run any AppleScript that you can find. To get you started, Apple has conveniently provided a handful of useful scripts. Navigate to the Scripts folder, which is located in the Library folder.
The scripts are divided into folders based on functionality, such as fonts, mail, and navigation. For example, open the Font topic folder, where you’ll find a script named Delete Empty Collections.scpt.
The AppleScript Editor slices and dices . . . and even checks syntax. Order now!
Figure 2-2:
The AppleScript Editor slices and dices . . . and even checks syntax. Order now!
Double-click the script to open it. Because it’s a compiled script and not an application script, the AppleScript Editor automatically loads the script and comes to the foreground (see Figure 2-3). This particular script opens the Font topic application and checks for empty font collections. If any are found, the script displays a prompt asking for confirmation and then deletes the empty collection if you click the OK button. To see the script in action, click the Run button or press 96+R.
Click the Run button in the Editor toolbar to execute the script you've loaded.
Figure 2-3:
Click the Run button in the Editor toolbar to execute the script you’ve loaded.

Next post:

Previous post: