11.5. Get
The default command is get. In effect, a sentence or clause with no verb is assumed to have get as its verb. So, for example:
tell application "Finder"
name of folder 1
end tell
The get command is supplied here and is the actual message sent to the Finder. It's exactly as if you had said get explicitly:
tell application "Finder"
get name of folder 1
end tell
One even sees code written like this:
tell application "Finder" to name of folder 1
AppleScript can also supply get in the middle of a line where needed. As we have already seen, this code:
tell application "Finder"
set oldname to name of folder 1
end tell
is actually treated by AppleScript as if it said this:
tell application "Finder"
set oldname to (get name of folder 1)
end tell
Do not imagine, however, that it makes no difference whether you ever say get, and that you can blithely omit it. On the contrary, it's probably better to err in the other direction and say get whenever you mean get. There are no prizes for obfuscated AppleScript, and you're most likely to confuse yourself (and impress no one else) if you get into bad habits. More important, omission of get from expressions of any complexity can cause runtime errors. For example, this:
tell application "Finder" to display dialog (name of folder 1)
-- error: Finder got an error: Can't make name of folder 1 into type string
is not the same as this:
tell application "Finder" to display dialog (get name of folder 1) -- Mannie
In the first example, name of folder 1 is a reference to an attribute; that's not something that can be displayed by display dialog, so we get an error. In the second, the get command fetches the value of that attribute, a string, and all is well. AppleScript programmers like to say that get resolves references
.
Sometimes, just the other way around, you don't want references resolved. This works (the first word of the document is deleted):
tell application "TextEdit" to delete word 1 of document 1
But this doesn't work (there is no error, but nothing happens):
tell
application "TextEdit" to delete (get word 1 of document 1)
In the second case, get resolves the reference, effectively evaluating the expression and yielding a string (the text of the first word). Let's say it's the word "This." Then we're telling TextEdit to delete the string "This"not as an attribute of some document but in the abstract. That's a meaningless command. (The fact that TextEdit doesn't complain should probably be regarded as a bug.)
As mentioned earlier, get (along with set) is implemented in a special way; it isn't a message to an object in the manner discussed through this chapter. Thus it can't be sent to a direct object by means of a tell block; you must always say what you want to get, immediately after the word get, or the compiler will complain:
tell 7 to get -- compile-time error: Expected expression but found end of script
|