Script Objects as Values

A script object is a datatype in AppleScript. This means that a variable’s value can be a script object. In fact, a script object definition basically is such a variable, one whose name is the name of the script object. You can refer to this variable, and get and set its value, just as you would any other variable. Here, we fetch a script object as a value and assign it to another variable:

script myScript
        display dialog "Howdy"
end script
local x
set x to myScript
run x -- Howdy

You can also assign a new value to a script object. No law says that this new value must be another script object; you’re just replacing the value of a variable, as with any other variable. So, you could do this if you wanted:

script myScript
        display dialog "Howdy"
end script
set myScript to 9
display dialog myScript -- 9

You can assign a script object the value of another script object, in effect replacing its functionality with new functionality. Of course, that new functionality must be defined somewhere to begin with. For example:

script sayHowdy
        display dialog "Howdy"
end script
script sayHello
        display dialog "Hello"
end script
set sayHowdy to sayHello
run sayHowdy -- Hello

Set By Reference

When you use set (as opposed to copy) to set a variable to a value which is a script object, you set the variable by reference. This means that the script object is not copied; the variable’s name becomes a new name for the script object, in addition to any existing names for the script ...

Get AppleScript: The Definitive Guide now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.