6.2. Levels and NestingLooking at how blocks are indented, we can think of them as arranged in levels. For example, in this code the if block is one level down from the on block (the handler definition), and the repeat block is two levels down from the on block: on myHandler( ) if weekday of (get current date) is Monday then repeat 3 times display dialog "Howdy" end repeat end if end myHandler Code that occurs directly inside a block may be said to be at the top level of that block. In the preceding code, the if block is at the top level of the handler. Of course, what's at the top level of a block needn't be another block. So in the preceding code, the display dialog command is at the top level of the repeat block. But when a block is at the top level of another block, we can describe the two blocks in relation to one another as nested. So in the preceding code, the repeat block is nested in the if block, and the if block is nested in the handler. Most blocks can just be nested in one another indiscriminately. But the rules for nesting handler and script object definitions are special (and this is part of what makes them special types of block, and why they are the subject of this chapter). In particular, there's a special rule for where a handler definition can appear:
This rule is enforced by the AppleScript compiler. So, this code won't compile: repeat 3 times on sayHowdy( ) display dialog "howdy" end sayHowdy end repeat -- compile-time error: Expected "end" but found "on" The rule also means, obviously, that you can't nest a handler directly within a handler. This code won't compile: on outer( ) on sayHowdy( ) display dialog "howdy" end sayHowdy end outer -- compile-time error: Expected "end" but found "on" But there's a way out of these limitations. A script object definition can appear anywhere. So to define a handler in a handler, define it in a script object in a handler: on outer( ) script s on sayHowdy( ) display dialog "howdy" end sayHowdy end script end outer We'll use this trick in some powerful ways later in the book. |