A global is a variable available to all the members of its script object. At the beginning of your script, you declare the global variable, and you assign it a value when you wish. Such value can be accessed by all members of the current script object: the main script (run) and all handlers.
The value of globals, as well as properties, is persistent after the script is run; and the scope is also the same. So, the main difference between both variables is that globals are defined at run time, against properties, which are defined at compile time.
global x
set x to 5 * 5
set y to 12
displayResult()
to displayResult()
display dialog x
end displayResult
We declare here the variable “x” as a global, and the handler “displayResult” can use such variable. However, if we use “display dialog y” (instead of “x”), we will get an error, since the handler can’t access to a local variable out of its own environment.
Here is a sample of the persistence of globals, posted at the MACSCRPT list:
global x
try
set x to x + 1
on error
set x to 0
end try
display dialog x
If you run this code several times you will see how the variable “x” is increased: 0, 1, 2…
Of course, we can access globals owned by other script objetcs, but we must ask the related script object to retrieve its value:
script sample
global x
set x to 5 * 5
end script
run sample
sample's x