Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [8]

By Root 1470 0
application to do everything it is capable of. AppleScript works by sending messages to the applications you are automating; these messages are called Apple events. You cannot send just any old Apple event to any old application. (Well, you can, but it might not have any effect.) The application to which you're sending an Apple event must recognize and respond to that Apple event. The ability to recognize and respond to a set of Apple events is a feature of the application. Giving the application this ability is up to the developers of the application—it isn't something that's within your power (unless you are also the developer of the application). An application that has this ability is said to be scriptable . If an application isn't scriptable, you're probably not going to be able to use AppleScript to automate it. (As you'll see in Chapter 24, there is sometimes a way around this limitation, but it should probably be used only as a last resort.) So, before you consider using AppleScript at all, you should have in mind some scriptable application that you want to automate with it.

Not only is AppleScript not omnipotent; it isn't even all that potent. AppleScript is a genuine programming language with some interesting and valuable features, but it's not very powerful or useful on its own. It takes some scriptable application to give AppleScript any real muscle. So, for instance, AppleScript's numeric abilities are limited (it has no built-in trigonometric or logarithmic functions ) and its facilites for text processing are fairly rudimentary (it doesn't support regular expressions , and it isn't even very good at extracting substrings). Granted, these shortcomings aren't as significant as they used to be. Mac OS X is loaded with other scripting languages, such as Perl, which are expert at regular expressions—and AppleScript can drive Perl (and vice versa), so success might simply be a matter of combining specialties appropriately. Nevertheless, the general spirit and intention of AppleScript is that the power should be invested mostly in various scriptable applications, not in AppleScript itself.

Thus it becomes crucial to be able to ascertain whether an application is scriptable. You'd think this would be an easily answered, black-and-white, yes-or-no question. But in fact it turns out to be frustratingly difficult to know whether an application is scriptable. (This book points out many instances of AppleScript's making something simple into something frustratingly difficult.)

You can obtain an initial overall survey of the situation by means of Apple's Script Editor program (it's in /Applications/AppleScript): choose File → Open Dictionary, which displays a list of applications present on your computer that the Script Editor thinks are scriptable. You should, however, regard this list with a bit of suspicion, and confirm that a particular application really is or is not scriptable. To do so, choose Window → Library, and in the Library window, press the "+" button (or Control-click to get the contextual menu, and choose Add). You'll see a standard Open dialog. Navigate to an application, select it, and press Open. One of two things will happen:

The application is reported as not scriptable

An error dialog may appear, stating: "Unable to add the application or extension because it is not scriptable." In this case, the application is definitely not scriptable and that's the end of that.

The application is added to the Library window

This means that the application might be scriptable. But you might have a false positive. To find out, double-click the application's listing in the Library window. This should open the application's dictionary display (see Figure 2-2). Even if it does, you still might have a false positive. Explore the dictionary, clicking the various Suites (in the first column of the browser) and looking through the classes and commands (in the second column) to make sure that these actually do something appropriate to the function of that particular application.

A good example is the application

Return Main Page Previous Page Next Page

®Online Book Reader