DekGenius.com
[ Team LiB ] Previous Section Next Section

9.1 Scoping of Script Objects

The way a script object is scoped differs depending on whether it is defined in a script object (including a script as a whole) or in a handler. A script object not defined in a handler is the base case, so we start with that.

A script object (not defined in a handler) is visible to code in the scope where it is defined, even if that code precedes the definition of the script object. For example:

run myScript -- Howdy
script myScript
        display dialog "Howdy"
end script

A script object (not defined in a handler) is visible to scopes within the scope where it is defined, but not before the script object is defined. Thus the downward effect of a script object definition is like the downward effect of a property declaration. For example, this works:

run myScript -- Howdy
script myOtherScript
        property x : "Howdy"
end script
script myScript
        display dialog myOtherScript's x
end script

But this doesn't:

run myScript
script myScript
        display dialog myOtherScript's x -- error
end script
script myOtherScript
        property x : "Howdy"
end script

A script object's visibility is thus confined by default to the scope where it is defined. But a script object defined in another script object is visible on demand wherever that surrounding script object is visible; we'll come to that in a moment (Section 9.2, later in this chapter).

9.1.1 Script Objects in Handlers

Within a handler, a script object definition must precede any reference to that script object. This is because until the handler actually runs, nothing within the handler definition happens—not even script object definitions. This rule is enforced by the compiler.

For example, this doesn't compile:

on myHandler(  )
        run myScript
        script myScript -- compile-time error
                display dialog "Howdy"
        end script
end myHandler
myHandler(  )

A script object defined in a handler can't be seen outside that handler. Again, this is because it doesn't exist except when the handler is executing.

A handler can return a script object, as we shall see later in this chapter.

9.1.2 Free Variables

Recall (Section 7.4.6) that an entity defined outside a handler or script object but globally visible and not redefined within it (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. In Section 7.4.6 our example of a free variable was a global variable. But it can also be a property, a handler, or a script object—in point of fact, any of the possible top-level entities of a script or script object.

Here's an example where the free variable is a property:

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

Here's an example where the free variable is the name of a handler:

run myScript -- Hello
set sayHello to sayGetLost
run myScript -- Get lost
on sayHello(  )
        display dialog "Hello"
end sayHello
on sayGetLost(  )
        display dialog "Get lost"
end sayGetLost
script myScript
        sayHello(  )
end script
    [ Team LiB ] Previous Section Next Section