Result

At runtime, every line of AppleScript code that actually executes an expression—that is, it isn’t blank, a comment, or mere flow control (looping and branching)—generates a result. This result is some sort of value; the particular value depends upon what the line does and what values it does it with.

The line need not be a “complete sentence”; any valid AppleScript expression constitutes a valid line, even if it does nothing (that is, even if it doesn’t have what a computer science person would call “side effects”). For example, this is a valid line of AppleScript code, and it has a value (can you guess what it is?):

5

A line’s result may be captured in two ways: explicitly or implicitly.

Explicit Result

The result of a line after it is executed may be captured explicitly by using the keyword result in the next line that is executed. For example:

5
display dialog result -- 5

One sees this technique used typically after fetching a value in a context of interapplication communication. For example, this is a fairly common style of coding:

tell application "Finder"
        get the name of every folder
end tell
set L to the result

Here’s another example:

tell application "Finder"
        count folders
end tell
set c to the result

The reason why people use this technique appears to be twofold. First, it may be a habit left over from HyperTalk, where this sort of thing was pretty much standard practice. Second, there seems to be a sense that a line is more legible and understandable if it consists of ...

Get AppleScript: The Definitive Guide 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.