Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [118]

By Root 1645 0
-- folder "Mannie" of desktop of application "Finder"

set x to contents of x

x -- folder "Mannie" of folder "Desktop" of folder "mattneub" of¬

folder "Users" of startup disk of application "Finder"

set x to contents of x

x -- folder "Mannie" of folder "Desktop" of folder "mattneub" of¬folder "Users" of startup disk of application "Finder"

The exact behavior here is entirely up to the target application, and doesn't have any particular significance. In each case you're just telling the application to do a get whose direct object is the very same "phrase" the application handed back to you previously as a reference. Whether the application returns the same phrase or a different phrase referring to the same object is entirely its own business.

Trouble with Contents


A problem arises when you're targeting an application whose dictionary defines a contents property for one of its object types. Applications shouldn't do this; it's bad behavior, because they're overlapping with a piece of AppleScript's own built-in vocabulary. In the context of a tell block directed at such an application, there is an ambiguity as to whether the word contents will be seen as the contents of operator or the application's contents property.

Warning

I'm told that the problematic nature of the contents property is actually an AppleScript bug. And AppleScript itself is to blame for taking the lead here; it defines a contents property for its selection-object class, luring application developers into doing the same sort of thing.

An example of such an offender is BBEdit. In BBEdit, when you ask for a text element such as a word, it gives you a reference rather than a string. That's good, because it's then possible to access that element in its context and do things to it. But then BBEdit does something bad: it defines the contents property as your way of obtaining the actual string. But it can be quite tricky to get AppleScript to ask BBEdit for the contents of something, because it often sees this as an attempt to dereference a reference instead.

So this works to obtain an actual string:

tell application "BBEdit"

set w to contents of word 4 of window 1

end tell

w -- "test"

But this doesn't:

tell application "BBEdit"

set w to contents of (get word 4 of window 1)

end tell

w -- characters 11 thru 14 of text document 1 of application "BBEdit"

And therefore neither does this:

tell application "BBEdit"

set x to word 4 of window 1

set w to contents of x

end tell

w -- characters 11 thru 14 of text document 1 of application "BBEdit"

And of course if you start by asking for a reference, things are even worse:

tell application "BBEdit"

set x to a reference to word 4 of window 1

set x to contents of x

set w to contents of x

end tell

w -- characters 11 thru 14 of text document 1 of application "BBEdit"

It looks as if, having gotten a reference to a word, you can never get from there to the actual text of that word. But there's a trick:

tell application "BBEdit"

set x to a reference to word 4 of window 1

get contents of text of x -- "test"

end tell

The proper behavior would have been for the application to define some other term for obtaining the contents of a thing. A typical approach is to use the term content instead. No confusion arises; AppleScript doesn't know that this is the singular of contents. Microsoft Word, Entourage, Apple Mail, and AppleScript Studio are examples of applications that do this.

Creating References to Variables


You can't make a reference to a local variable. Well, you can, but if you try to use it you'll get a mysterious error. For example:

local x

set x to {1, 2, 3}

set y to a reference to x

get item 1 of y -- error: Can't make item 1 of x into type reference

But a reference can itself be stored in a local variable:

local y

set x to {1, 2, 3}

set y to a reference to x

get item 1 of y -- 1

You can make a reference to anything that isn't a local, such as a global or a top-level entity:

script myScript

property x : 5

set y to a reference to x

set contents of

Return Main Page Previous Page Next Page

®Online Book Reader