DekGenius.com
[ Team LiB ] Previous Section Next Section

6.5 Blocks

A block is one or more lines of code demarcated from its surroundings as having a separate nature or purpose. A block is announced by a line stating what type of block it is; then comes the code of the block; and finally the block is terminated by a line starting with the keyword end. Blocks can occur within blocks.

It's very easy to spot a block in AppleScript code, because at compile time the code 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 or type; for example, I might say "an on block" or "a repeat block".

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 and to provide a further level of local scope. In AppleScript the scoping issue doesn't arise (as we shall see), 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
    [ Team LiB ] Previous Section Next Section