9. Reference Forms
Property reference
A property either contains a simple value or represents a one-to-one relationship between two application objects. Properties either describe the application object (its name, class, size, color, preferences, etc.) or provide a convenient reference to other object(s) of interest to users (e.g. home, currentTrack).
Syntax:
[reference property]
Examples:
[textedit name]
[[[textedit documents] at: 1] text]
[[[finder home] files] name]
Element references
Elements represent a one-to-many relationship between application objects. Elements usually reflect the containment structure of the application object model and generally provide multiple ways of referencing some or all of those objects (e.g. characters/words/paragraphs of documents by index/relative-position/range/filter).
All Elements
Syntax:
[reference elements]
Examples:
[[finder home] folders]
[textedit windows]
[[[textedit documents] paragraphs] words]
by Name
Syntax:
[elements byName: selector]
selector = NSString -- name of object (as matches its 'name' property)
Examples:
[disks byName: @"Macintosh HD"]
[files byName: @"index.html"]
Note: applications usually treat object names as case-insensitive. Where multiple element have the same name, a by-name reference only identifies the first element found with that name. (Tip: to identify all elements with a particular name, use a by-test reference instead.)
by Index
Syntax:
[elements byIndex: selector]
selector = NSNumber -- index of object
[elements at: selector]
selector = int -- index of object
Examples:
[words byIndex: [NSNumber numberWithInt: 3]]
[items at: -1]
Note: elements are one-indexed (AEM-style indexing), not zero-indexed (Ruby-style indexing).
Note that while element indexes are usually integers, some applications may accept other types (e.g. Finder accepts aliases in filesystem references).
by ID
Syntax:
[elements byID: selector]
selector = anything -- object's id
Examples:
[windows byID: 4321]
by Absolute Position
Syntax:
[elements first] -- first element
[elements middle] -- middle element
[elements last] -- last element
[elements any] -- random element
Examples:
[documents first]
[paragraphs last]
[files any]
by Relative Position
Syntax:
[element previous: className] -- nearest element of a given class to appear
before the specified element
[element next: className] -- nearest element of a given class to appear
after the specified element
className = ASConstant -- class of element
(see Classes and Enumerated Types)
Examples:
[[words at: 3] next: [TEConstant word]]
[[paragraphs at: -1] previous: [TEConstant character]]
by Range
Range references select all elements between and including two references indicating the start and end of the range. The start and end references are normally declared relative to the container of the elements being selected. These references are constructed using a glue-defined 'Con' macro, e.g. TECon, as their root. For example, to indicate the third paragraph of the currrent container object:
[[TECon paragraphs] at: 3]
For convenience, the -byRange: method also allows start and end references to be written in shorthand form where their element class is the same as the elements being selected; thus:
[[ref paragraphs] byRange: [[TECon paragraphs] at: 3]
[[TECon paragraphs] at: -1]]
can also be written as:
[[ref paragraphs] byRange: [[NSNumber numberWithInt: 3]
to: [[NSNumber numberWithInt: -1]]
Where start and end points are both indexes (the most common form), this can be shortened even further by using the -at:to: convenience method:
[[ref paragraphs] at: 3 to: -1]
Some applications can handle more complex range references. For example, the following will work in Tex-Edit Plus:
[[ref words] byRange: [[TEPCon characters] at: 5]
to: [[TEPCon paragraphs] at: -2]]
Syntax:
[elements byRange: start to: end]
start = NSNumber | NSString | ASReference -- start of range
end = NSNumber | NSString | ASReference -- end of range
[elements at: start to: end]
start = int -- start of range
end = int -- end of range
Examples:
[folders byRange: @"Documents" to: @"Movies"]
[documents at: 1 to: 3]
[text byRange: [[TEPCon characters] at: 5]
to: [[TEPCon words] at: -2]]
by Test
A reference to each element that satisfies one or more conditions specified by a test expression:
[elements byTest: testExpression]
Test expressions consist of the following:
A relative reference to each element being tested. This reference must be constructed using the glue's 'Its' macro as its root, e.g.
TEIts. Its-based references support all valid reference forms, allowing you to construct references to its properties and elements. For example:TEIts [TEIts size] [[TEIts words] first]One or more conditional tests, implemented as methods on the reference being tested. Each method takes a test reference or a value as its sole argument.
Syntax:
[reference lessThan: value] [reference lessOrEquals: value] [reference equals: value] [reference notEquals: value] [reference greaterThan: value] [reference greaterOrEquals: value] [reference beginsWith: value] [reference endsWith: value] [reference contains: value] [reference isIn: value] [reference doesNotBeginWith: value] [reference doesNotEndWith: value] [reference doesNotContain: value] [reference isNotIn: value] value = reference or valueExamples:
[TEIts equals: @""] [[FNIts size] greaterThan: [NSNumber numberWithInt: 1024]] [[[TEIts words] first] beginsWith: @"A"] [[[TEIts characters] first] equals: [[TEIts characters] last]]Note that Boolean comparison tests can be written as either
[reference equals: AEMTrue]or justreference, e.g.[[folderRef files] byTest: [FNIts locked]]Zero or more logical tests, implemented as properties/methods on conditional tests. The
-AND:and-OR:methods take either a conditional or logic test as arguments .Syntax:
[test AND: test] [test OR: test] [test NOT]Examples:
[[TEIts equals: @""] NOT] [[[FNIts size] greaterThan: [NSNumber numberWithInt: 1024]] AND: [[FNIts size] lessThan: [NSNumber numberWithInt: 10240]] [[[[TEIts words] at: 1] beginsWith: @"A"] OR: [NSArray arrayWithObjects: [[[TEIts words] at: 2] contains: @"ce"], [[[TEIts words] at: 1] equals: @"foo"], nil]]
Insertion location
Insertion locations can be specified at the beginning or end of all elements, or before or after a specified element or element range.
Syntax:
[elements beginning]
[elements end]
[element before]
[element after]
Examples:
[documents end]
[[paragraphs at: 1] before]