One Step Beyond: AppleScript Programming (MacBook)

Creating AppleScripts can soon become very involved, bordering on programming. Don’t let that term programming scare you away, though. You needn’t be a software developer to take advantage of AppleScripts. Apple provides a lot of help to get you started along the AppleScript trail.

Grab the Dictionary

Perhaps the greatest resource for AppleScript novices and experts alike is the AppleScript Dictionary. Although many applications are scriptable, not all are. To be scriptable, an application must contain an AppleScript Dictionary. An AppleScript Dictionary details the various commands and objects of an application that you can access via AppleScript.
The AppleScript Editor application allows you to peer inside an application and view its AppleScript Dictionary. To open an application’s Dictionary, choose FileoOpen Dictionary. Mac OS X searches through your installed applications and presents you with the Open Dictionary dialog, as shown in Figure 2-4, which lists all applications that have a dictionary and are therefore scriptable.
If you don’t see your favorite application in the list, alas, it’s probably not scriptable. To make certain, click the Browse button and select the application in question.
After you select an application and click Choose, the AppleScript Editor displays that application’s AppleScript Dictionary. An application’s dictionary lists all the features of that application that are scriptable.
Scriptable features are divided into categories, called Suites, which you can see on the left side of the AppleScript Dictionary. Every Mac application is supposed to support the Standard Suite, which lists common terms that most applications should support.
Viewing the AppleScript Dictionary within the AppleScript Editor.
Figure 2-4:
Viewing the AppleScript Dictionary within the AppleScript Editor.
Click an item in the Suite on the left side of the Dictionary to view detailed information about its capabilities, as shown in Figure 2-5.
Click an item in a Suite to view details about its capabilities and syntax.
Figure 2-5:
Click an item in a Suite to view details about its capabilities and syntax.
By surveying the various Suites of an application, you begin to see what tasks you can automate. The Finder, with its huge AppleScript Dictionary, is perhaps the most scriptable of all applications (followed closely by mammoths like FileMaker Pro).


Anatomy of a simple script

Although a full discussion of AppleScript programming is beyond the scope of this topic — after all, we have other things to talk about, too — that doesn’t mean that you can’t produce some quick and useful scripts. Most AppleScripts begin with a command that addresses the application that you want to automate. Enter this command into a new AppleScript document, which you create by pressing 96+N.

tell application “Finder”

This is like saying, “Hey, Finder, listen up! I’m going to send commands your way!” The double quotation  surround the application name that you’re addressing in the command.
Similarly, after you finish instructing the Finder what tasks you want performed, you must also tell it to stop listening. As such, typical scripts end with end tell
With the shell of a script in place, all you have to do is add commands between the tell and end tell commands of the script. If you want your script to force an application to the foreground, an activate command is usually the first line within the shell of your script.

tell application “Finder”

activate end tell

Believe it or not, this is technically a complete and valid script! It doesn’t do much, though, so add some more functionality to make it accomplish something worthwhile. For example, suppose you want to perform some housekeeping chores each time you log in to your MacBook. Some desirable tasks might include
♦ Emptying the Trash
♦ Having your laptop say “Hello!” to you
(Okay, I’ll grant that hearing your MacBook say “Hello!” isn’t a housekeeping chore, but it makes the whole script that much more fun — and really impresses your visitors, too. No one ever said programming had to be boring!)
To add these functions, you can do so by using a language you already know: English. As I mention earlier, Apple tries (and sometimes succeeds) to make AppleScript as English-like as possible. That way, you don’t have to know some silly computer language; just use your native tongue. For example, to empty the Trash, tell the Finder to do so.

empty trash

The trickiest line of code might be the speech, and that’s only because you need to remember to add quotation . AppleScript thinks that anything without quotation  is an AppleScript command.

say “Hello!”

The result is a super-simple script that anyone can read but that performs two powerful functions. The completed script looks like this:
tmp116-26_thumb[2]
After you complete the script, choose FileOSave to save your script. Because you want the script to execute and then quit, use the File Format field to save it as an Application. Also, make sure that the Stay Open check box and the Startup Screen check box are deselected. And don’t forget to name your script in the Save As text field.
To have the script automatically run each time you log in to your MacBook, save the script anywhere you wish. (Assign its location in the Where field of the Save window.) Open System Preferences by clicking its icon in the Dock; then click the Accounts icon. Make sure that your account is selected and then click Login Items. Click the Add button (which proudly bears a plus sign) and navigate to your script in the Open dialog that appears. After you click the Add button of the Open dialog, you see the script in the Login Items window.

Next post:

Previous post: