Monday, June 26, 2017

#1 2005-10-13 02:49:22 pm

julifos
Administrator
From: MalasaƱa, Madrid
Registered: 2002-11-20
Posts: 2014

What is a "global"?

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.

Applescript:

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:

Applescript:

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:

Applescript:

script sample
   global x
   set x to 5 * 5
end script

run sample
sample's x

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.033 seconds, 8 queries executed ]

RSS (new topics) RSS (active topics)