Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [91]

By Root 1394 0
myScript

myHandler -- error: The variable myHandler is not defined

end script

on myHandler( )

display dialog "Howdy"

end myHandler

Note that a script object defined not at the top level of a script object (or script) is not a top-level entity. Basically, it's an implicit local (see "Scope of Undeclared Variables," later in this chapter), which means that such a definition must precede any reference to itself, and that it can't be seen outside the scope where it is defined. Interestingly, when you define a script object at the top level of a handler, the rule about the definition preceding a reference to itself is enforced by the compiler (with a mysterious error message):

on myHandler( )

run myScript

script myScript -- compile-time error: The myScript property is specified more

than once

display dialog "Howdy"

end script

end myHandler

myHandler( )

Scope of Locals


An explicit local is a variable whose name appears in a local declaration . A local declaration can declare several variables at once, delimited by comma:

local a

local b, c, d

A local variable is visible only within the very same region of scope where it is declared—not outside it, and not at a deeper level. (But there's an exception, which we'll come to in a moment.)

Local variables completely disambiguate a name within their own scope, and local variables in other scopes can have the same name without conflict. Suppose a script object starts like this:

script myScript

local x

The local declaration for x means that from now on when code in this script object's scope says x it means this local x and no other. What's more, other scopes may declare their own local x, they may declare a global x, they may bang the floor and have a temper tantrum, but they absolutely will not be able to have any effect upon myScript's x, nor will anything myScript does with its x have any effect upon them.

Here's an example of a local variable in action:

local x

set x to 5

script myScript

display dialog x -- error: The variable x is not defined

end script

run myScript

Observe how completely different this is from what would have happened if x had been a top-level property. Here, there is a variable called x and it is defined, but it is declared local and therefore is visible only within its own scope. That scope is the top-level script. The display dialog x command is in a different scope, that of the script object myScript. Therefore AppleScript takes this to be a different x, and this different x has never been assigned a value. (I'll explain later just what x AppleScript takes the x of display dialog x to be.)

A local declaration overshadows the visibility of a top-level entity from a higher level:

property x : 5

script myScript

local x

display dialog x -- error: The variable x is not defined

end script

run myScript

But this overshadowing affects only the scope of the local declaration—not a deeper scope:

property x : 5

script myScript

local x

on myHandler( )

set x to 10

end myHandler

myHandler( )

set x to 20

end script

run myScript

display dialog x -- 10(not 20)

The dialog displays 10, not 20. Even though myScript overshadows the top-level property x with a local x declaration, this has no effect on myHandler, which still sees the top-level property x. (This makes sense, because it can't see myScript's local x.) When myHandler sets x to 10, that is the same x as at the top level. When myScript sets x to 20, that's its local x, and the value displayed in the last line is unaffected.

In a handler, the variable names used for parameters in the definition of the handler are local within the handler. Naturally, a handler may also declare further locals:

on myHandler(what)

local x

set x to 7

end myHandler

myHandler("howdy")

display dialog what -- error: The variable what is not defined

display dialog x --error: The variable x is not defined

Now we come to the great exception: a script object defined in a handler can see the handler's local variables. To a script object in a handler, the handler's locals

Return Main Page Previous Page Next Page

®Online Book Reader