12.6. 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.
| 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.
|