Compiled Script Files as Script Objects

A script can read a compiled script file and incorporate its contents as a script object. This provides a way for scripts in different files to refer to one another. You might use this facility as a means of persistent storage, in combination with the fact that top-level entities in scripts survive being saved as a compiled script file; or you might use it as a way of building a library of commonly needed routines.

This facility depends upon three verbs, described here, that are not part of AppleScript proper; they are implemented in a scripting addition (Chapter 4) that is standard on all machines.

Reference Section

Reference Section

Reference Section

Library

A compiled script file may be used as a place to store commonly needed routines. A file used in this way is called a library. A running script can then access the contents of the library using load script . The library’s top-level entities, including its run handler, are then available to the running script.

For example, suppose we have saved the handler makeFilterer (see the end of Section 9.5.3) in a compiled script file makeFilterer.scpt. We can then call makeFilterer from another script:

set s to load script file "myDisk:makeFilterer.scpt"
on isNumber(x)
        return ({class of x} is in {real, integer, number})
end isNumber
tell s's makeFilterer(isNumber) to filter ({"hey", 1, "ho", 2, 3})

That code assigns the entire script of the compiled script file makeFilterer.scpt to a variable

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.