Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [55]

By Root 1522 0
That's a simple example; for more complicated code, the chances increase that you may be mistaken about what result represents. Why risk this sort of mistake when in fact it is never necessary to use result in the first place?

Also, result is volatile. It changes after the execution of every expression. If you get into the bad habit of not capturing values when they are generated, because you intend to pick them up later using result, you are just asking for trouble. You might, in the course of further developing your code, insert a line between the line where the value is generated and the line where you pick it up as result; the value of result will then be different from what you expect. This kind of mistake is very difficult to debug. So I recommend that you not get into the habit of using result at all.

Implicit Result


The result of a line's execution is captured implicitly if it is the last line executed in a handler or script. This means that in theory there is no need to return a value explicitly (using the keyword return) from a handler or script. For example, instead of this:

on add(x, y)

return x + y

end add

display dialog add(1, 2)

it is possible to say this:

on add(x, y)

x + y

end add

display dialog add(1, 2)

This technique suffers from the same drawbacks as using result. The keyword return has two great advantages: you know exactly what you're returning (because that's the value of whatever follows the word return) and when you're returning it (because the handler exits the moment return is encountered). To rely on an implicit result is to know neither of these things. A line's result, as we've seen, may not be what you think it is. And the value returned by a handler or script is not the value of its physical last line, but rather the value of whatever line happens to be executed last; where there is flow control (loops and branches), you might not know what line this will be.

However, I do make use of the implicit result in one particular context—when developing and testing a script. If a script does not explicitly return a result (using return), a script editor application always displays the implicit result after execution. To check whether the script is working as expected, I specify the value whose implicit result I want to see:

on add(x, y)

x + y

end add

set z to add(1, 2)

z

The last line here causes the value of z to be displayed after the script executes; thus, I can check that z is taking on the expected value. The last line is a way of saying return z, only better. To see why, let's suppose you return the value of z explicitly, like this:

on add(x, y)

x + y

end add

set z to add(1, 2)

return z

You now proceed with developing the script, appending additional code after this snippet, and are very surprised when it doesn't work as expected. The reason is that you've accidentally left this line in the script:

return z

When that line is encountered, execution terminates (because that's part of what return means); the code that follows it is never executed. You will probably be confused as to what's gone wrong; in fact, you might continue developing your script, not realizing that anything has gone wrong, and imagining that the result shown by your script editor is the implicit result from a later line! By contrast, the nice thing about this expression:

z

is that it has no effect at all on the behavior of your script, even if further code is subsequently appended.

Comments


AppleScript permits two kinds of comment : single-line comments and delimited comments. Everything including and after two successive hyphens on a line is a single-line comment. For example:

set a to 1 -- this a comment on the same line as a command

-- this a comment on a line by itself

The comment delimiters are (* and *). Everything between comment delimiters is a comment. Such a comment may span multiple lines. This code contains three stretches of text that are legally commented out with comment delimiters:

set a to 1 (* because we feel like it;

tomorrow we may not feel

Return Main Page Previous Page Next Page

®Online Book Reader