AppleScript_ The Definitive Guide - Matt Neuburg [68]
Accessing Top-Level Entities
The most important fact about a script object's top-level entities is that even though they are not directly visible to code outside the script object, they are accessible on demand to any code that can see the script object. This means that they can be both retrieved and assigned to (fetched and set) by such code.
A special way of talking is required for doing this. Because a script object's top-level entities are not visible outside the script object, it is necessary, in effect, to ask the script object politely to yield access to a top-level entity (and the script object always politely complies). We say that to access a script object's top-level entities, you must target that script object. The syntax for doing this comes in two forms:
Use the of operator (or the 's operator) to specify the top-level entity in relation to its script object. For example:
script myScript
property x : "Howdy"
on sayHowdy( )
display dialog x
end sayHowdy
script innerScript
display dialog x
end script
end script
set x of myScript to "Hello"
myScript's sayHowdy( ) -- Hello
run innerScript of myScript --Hello
Alternatively, refer to the top-level entity within a tell block addressed to the script object. This requires the use of the keyword its, except in the case of a handler call. For example:
script myScript
property x : "Howdy"
on sayHowdy( )
display dialog x
end sayHowdy
script innerScript
display dialog x
end script
end script
tell myScript
set its x to "Hello"
sayHowdy( ) -- Hello
run its innerScript -- Hello
end tell
In the second form, the keyword its (explained more fully in Chapter 11) is required to specify that we mean myScript's top-level entities and not some other variables. If you omit its from the line where you set x, you actually set a different x (an implicit global, as we'll see in Chapter 10), not the property of myScript. If you omit its from the line where you run innerScript, there is a runtime error, because no defined variable called innerScript is visible here.
(It makes no difference whether the keyword its appears before the call to sayHowdy. This special treatment of handler calls will be discussed in "Handler Calls, Commands, and Script Objects," later in this chapter.)
Persistence of Top-Level Entities
Another remarkable feature of a top-level entity of a script object is that it is persistent, meaning that its value survives the execution of the script. If a top-level entity's value is changed during the execution of a script, then if you execute the script again, the top-level entity will have this new value at the outset, as long as you haven't done something to reinitialize it.
So, for example:
script myScript
property x : 5
end script
tell myScript
set its x to (its x) + 1
end tell
display dialog myScript's x
In a script editor application, run that code. A dialog appears saying 6. Now, for dramatic effect, without closing the script window or making any other change, walk away and do something else, such as get a cup of coffee. Now run the code again. The dialog says 7!
A script as a whole is a script object, so we can demonstrate the very same thing much more simply with a property of the script as a whole:
property x : 5
set x to x + 1
display dialog x
The first time you run this, the dialog says 6. Then when you run it again, the dialog says 7.
This amazing behavior is possible because AppleScript has a memory (see "Maintenance of State" in Chapter 3). Your compiled script is in AppleScript's memory. After the script is executed, AppleScript retains the compiled script, and along with it, the values of all its