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 ...

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.