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 -- HowdyYou 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 -- 9You 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 LostWhen you use set (as opposed to copy) to set a variable to a value that is a script object, you set the variable by ...
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