Object specifiers
How object specifiers work
[TO DO: finish (note that this is a bit of a rehash of ch.2; might be simpler just to refer users back to that)]
A property contains either a simple value describing an object attribute (name
, class
, creationDate
, etc.) or an object specifier representing a one-to-one relationship between objects (e.g. home
, currentTrack
).
Elements represent a one-to-many relationship between objects (documents
, folders
, fileTracks
, etc).
TO DO: note both appear as properties on XXSpecifier; users don't instantiate XXSpecifier directly but instead construct via chained property/method calls from XXApplication or generic specifier root (XXapp, XXcon, XXits)
characters/words/paragraphs of documents by index/relative-position/range/filter
[TO DO: list of supported reference forms, with links to sections below]
[TO DO: following sections should include AppleScript syntax equivalents for reference]
Reference forms
Property
var PROPERTY: XXSpecifier
Syntax:
specifier.property
Examples:
textedit.name
textedit.documents[1].text
finder.home.files.name
All elements
var ELEMENTS: XXSpecifier
Syntax:
specifier.elements
Examples:
finder.home.folders
textedit.documents
textedit.documents.paragraphs.words
Element by index
subscript(index: AnyObject) -> XXSpecifier
Syntax:
elements[selector]
selector : Int | AnyObject -- the object's index (1-indexed), or other identifying value [1]
[1] While element indexes are normally integers, some applications may also accept other types (e.g. Finder's file/folder/disk specifiers also accept alias values). The only exceptions are String
and XXSpecifier
, which are used to construct by-name and by-test specifiers respectively.
Be aware that index-based object specifiers always use _one-indexing_ (i.e. the first item is 1, the second is 2, etc.), not zero-indexing as in Swift (where the first item is 0, the second is 1, etc.).
Examples:
words[3]
items[-1]
Element by name
subscript(index: String) -> XXSpecifier
func named(name: AnyObject) -> XXSpecifier
Specifies the first element with the given name. (The subscript syntax is preferred; the named
method would only need used if a non-string value was required.)
Syntax:
elements[selector]
selector : String -- the object's name (as defined in its 'name' property)
Applications usually treat object names as case-insensitive. Where multiple element have the same name, a by-name specifier only identifies the first element found with that name. (To identify all elements with a particular name, use a by-test specifier instead.)
Examples:
disks["Macintosh HD"]
files["index.html"]
Element by ID
func ID(elementID:AnyObject) -> XXSpecifier
Syntax:
elements.ID(selector)
selector : AnyObject -- the object's id (as defined in its 'id' property)
Examples:
windows.ID(4321)
Element by absolute position
var first: XXSpecifier
var middle: XXSpecifier
var last: XXSpecifier
var any: XXSpecifier
Syntax:
elements.first -- first element
elements.middle -- middle element
elements.last -- last element
elements.any -- random element
Examples:
documents.first
paragraphs.last
files.any
Element by relative position
func previous(elementClass: AEBSymbol) -> XXSpecifier
func next(elementClass: AEBSymbol) -> XXSpecifier
Syntax:
// nearest element of a given class to appear before the specified element:
element.previous(elementClass)
// nearest element of a given class to appear after the specified element
element.next(elementClass)
elementClass : AEBSymbol -- the name of the previous/next element's class
Examples:
words[3].next(TED.word) // word 4
paragraphs[-1].previous(TED.character) // the last character before the last paragraph
Elements by range
subscript(from: AnyObject, to: AnyObject) -> XXSpecifier // by-range
Range references select all elements between and including two object specifiers indicating the start and end of the range. The start and end specifiers are normally declared relative to the container of the elements being selected.
These sub-specifiers are constructed using the glue's' XXcon
constant, e.g. TEDCon
, as their root. For example, to indicate the third paragraph relative to the currrent container object:
TEDCon.paragraphs[3]
Thus, to specify all paragraphs from paragraph 3 to paragraph -1:
paragraphs[TEDCon.paragraphs[3], TEDCon.paragraphs[-1]]
For convenience, sub-specifiers can be written in shorthand form where their element class is the same as the elements being selected; thus the above can be written more concisely as:
paragraphs[3, -1]
Some applications can handle more complex range references. For example, the following will work in Tex-Edit Plus:
words[TEPcon.characters[5], TEPcon.paragraphs[-2]]
Syntax:
elements[start, end]
start : Int | String | AEBSpecifier -- start of range
end : Int | String | AEBSpecifier -- end of range
Examples:
documents[1, 3]
folders["Documents", "Movies"]
text[TEPCon.characters[5], TEPCon.words[-2]]
Elements by test
subscript(index: XXSpecifier) -> XXSpecifier
A specifier to each element that satisfies one or more conditions specified by a test specifier:
elements[selector]
selector : XXSpecifier -- test specifier
Test expressions consist of the following:
A test specifier relative to each element being tested. This specifier must be constructed using the glue's 'XXits' root, e.g.
TEDIts
. Its-based references support all valid reference forms, allowing you to construct references to its properties and elements. For example:TEDIts TEDIts.size TEDIts.words.first
One or more conditional tests, implemented as operators/methods on the specifier being tested. The left-hand operand or receiver must be an
XXSpecifier
instance; the other operand or argument can be anything.Syntax:
specifier < value specifier <= value specifier == value specifier != value specifier > value specifier >= value specifier.beginsWith(value) specifier.endsWith(value) specifier.contains(value) specifier.isIn(value)
Examples:
TEDIts == "" FINits.size > 1024 TEDIts.words.first.beginsWith("A") TEDIts.characters.first == TEDIts.characters.last
Caution: if assigning a test specifier to a variable, the variable must be explicitly typed to ensure the compiler uses the correct operator overload, e.g.:
let test: AEBSpecifier = TEDIts.color == [0,0,0] let query = textedit.documents[1].words[test]
Zero or more logical tests, implemented as properties/methods on conditional tests. All operands must be conditional and/or logic test specifiers.
Syntax:
test && test test || test !test
Examples:
!(TEDIts.contains("?")) FINits.size > 1024 && FINits.size < 10240 TEDIts.words[1].beginsWith("A") || TEDIts.words[1].contains("ce") || TEDIts.words[2] == "foo"
Element 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.
var beginning: XXSpecifier
var end: XXSpecifier
var before: XXSpecifier
var after: XXSpecifier
Syntax:
elements.beginning
elements.end
element.before
element.after
Examples:
documents.end
paragraphs.last.before