AppleScript_ The Definitive Guide - Matt Neuburg [104]
Thus there is a sense in which tell and of are interchangeable. In determining the target, AppleScript actually works its way up the chain of ofs and tells until it assembles a complete target . (I am deliberately waving my hands over what I mean by "a complete target," but it means something like an application, a script, or a value within your script.)
Whether you use of or tell on any particular occasion will depend on various circumstances. Sometimes it's just a matter of style and legibility, but sometimes you'll be constrained by other factors. Suppose you want to open file 1 of folder 1, and then file 1 of folder 2. And suppose your code starts this way:
tell application "Finder"
tell folder 1
From within this nested tell block, there is absolutely no way to speak of folder 2. You could use the words "folder 2," but you're already inside a tell block targeting folder 1, so this would mean "folder 2 of folder 1," which is not what you want. There isn't any notation that backs you out one level (like .. in a POSIX pathname). You must actually be one level higher. An experienced AppleScript programmer will plan ahead and not nest tell blocks too deeply:
tell application "Finder"
open file 1 of folder 1
open file 1 of folder 2
end tell
Another solution is, as you're working your way down the nest of tell blocks, to capture a reference to something you're going to need later at a level too deep to refer to it. For example:
tell application "Microsoft Entourage"
tell in box folder
move message 1 to folder "temp"
-- error: Microsoft Entourage got an error: Can't get message 1 of in box folder
end tell
end tell
Despite the very misleading error message, the actual problem here is that there is no such thing as folder "temp" of in box folder—folder "temp" is a top-level folder. One way out would of course be not to dive into a tell block:
tell application "Microsoft Entourage"
move message 1 of in box folder to folder "temp"
end tell
But in a bigger script or with a long chain of ofs, we might not like that solution. So we'll capture a reference to folder "temp" at a point where we can speak of it, and use that reference later:
tell application "Microsoft Entourage"
set ftemp to folder "temp"
tell in box folder
move message 1 to ftemp
end tell
end tell
Terms in Scope
The rules for targeting do not override the scoping rules described in the previous chapter. A tell block does not cut off access to the surrounding context—and a good thing too, because if it did, you wouldn't be able to use terms that are in scope while explicitly targeting something. AppleScript does not blindly send messages to the target; it looks at the target's terminology, but it also looks to see whether you might be using a term that's in scope from your script. You can therefore quite freely mingle terms defined in the current context of your script with terms defined by the target.
(The mechanism is actually quite subtle, and if you press up against its limits, conflicts can arise. I'll give some examples in this chapter, and there is further detail in the section "Resolution of Terminology" in Chapter 20.)
For example:
set x to "howdy"
tell application "Finder"
count x
end tell
AppleScript knows that x is something meaningful in the context of the script itself, so it doesn't send any message to the Finder asking about x. (That's a good thing, because the Finder doesn't know about anything called x.)
Here's an example with considerable mingling of terms from the two namespaces, yours and the Finder's:
set newname to "someFolder"
tell application "Finder"
set oldname to name of folder 1
set name of folder 1 to newname
end tell
display dialog oldname
Here, oldname and newname are (implicit) globals within the script, and their values are set and retrieved without involving the Finder. To accomplish this, AppleScript must unravel your phrases into separate commands. Consider this line:
set oldname to name of folder 1
AppleScript actually does two things here. First it sends to the Finder