AppleScript_ The Definitive Guide - Matt Neuburg [89]
on newCounter( )
script aCounter
property c : 0
on increment( )
set c to c + 1
end increment
end script
return aCounter
end newCounter
-- and here's how to use it
set counter1 to newCounter( )
counter1's increment( )
counter1's increment( )
counter1's increment( )
set counter2 to newCounter( )
counter2's increment( )
counter1's increment( )
display dialog counter1's c -- 4
display dialog counter2's c --1
Chapter 10. Scope
By scope is meant the ability of one region of code to see variables in another region of code. The rules of scope for AppleScript are remarkably involved—as involved of those of any computer language I know. They are a perennial source of pitfalls for beginning and experienced programmers alike. If you don't understand the rules of scope, or (even worse) if you try to ignore them, you'll find your scripts mysteriously going wrong in confusing ways. This chapter discusses the rules of scope, along with a powerful advanced scope-related feature of AppleScript—closures.
Regions of Scope
Every AppleScript program has regions of scope . Some regions of scope may be inside others, but they do not partially intersect—given two regions, either one is entirely inside the other or they are completely distinct. In other words, the regions of scope are nested. The top level of the script is a script object and is a region of scope . Any other regions of scope are inside this, and are created by the presence of scope blocks: handler definitions and script object definitions. (See Chapter 6.)
In Example 10-1, I've sketched a sample script with scope blocks nested in various combinations. At every point where code can go, I've put a comment distinguishing that region of scope by number. So, scope 1 is the top level of the script itself (its implicit run handler); scope 2 is the code within handlerOne, a handler defined at the top level of the script; scope 3 is the code within scriptOne, a script object defined within handlerOne; and so on.
Example 10-1. Regions of scope (continued)
-- scope 1
on handlerOne( )
-- scope 2
script scriptOne
-- scope
3
end script
-- scope 2
end handlerOne
-- scope 1
script scriptTwo
-- scope 5
on handlerTwo( )
-- scope 6
end handlerTwo
-- scope 5
script scriptThree
-- scope 7
end script
-- scope 5
end script
--scope 1
The region in which a variable is visible is called its scope. A variable that is visible at a certain point is said to be in scope at that point. To understand scope is to know what kinds of variable can belong to a given region of scope and in what other regions each variable is in scope.
Note that visibility is visibility and access is access. If code can see a variable in any manner, it can access it, which means it can both get and set that variable. In the examples in this chapter I will mostly use getting rather than setting, because that's the simplest way to detect when code can't see a variable (the attempt to fetch the value of an undefined variable results in a runtime error—see Chapter 7).
Kinds of Variable
With regard to scope, we must distinguish four kinds of variable:
Top-level entities
Top-level entities are script properties declared, and script objects and handlers defined, at the top level of a script or script object. (See "Top-Level Entities" in Chapter 8.)
Explicit locals
An explicit local is a variable announced by a local declaration , which looks like this:
local x
Explicit globals
An explicit global is a variable announced by a global declaration, which looks like this:
global x
Undeclared variables
A undeclared variable is none of the above. It's just a name that your code suddenly starts using. This is legal, but when you do it, AppleScript assigns the variable a scope , in ways that may surprise you.
The scope of a variable, and what kind of variable it is (these amount to the same thing), are determined at compile time .
Scope of Top-Level Entities
A top-level