Previous Page
Next Page

10.8. Free Variables

A variable defined outside a handler or script object, but globally visible within it, and not overshadowed by a declaration of the same name, is called a free variable with respect to that handler or script object. A free variable in AppleScript is identified with a top-level entity or global through the downward effect of its declaration. For example:

global x, y, z
script myScript
    property x : 1
    local y
    yy
    z
end script

Within myScript, x is explicitly defined as a property and y is explicitly defined as a local, so neither is a free variable. The variable yy isn't explicitly defined within myScript, but it isn't defined outside it either, so it is an implicit local and not a free variable. But the variable z is globally visible within myScript (from the global declaration at the start of the code), and the name is not redeclared within myScript, so z within myScript is a free variable, and is identified with the global z declared in the first line.

A free variable takes its value within the handler or script object at the time the code runs, not at the time the handler or script object is defined. For example:

global x
set x to 5
on myHandler( )
    display dialog x -- x is a free variable
end myHandler
set x to 10
myHandler( ) -- 10 (not 5)

The dialog displays 10, not 5. It doesn't matter that x had been set to 5 when myHandler was defined; it only matters what its value is when the code inside myHandler actually runs. By that time, x has been set to 10.

Free variables' values are determined at runtime; but the identification of the fact that a variable is a free variable, and its association with some particular globally visible variable, happens during compilation. This way of resolving the meaning of free variable names is called lexical scoping .

So, in the previous example, it is important that x is declared global before myHandler is defined. If you move the global x declaration to after the myHandler definition, the script generates an error at runtime. Even if x is declared global before (temporally) myHandler runs, that's not good enough. We must declare x global before (physically) myHandler speaks of it; otherwise, myHandler's x isn't the global x, and when the code runs, myHandler's x isn't defined. This is another reason why you should declare your variables, and not only declare them but declare them early, even though AppleScript doesn't require you to do so.

Here's an example with two top-level entities as free variables. One is a property, the other is a script object:

property x : 5
script myScript
    display dialog x -- x is a free variable
end script
script myOtherScript
    set x to 20 -- x is a free variable
    run myScript -- myScript is a free variable
end script
set x to 10
run myScript -- 10
run myOtherScript -- 20


Previous Page
Next Page