Script Properties
A script property (often just called a property) is a script-level global variable with initialization. A script property must be declared, and an initial value must be supplied as part of the declaration. The syntax is:
propertypropertyName
:initialValue
For example:
property x : 5
The abbreviation for
property
is
prop
.
A property declaration can appear only at top level or at the top level of a script object. For example:
property x : 5 script myScript property y : 10 -- other stuff end script -- other stuff
A property is a variable, so its value can be set and fetched in the normal way. For example:
property x : 10 display dialog x -- 10 set x to 5 display dialog x -- 5
Scoping of Properties
A property is a kind of global variable, and a property declaration has the same downward effect as a global declaration:
property x : 10 script myScript display dialog x end script on myHandler( ) display dialog x end myHandler run myScript -- 10 myHandler( ) -- 10
Both myScript
and myHandler
can
see the property x
, because the property
declaration works like a global declaration with respect to its
downward effects.
The big difference between a global variable and a script property is in the upward effect of their declaration. A property’s scope is confined to the script object where it is declared. The property is automatically visible downwards, as if the property declaration had been a global declaration; but it is not automatically visible anywhere else. Different script ...
Get AppleScript: The Definitive Guide 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.