AppleScript_ The Definitive Guide - Matt Neuburg [57]
It's very easy to spot a block in AppleScript code, because in decompiled code its lines are indented from the announcement line and the termination line. For example:
myHandler( )
on myHandler( )
repeat 3 times
display dialog "Howdy"
end repeat
end myHandler
That code contains two blocks. One is announced with the on myHandler line, and is terminated by the end myHandler line; everything in between them is the code of that block. That code consists of another block, announced with the repeat line and terminated by the end repeat line; the line of code in between them is the code of that block.
In this book I frequently refer to such blocks by their announcement keyword; for example, I might say "a repeat block."
Some blocks (just two, actually—tell blocks and if blocks) have single-line variants. This permits some rather twisted condensed syntax. For example:
tell application "Finder"
if exists folder "Mannie" then
reveal folder "Mannie"
end if
end tell
You can reduce one or both of those blocks to a single line. So, this is legal (but I never talk this way, and I don't recommend you do either):
tell application "Finder" to if exists folder "Mannie" then
reveal folder "Mannie"
end if
Tip
When typing a block, don't bother to type the name of the block a second time in the end line. Just type end for that line; the compiler will fill in the name of the block. (For example, don't type end repeat; just type end, on a line by itself, and the compiler will see that this corresponds to a preceding repeat line and will fill in the full end repeat for you.) This is not just a time-saving device; it's also a way to ensure that your blocks are structured correctly.
The only blocks you can make in AppleScript are those for which keywords are supplied; you cannot indent arbitrarily for clarity, as you can in UserTalk or C. So, for example, in UserTalk you can say this:
local (x)
bundle
x = 4
msg (x)
The keyword bundle here does nothing except to allow some code to be indented for clarity. It also provides a further level of local scope. In AppleScript the scoping issue doesn't arise, but a way of indenting for clarity might still be nice. To achieve it you would need to misuse an existing block type. For example:
local x
repeat 1 times
set x to 4
end repeat
display dialog x
The
AppleScript allows you to use the word the before almost anything. This is pure syntactic sugar, and I never use it. For example, this is perfectly legal:
set the x to the 9
display dialog the (get the the the the x + the 1)
Now, really.
Chapter 6. A Map of the World
Every AppleScript program is a script. This chapter describes the structure of a script. A script is composed of certain definite building blocks, and they go together in a certain way. The next several chapters will return to each of these building blocks individually and explain them in full detail, but first we need an introduction to the entire cast of characters that constitute a script. That's what this chapter is for. It provides the crucial overall map of a script's world, so that in our explorations during subsequent chapters you'll know where we're going and how the geography fits together.
Scope Blocks
Recall (from "Blocks" in Chapter 5) that a pretty-printed script displays its structure by means of indentation. Every set of indented lines is introduced by an on line and terminated by a corresponding end line. The whole thing is a block . For example, this is a repeat block:
repeat 3 times
display dialog "Howdy"
end repeat
It turns out that two types of block are very special in AppleScript: a script object definition and a handler definition. They are special in many ways. These are the only blocks that function