1.2. Is This Application Scriptable?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 freeso 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 applicationit 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 expressionsand 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:
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 applicationnamely, 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 bookfor example, in Chapter 2, Chapter 3, and especially Chapter 20you'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.) |