Section 10.1. Regions of Scope


10.1. Regions of Scope

Every AppleScript program has regions of scope . Some regions of scope may be inside others, but they do not partially intersectgiven 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 errorsee Chapter 7).




AppleScript. The Definitive Guide
AppleScript: The Definitive Guide, 2nd Edition
ISBN: 0596102119
EAN: 2147483647
Year: 2006
Pages: 267
Authors: Matt Neuburg

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net