Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [110]

By Root 1523 0
be. For example, in Entourage a message's name can be changed, and its index within its folder may change, but its ID is constant.

To specify an element by ID, say the class followed by the keyword id followed by the ID value. This value will have been obtained at some earlier point, typically by asking for an element's ID property:

tell application "Microsoft Entourage"

set messageID to ID of message 1 of in box folder -- 2849, if you must know

-- more code goes here...

get message id messageID

end tell

Some


A random element may be specified by saying some followed by the class:

tell application "Finder"

name of some disk -- "gromit"

name of some disk -- "feathers"

name of some disk -- "feathers"

end tell

Every


It may be possible to get a list of every element of a class. To ask for such a list, say the keyword every followed by the class; alternatively, you may be able to say just the plural of the class:

tell application "Finder" to get every disk

tell application "Finder" to get disks

If asking for just one element would result in a reference, the result in this case is a list of references.

Range


Elements may be ordered, and you may be able to obtain a list of contiguous elements (a range ) by giving the first and last index number you're interested in. It is generally not important in what order you give these index numbers.

To specify elements by range, say the class in a plural form (or every and the class) followed by an index number, the keyword thru (or through), and another index number. You can say beginning or end instead of an index number:

get words 1 thru 4 of "now is the winter of our discontent"

get words beginning thru 4 of "now is the winter of our discontent"

Alternatively, you may be able to get a list of contiguous elements of a class where the range is marked off by two element specifiers for some other class. In this case, you say the class in a plural form (or every and the class) followed by the keyword from, an element specifier for the starting point, the keyword to, and an element specifier for the ending point. Again, you can say beginning or end instead of an element specifier:

get words from character 12 to character 17 of "now is the winter"

get words from character 12 to character -1 of "now is the winter"

get words from character 12 to end of "now is the winter"

There is a tendency to confuse or conflate these two forms, and to try to say something like this:

get words 1 to 3 of "now is the winter of our discontent" -- compile-time error

You can't do that. "To" is not "thru"! Keep these two constructions straight. Practice them before going to bed.

Relative


Elements may be ordered, and it may be possible to refer to an element as the successor or predecessor of another element. To ask for an element in this way, say the name of the class, the keyword before or after, and an element specifier:

tell application "TextEdit"

tell document 1

get word after word 1

end tell

end tell

A synonym for before is in front of. Synonyms for after are behind and in back of.

That wasn't a very useful example, because we could have asked for word 2. But relative specifiers are useful when you can obtain an object of one class in positional relation to another. For example, in BBEdit all text has insertion point elements lying between the characters. Thus you can say this:

tell application "BBEdit"

tell text of window 1

get insertion point before word 4

end tell

end tell

Given an insertion point in BBEdit, you can set its contents property to alter the text. This code changes "This is a test" to "This is a great test":

tell application "BBEdit"

tell text of window 1

set pt to insertion point before word 4

set contents of pt to "great "

end tell

end tell

Similarly, some applications have a class called location reference (or "insertion location ," depending on how the dictionary is displayed). Unlike BBEdit's insertion point, you usually can't get one directly; instead, you form one, primarily as the at or to parameter

Return Main Page Previous Page Next Page

®Online Book Reader