Levels and Nesting

Looking 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:

A handler may be defined only at the top level of a script object (or of a script as a whole ).

This rule is enforced by the AppleScript compiler. So, this code won't compile:

repeat 3 times on sayHowdy( ) display dialog "howdy" ...

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.