Returned Value

When a handler is executed, it may return a value. This value is the handler's result .

Tip

The term result is used here in a technical sense. A handler may do other things besides return a result, and these other things may be quite significant in the world outside of the handler; but although these things may result from calling the handler, in a nontechnical sense, technically they are the handler's side effects, not its result. Thus, if you call a handler that erases your hard drive and returns the number 1, you might say, "The result of calling the handler was that my hard drive was erased," but technically you'd be wrong: the result was 1; the erasure of your hard drive was a side effect . (Clearly a side effect can be much more important than a result.)

When a handler is called, the result of executing the handler is essentially substituted as the value of the call that executed the handler.

For example:

on getRam( )
    set bytes to system attribute "ram "
    return bytes div (2 ^ 20)
end getRam

The handler getRam returns the amount of RAM installed on the user's machine, in megabytes. On my machine, it returns the number 1024. This means that a call to getRam( ) can presently be used wherever I would use the number 1024; in effect, it is the number 1024. For example:

on getRam( )
    set bytes to system attribute "ram "
    return bytes div (2 ^ 20)
end getRam
display dialog "You have " & getRam( ) & "MB of RAM. Wow!"

The call to getRam( ) in the last line behaves exactly as ...

Get AppleScript: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.