Re: Unwinding the containment hierarchy of a reference
Re: Unwinding the containment hierarchy of a reference
- Subject: Re: Unwinding the containment hierarchy of a reference
- From: Paul Berkowitz <email@hidden>
- Date: Tue, 07 Dec 2004 13:35:50 -0800
On 12/7/04 12:44 PM, "Scott Babcock" <email@hidden> wrote:
> Given an arbitrary AppleScript object reference, is there a reasonable
> way to determine the object's containment hierarchy? For example:
>
> tell application "TextEdit"
> set x to a reference to word 12 of document 2
> end tell
>
> Is there a programmatic way to determine that word 12 is contained by
> document 2, which is contained by application "TextEdit"?
No. For one thing, the same object can be an element of more than one parent
object. And even then it only works if the child object has a property
defined for that parent, which doesn't happen often.
Here's one that does: Entourage defines a 'storage' property of a message,
which gives you the folder it's in. Without that storage property, you
couldn't find out without checking every single folder in the hierarchy for
that message. And folder has a 'parent' property, which gives you a
subfolder's parent folder, and a top IMAP folder's IMAP account, and a
top-level local folder gives the application (or it's supposed to: actually
it errors, but name of parent' gets you "Microsoft Entourage"). But there's
no way to discover which group(s) a particular contact is in, since
'contact' no 'group' property (it does so in Apple's Address Book however,
so you can do it there), without checking through every group in a repeat
loop. In some cases a 'whose' clause can do the job.
Here's another example, which you might recognize. In Microsoft Word. 'word'
is an element of document, sentence, and text range, among other classes. So
any particular word has several different containers: its document, its
sentence, and umpteen possible text ranges. There can be no single
'container'. What would be nice might be if it had a sentence property -
returning its sentence - and a document property - returning its document.
As it is you'd have to go through every sentence and every document looking
for that word (or its content, if that's what you want), checking. 'cell'
does have a 'row' property and a 'column' property: that's the only case I
know of offhand where you can find the container(s) of an object in Word.
It's also about the only situation I can think of where you'd really need
to.
In the example you gave 'word 12' has no meaning outside of 'word 12 of
document 2' [of the application] - therefore you know by definition it
exists. Or, as Michelle said, if you're checking to see whether there is
indeed a 12th word of a 2nd document, just check for it existence.
If you get hold of some object via selection, the application already knows
which objects you have and should tell you. TextEdit is rather feeble, but
Word should be able to get you the text object of the selection, and ought
to know which words of which paragraphs are selected. But maybe it can't. In
Entourage, selecting a set of contacts or calendar events in a custom view,
or even a mixture of both, returns a list specifying precisely which objects
are in the list {contact id 123, event id 345}. But this is a simple object
model where all these items are elements of the application. It's always
gong to be very complicated in a model where a word cab be an element of its
sentence, of the text object of its paragraph, of its document. etc.
So, no, it doesn't - and can't possibly - come "free" in AppleScript. The
developers have o implement it by specifying a single one-one property that
returns what you want. I think it would be reasonable for 'word' and the
other classes in the Text Suite to have a 'document object' property that
returned the document. 'paragraph' should have a similar 'document object'
property too. The result should probably include the index of the document
in the application. (document 3, e.g.). You can always get its name and
other properties form that.
--
Paul Berkowitz
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden