Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [60]

By Root 1435 0

Code and the Run Handler


So far, we've talked about blocks, and in particular about script object and handler definitions, as the constituents of a script. But of course there's more to a script than that: there's the actual code, the commands, the stuff that does something. From a visual point of view, code can appear anywhere. But wherever code appears, if you work your way up the levels of nesting, sooner or later you'll always come to either a handler or a script object. That's what the code is really inside of.

For example:

set x to 1

on outer( )

set xx to 1

script s

set xxx to 1

on sayHowdy( )

display dialog "howdy"

end sayHowdy

set xxxx to 1

end script

set xxxxx to 1

end outer

set xxxxxx to 1

That script is exactly the same as an earlier example, except that I've added code lines, so that given the structure of the script (a handler in a script object in a handler), at least one line of code appears in every place where code can possibly appear. All the lines of code are set commands, except for the display dialog command in the middle. So now consider the line "set xx to 1." Where is it, structurally? It's inside the handler outer. You can do this for every line of code.

This little game is significant because the regions demarcated by the handler and script object definitions are also regions of scope. The line "set xx to 1" talks about a variable called xx. Because of how and where this line occurs, there are strict rules about what parts of the script can and can't see this variable xx. (Those are the rules I'll talk about in Chapter 10.)

Now, here's a curious fact. Strictly speaking, code doesn't occur just anywhere. Code can occur only in a handler. Once again, this may sound weird, but it is actually very elegant, because it makes the structure of a script nice and uniform. There's just one problem: it looks like I must be lying. Surely the first line of the script, "set x to 1," is not in a handler, right? It's in a script object, because it's at the top level of the script and the script as a whole is itself a script object . Similarly, the line "set xxx to 1" is at the top level of a script object, and not in a handler. Right?

Wrong. It turns out that there's a secret kind of handler you can't see. The rule is that every script object has a handler called run. This special handler is called the script object's run handler . If you like, you can actually express this run handler explicitly. The script object is then said to have an explicit run handler .

Here is a script with an explicit run handler:

on run

display dialog "howdy"

end run

You will observe that the on run statement lacks any parentheses, unlike other handler definitions we've seen. The run handler is special and doesn't use parentheses.

If a script object has no explicit run handler, then it has an implicit run handler . Here is a script with an implicit run handler:

display dialog "howdy"

Those two scripts are almost exactly the same. They are both scripts with a run handler containing exactly one line of code (the display dialog command). They both do exactly the same thing. The difference is that in one case the run handler is explicit, and in the other the run handler is implicit.

When a script (or script object) runs, what runs is its run handler. This will be much more significant to you when I talk more about script objects, but for now just concentrate on your script as a whole. When you execute a script, whether by pressing the Run button in a script window in a script editor application, or by giving a compiled script file to a script runner and calling the script through the interface, or whatever, what runs is that script's top-level run handler, whether implicit or explicit. The two previous scripts with display dialog do exactly the same thing, whether the run handler is implicit or explicit.

A script object (including a script as a whole) has exactly one run handler. Therefore it cannot have both an explicit and an implicit run handler. That would be ridiculous. It would also be illegal. This

Return Main Page Previous Page Next Page

®Online Book Reader