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 -- HowdyYou 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 -- 9You 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 -- HelloSet 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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access