Scope of Top-Level Entities

A top-level entity is visible in the scope where it is defined, and at all deeper levels nested in that scope subsequent to where it is defined.

Let's take, for example, a script property. The script property x here, declared at the top level of the script as a whole, is also in scope inside a script object and a handler:

property x : 10
script myScript
    display dialog x
end script
on myHandler( )
    display dialog x
end myHandler
run myScript -- 10
myHandler( ) --10

If a top-level entity is in scope in a script object, a script object nested at a deeper level may declare a top-level entity with the same name. This deeper name will overshadow the first entity's name within that deeper scope. Thus in this example there is never the slightest ambiguity as to what is meant by x at any point:

property x : 5
script scriptOne
    property x : 10
    script scriptTwo
        property x : 20
        display dialog x
    end script
    display dialog x
    run scriptTwo
end script
script scriptThree
    property x : 30
    display dialog x
end script
script scriptFour
    display dialog x
end script
display dialog x -- 5
run scriptOne -- 10, 20
run scriptThree -- 30
run scriptFour --5

Regions of scope outside a script object cannot see that script object's top-level entities. But if they can see a name referring to that script object, they can ask to access that script's top-level entities, as explained in "Top-Level Entities" in Chapter 8. In the same way, a region of scope at a deeper level can access an overshadowed ...

Get AppleScript: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.