Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [272]

By Root 1600 0
script.

AppleScript has the power to do very far-reaching things, such as deleting files and wrecking your document. You want to be very sure things are working before you throw the switch that says, "This is not a drill."

Know the language.

It's true that in the course of development I did a lot of guessing about FrameMaker's object model, but I didn't guess about the language itself. I couldn't have written this program at all if I hadn't known already what my and it mean, and how to use tell and of, and how to form a boolean test specifier, and what the difference is between a property and an element. AppleScript may look like English, and that might make you think you already know AppleScript because you already know English. If you think that, you're wrong. AppleScript is a rule-based programming language like any other. It is rigorous, choosy, and precise. This book can't teach you to write that one special script you'd like to write, but it can and does teach you the language.

Appendix B. Apple Events Without AppleScript


Considering AppleScript purely as a vehicle for constructing and sending Apple events to scriptable applications (and for receiving and parsing the replies), and in light of the shortcomings and inconveniences of the AppleScript language, one may reasonably wonder why it might not be possible to construct and send Apple events using some other (possibly more attractive) language. Such an approach would have the advantage of letting you work in a language or a development environment you favor, plus at runtime you'd bypass the overhead of the AppleScript compiler and runtime engine. As a matter of fact, there are several such alternative languages to choose from. It is not at all trivial to mold another language to operate on Apple events in a way that parallels AppleScript's own approach, especially because AppleScript does some odd (or buggy) things under the hood, and also because of the inherent problem of extensible terminology; nevertheless, it can be and has been done. This is not, strictly speaking, an AppleScript matter; indeed, it is quite clearly an anti-AppleScript matter! So in theory, discussion of it should have no place in this book. Nevertheless, my own favorite ways of sending Apple events have often included some non-AppleScript language or other, and the subject is certainly an interesting one, so I've included some information on the topic here, as an appendix.

Here, then, are some ways to send Apple events without using AppleScript. Your choice might depend upon what languages you like or are already using, and upon the nature of a particular language's implementation of Apple events. Throughout this appendix, the "model events" (the Apple events we will attempt to construct and send for purposes of example) will be those corresponding to the following script:

tell application "BBEdit"

make new document

tell document 1

set its text to "Hello, world!"

end tell

end tell

At programming level, these events provide a nice balance between simplicity and complexity: we're forming a two-level object specifier, which is a fairly interesting thing, without getting too deep into the nitty-gritty. At the user level, the result is pleasantly satisfying, as (if our code is correct) we can see the new window appear in BBEdit with the words "Hello, world!" proudly emblazoned on it.

Pure Raw Apple Events


If you're a Carbon programmer, you can construct a raw Apple event yourself, directly, in code. A number of other languages, such as Objective-C/Cocoa and REALbasic , give you the wherewithal to do the same thing in a possibly more convenient form. Still, no matter how you slice it, building a raw Apple event is a lot of work, and because every Apple event is different, this approach is not particularly general or flexible. You can compensate by developing libraries of wrapper functions, and Apple provides such libraries (such as the MoreAppleEvents sample code available at the developer web site), but it's still a fairly unpleasant procedure.

Here's some code for

Return Main Page Previous Page Next Page

®Online Book Reader