AppleScript_ The Definitive Guide - Matt Neuburg [59]
Tip
What is the cumbersome word "definition" doing in my explanation of an on block and a script block? Strictly speaking, there is a real distinction to be drawn here. A handler or a script object is some code; a handler definition or a script object definition is a block stating what that code is. This distinction will become important later in the book, when it turns out that a script object or a handler is really just a value like any other value (like the number 5 or the string "howdy"), whereas the block is really just a way (and not the only way) of creating and giving a name to that value. Nevertheless, the bare terms "handler" and "script object" (without "definition") are very often used loosely and conveniently to mean the blocks , and I will use them this way too where there is no danger of confusion.
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"
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.
The Top Level
The top level of all is the script as a whole. (Okay, I lied. There are actually some secret levels even higher than that. But just pretend, for now, that there aren't.) What's more, the script as a whole is itself a script object, even though it has no name and is not defined by a block.
This strange-sounding fact is actually extremely cool. It gives your script a sort of overall uniformity. For example, a handler definition can't occur anywhere except at the top level of a script object. So why is it possible for this to be a script?
on sayHowdy( )
display dialog "howdy"
end sayHowdy
sayHowdy( )
There's a handler definition sitting there, not nested in a script block. But that doesn't break the rule; it accords with it, because the script as a whole is itself a script object, so this handler definition is in fact at the top level of a script object, precisely where it should be.