Previous Page
Next Page

B.2. JavaScript

The free JavaScript OSA component file from Late Night Software (see "Other Scripting Languages" in Chapter 3) implements an OSA language that appears in your script editor application; this language is JavaScript with some additions that allow it to construct and send (and receive) Apple events.

Actually, JavaScript OSA can construct Apple events in two ways. If you like, you can construct raw Apple events by a process quite similar to the earlier REALbasic example. However, JavaScript OSA can also read the dictionary of the target application in real time and incorporate its terminology into the JavaScript language. The target application is, in effect, represented by an object with properties and methods corresponding to AppleScript attributes and commands. Here is code to generate and send our model events:

with (MacOS.appBySignature("R*ch")) {
    make(_types.document);
    document[1].valueOf( ).text = "Hello, world!";
}

This mode of expression is extraordinarily compact and direct. A with clause and dot notation replace the chain of ofs and tells; true assignment with an equal-sign replaces set. Elements appear as arrays. Certain distinctions that are muddied by AppleScript are clarified; the term document is different as an element and as a class, for instance.

JavaScript OSA can't express every kind of Apple event (in particular, boolean test specifiers are not supported), but this is compensated for by the powerful combination of the cool and elegant object-oriented JavaScript language with the automatic implementation of AppleScript terminology. Most important, JavaScript OSA is an OSA language, meaning that it's usable just about anywhere you would use an AppleScript compiled script file. For instance, many of the scripts I use with script runners such as Entourage's and BBEdit's Script menus are written in JavaScript.

To learn more about JavaScript OSA, download it and look through the "Introducing JavaScript OSA" manual (written by yours truly).


Previous Page
Next Page