Chapter 9. Script Objects

A script object is a script within a script. In fact, a script really is a script object, but it also has a special status as the top-level script object—the script object that contains all others. A script object is defined using a block of code that starts with the keyword script, with syntax of this sort:

script scriptName
        -- commands within the script object
end script

Tip

When typing a script definition, don’t bother to type the full phrase end script in the end line. Just type end for that line; the compiler will fill in the word script.

A script object may be defined anywhere—at top level, within a script object, or within a handler. The mere presence of a script object definition does not itself cause execution of any code within the script object. A script object’s code is run only on demand. (Later sections of this chapter, such as Section 9.2 and Section 9.3, discuss how to make such a demand.)

Like a script, a script object may contain script properties, handlers, script objects, and code. A script object is a device for organization of code and data. Related handlers, script objects, and variables can be packaged in a single self-contained script object. Also, a compiled script file can be accessed as a script object by a running script; thus a script object can function as a library of commonly needed code, or as persistent storage for variable values.

Together with variables and handlers, script objects complete a script’s “map of the world.” ...

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.