From a technological and historical perspective, AppleScript is one of the greatest innovations and distinguishing features of the Mac OS. The System provides not only a mechanism for applications to communicate with one another, ordering one another about, getting information from one another, and generally collaborating to avail themselves of one another’s strengths and abilities, but also a way for ordinary users to take advantage of this mechanism programmatically. The user can write and execute code in the AppleScript language as a way of automating the behavior of applications, reducing many steps to one, throwing the burden of repetition and calculation onto the computer, and combining the powers of multiple applications into a seamless united workflow. AppleScript is a labor-saving device that lets ordinary users program the computer for themselves; and, after all, labor-saving and programming are just what computers are all about.
Although AppleScript was long treated by Apple itself as something of an unwanted, troublesome step-child—and has even (according to apocryphal legend) at times come perilously near being tossed onto the scrapheap—it has lately prospered, and is now perhaps entering a kind of Golden Age. AppleScript has been embraced and acknowledged and is starting to take its rightful place in the firmament of Apple’s star technologies. It is noticed on Apple’s own web pages as a major aspect of Mac OS X (for example, see http://www.apple.com/macosx/overview/). The Script Editor has been rewritten as a Cocoa application. Scripts may be run from a System-wide menu. More and more of Apple’s own new applications are scriptable. Integration with Unix scripting has been provided. AppleScript can even be used to drive applications that are not technically scriptable at all. And users can actually write a genuine application with a full-fledged Aqua user interface—windows, menus, buttons, text fields, scrolling lists, and more—using AppleScript as their programming language, thanks to the astounding AppleScript Studio. And it all comes for free as part of Mac OS X.
In this context, with interest in AppleScript waxing anew, the need for a complete explanatory manual and reference is greater than ever. In that spirit, this book is offered. It is hoped that it will prove helpful to AppleScript’s beginning and veteran users alike. No prior knowledge of AppleScript is assumed, nor any previous programming experience, so that the complete beginner can use this book to learn AppleScript from the ground up; at the same time, the book aims at such a degree of technical depth and completeness as will satisfy the needs of those who wish only to consult it to check some point of syntax, or to gain a firmer understanding of such advanced arcana as how the scoping rules operate, how terminology is resolved, or what an Apple event really is.
What should be the scope of a book about AppleScript? This is a tricky problem, and one that earlier books, in my view, have not always dealt with satisfactorily. The trouble is that AppleScript is really two subjects. First, there is what one may call AppleScript itself, a “little language,” not particularly useful or powerful on its own, but ready to talk to scriptable applications and to take advantage of their utility and power. Second, there is AppleScript as extended and implemented by particular scriptable applications: how to use AppleScript to talk to the Finder, how to use AppleScript to talk to Adobe Photoshop, how to use AppleScript to talk to QuarkXPress, and so forth.
On the whole, this book makes no attempt to treat this second aspect of AppleScript. This may be surprising to readers accustomed to some earlier books, but I believe it to be the right decision nonetheless. AppleScript as implemented by particular applications is a massive, encyclopedic subject. It would be easy to write an entire book of techniques, tricks, and tips for scripting just one major application. And the scope of any attempt to do this for every scriptable application would be open-ended, since it is impossible to know what scriptable applications the reader has or might acquire, and since new applications, any of which might be scriptable, are being developed all the time. Also, such treatment is largely unnecessary. Every scriptable application includes a dictionary telling the user about how it extends the language; and the user can employ this, together with trial and error, and possibly examples from documentation and the Internet, to obtain pretty fair mastery over the art of scripting that application. There might even be books on the exact subject the reader is interested in. (Thus, for example, it is far better that the reader should consult Ethan Wilde’s book Adobe Illustrator Scripting with Visual Basic and AppleScript than that the present book should attempt to compress a treatment of the same material into some reduced and undoubtedly inadequate form.)
My choice, therefore, is between concisely teaching the reader to fish and giving the reader a large pile of possibly quite unnecessary fish. Readers who know anything of my work (or anything about fish) will know instantly which choice I would make! Rather than trying to encompass the details of scripting every application, my approach in this book has been to explain the AppleScript language itself, and to describe how a dictionary works and what a user can and can’t learn from it, providing examples from across the range of applications that I actually use, so that the reader will be mentally equipped and educated and able independently to study and experiment with scripting any application.
Besides, books about the first aspect of AppleScript—about AppleScript itself—have been surprisingly few and far between. It is here that the need exists. The fact is that I have never seen the AppleScript language taught, explained, and documented in what I would regard as a clear, rigorous, and helpful way. Considering how long AppleScript has been around, it is hard to explain this lack. It may have partly to do with the lack of clear explanation from Apple itself. After all, Apple wrote AppleScript, and only the folks at Apple have access to AppleScript’s inner workings. Yet the only Apple manual of AppleScript, the AppleScript Language Guide, generally lacks explanatory depth.
There is a kind of unspoken myth—we may call it the “ease of use” myth—that tries to give the impression that AppleScript is so easy and intuitive that it doesn’t really need explanation. Apple possibly didn’t want users to see AppleScript as a full-fledged programming language, with all the precision, complexity, and sophistication that this entails, because that would be something that users would have to learn, exercising those parts of their brain to which a Macintosh, with its windows and icons and colorful buttons, isn’t supposed to appeal. Instead, AppleScript is supposed to be so simple, so thin, so easy, so English-like, so intuitive, that there is hardly anything to learn in the first place; just pick up an application and its dictionary and presto, you’re ready to script it.
Nothing could be further from the truth. First you must learn the language; only then will a dictionary make sense and be useful. AppleScript is not a mere veneer, an intuitive and obvious “glue” for hooking together the terms from an application’s dictionary into sentences that will script that application as the user desires. On the contrary, it’s a real programming language—a really interesting programming language (not least because it’s fairly cranky, opaque, quirky, and oddly designed). To conceal this fact from the potential user of AppleScript does that user no favor whatever. Every day I see on the Internet users who are starting AppleScript, who seem to imagine that they’re just going to “pick it up”—that their AppleScript code will somehow just write itself. Well, it won’t. A beginning user who expects to cut to the chase, to pick up an application’s dictionary and just start scripting, is likely to give up in frustration. As Socrates said of virtue, AppleScript isn’t just something we all somehow are born knowing; it must be learned, and therefore it must be taught. There is nothing about AppleScript that makes it any less susceptible to scrutiny, careful description, and ordered, Euclidean explanation than any other language.
In this light, I have written the AppleScript book that I have for so long myself wished to read. Also, I have always found myself rather confused about AppleScript—I could use it with reasonable effectiveness, but I was always somewhat hazy on the details—so writing this book has been an opportunity for me to dispel my own confusion. The result is a reasoned, rigorous, step-by-step presentation of the AppleScript language, intended for instruction and for reference, a studious, patient, detailed, ordered exposition and compendium of the facts. This book presents AppleScript as a programmer, a student, a thinker would learn it. In short, it’s just what I’ve always wanted! This book has helped me tremendously: before I wrote it, I didn’t really quite understand AppleScript; now I believe I do. I hope reading it will do the same for you.
Get AppleScript: The Definitive Guide 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.