DekGenius.com
[ Team LiB ] Previous Section Next Section

10.4 It

The keyword it represents the target. This can be useful in helping you understand who the target is. It can also be useful as an explicit target, in situations where AppleScript would otherwise misinterpret your meaning. In situations where you would say of it after a word, you may say its before that word instead.

This example shows it used while debugging, to make sure we understand who the target is:

tell application "Finder"
        tell folders
                it -- every folder of application "Finder"
        end tell
end tell

We have already seen (Section 9.2.1) the need for it when accessing a script object's top-level entities within a tell block addressed to the script object. Without it, this code fails:

script myScript
        property x : 5
end script
tell myScript
        display dialog x -- error
end tell

There is no x in scope, so there's a runtime error. Similarly, if there were an x in scope, AppleScript would identify this x with that x, rather than with myScript's property x, unless we use it:

script myScript
        property x : 5
end script
set x to 10
tell myScript
        display dialog its x -- 5, but 10 if we omit its
end tell

When targeting an application, however, there is generally no need for it used in this way. That's because, unlike a script object, an application has a dictionary, so AppleScript knows when you're saying the name of a property of that application. For example, the Finder has a property home; there is no need for its to tell AppleScript that we mean the Finder's home rather than a variable in scope:

set home to "Ojai"
tell application "Finder"
        get home -- folder "mattneub" of folder "Users"...
end tell

In fact, here the problem is more likely to be how to refer to the variable home in the context of a tell block targeting the Finder. The next section (Section 10.5) discusses this.

However, it is needed when targeting an application with a tell block in order to distinguish a property from a class, when (as often happens) these have the same name (Section 10.6, later in this chapter). The preceding example didn't display this problem, because home is not the name of a class. But consider this example:

tell application "Finder"
        tell folder 1
                get container -- container, a class
        end tell
end tell

This was not the desired result. To get the container property of a folder, we must use its or (what amounts to the same thing) the of operator:

tell application "Finder"
        tell folder 1
                get its container -- folder "Desktop" of...
        end tell
        get container of folder 1 -- folder "Desktop" of...
end tell

Another typical use of it when targeting an application appears toward the end of Section 3.2.4. Here the find command requires an in parameter specifying a document, but we are already in the context of a tell block targeting that document. Since the in parameter is the same as the target, we can express it as it.

    [ Team LiB ] Previous Section Next Section