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 defines exactly such a variable. 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
set x to myScript
run x -- Howdy

You can also assign a new value to a variable whose value is a script object. No law says that this new value must be another script object; you're just replacing the value of the variable, as with any other variable. The original script object is lost if this variable name is your only way of referring to it. 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 to a variable whose value is 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. The old functionality is lost if this variable name is your only way of referring to it. For example:

script sayHowdy
    display dialog "Howdy"
end script
script sayGetLost
    display dialog "Get Lost"
end script
set sayHowdy to sayGetLost
run sayHowdy -- Get Lost

When you use set (as opposed to copy) to set a variable to a value that is a script object, you set the variable by ...

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.