AppleScript isn't just a language; it's an underlying technology supporting that language. Because this technology is present as part of the system, you get it for free—so you may as well take advantage of it. And because you know this technology will be present on any Mac OS computer, you can share with others any useful AppleScript program you happen to write.
So AppleScript is omnipresent. But it's not omnipotent. AppleScript, remember, is all about telling an application to do automatically things of the sort you might make it do manually. But AppleScript does not let you tell every 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 called, appropriately enough, Dictionary (located in /Applications). In Script Editor, you can open the Dictionary application's dictionary display; this dictionary has about two dozen entries. But it's a false positive; the Dictionary application is not really scriptable. What you're seeing are merely some classes and commands inherent in any Cocoa application, merely by virtue of being a Cocoa application. The way you know this is that none of these classes and commands has anything to with the primary function of the Dictionary application—namely, looking up the definition of a word.
What you're looking for, in other words, are applications whose scriptability exposes their true power so that AppleScript can automate them usefully. The scriptable applications I use with some regularity include many of those supplied by Apple as part of Mac OS X, such as Address Book, iCal, iTunes, Mail, Safari, Apple System Profiler, and the Finder. Then there are important third-party programs like Microsoft Word, Excel, and Entourage, FileMaker Pro, Interarchy, BBEdit, StuffIt Expander, and GraphicConverter. You might also have QuarkXPress, or any of the heavily scriptable Adobe applications such as Photoshop, Illustrator, InDesign, or Acrobat. A delightful recent trend is that Apple has made it increasingly easier to add scriptability to Cocoa applications, so new applications are tending to be scriptable. Examples are OmniOutliner and OmniGraffle, Hog Bay Notebook, Intaglio, and many others.
(As you read this book—for example, in Chapter 2, Chapter 3, and especially Chapter 20—you'll learn much more about an application's dictionary, how to navigate it, what it does, and what it tells you. See Chapter 23 for more about the scriptable applications included with a default Tiger installation.)
Get AppleScript: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.