Container classes that want to evaluate certain object specifiers on their own should implement the indicesOfObjectsByEvaluatingObjectSpecifier: method defined by NSScriptObjectSpecifiers (a category on NSObject). For example, you might choose to implement this method if you find that whose clause evaluation is too slow and you want to do your own evaluation to speed it up (though for most applications, performance of the default whose mechanism should be sufficient).
If this method returns nil, the object specifier method for the class does its own evaluation. If this method returns an array, the object specifier uses the NSNumber objects in the array as the indices of the specified objects.
Therefore, if you implement this method and when you evaluate the specifier there are no objects that match, return an empty array, not nil. If you find only one object, you return its index in an array. Returning an array with a single index where the index is –1 is interpreted to mean all the objects match.
The Sketch application implements this method in its document class, as shown in Listing 6-3. This allows Sketch to directly handle some range and relative specifiers for graphics, so it can support script statements such as graphics from circle 3 to circle 5, circles from graphic 1 to graphic 10, or circle before rectangle 3.
Listing 6-3 indicesOfObjectsByEvaluatingObjectSpecifier: method from Sketch
Here’s a description of how this method works:
If the passed specifier is a range specifier, it returns the result of invoking another Sketch method to evaluate the specifier.
The method indicesOfObjectsByEvaluatingRangeSpecifier: allows more flexible range specifiers to be used with the different graphic keys of a SKTDrawDocument.
You can examine this method in Sketch. Describing it in full is beyond the scope of this discussion.
If the passed specifier is a relative specifier, it returns the result of invoking another Sketch method to evaluate the specifier.
The method indicesOfObjectsByEvaluatingRelativeSpecifier: allows more flexible relative specifiers to be used.
Again, this method is available in Sketch, but is beyond the scope of this discussion.
For any other type of specifier, this method returns nil so that the default object specifier evaluation will take place.
Last updated: 2008-03-11