0

Given a scriptable object's NSScriptObjectSpecifier, how do I tell the index of object(s) in their container?

I cannot find any accessor method for that in the NSScriptObjectSpecifier or its subclasses.

If the object is a NSIndexSpecifier, it's easy to get the index. But if it's something else, e.g. a NSNameSpecifier, then it needs to be inquired. The Cocoa Scripting guide says that the framework does that automatically when it needs to learn the index, but it's not explained how that's done.

There is the method indicesOfObjectsByEvaluatingObjectSpecifier a scriptable object can implement to return such indices, but that's an optional method, and so I assume I cannot simply invoke that on any scriptable object.

Is there some other easy way to learn the index, or do I have to implement my own method that walks down the specifiers and handles all the possible cases on its own?

Thomas Tempelmann
  • 11,045
  • 8
  • 74
  • 149
  • Have you implemented `-objectInAtIndex:`? – vadian May 15 '16 at 13:08
  • Yes. How would that help? I don't want to have to get all items and then search them for the object in question. That'd lead to quite a bad performance. – Thomas Tempelmann May 15 '16 at 13:21
  • Then I don't understand the question. For any subclass in the hierarchy you have to provide a `NSScriptObjectSpecifier` containing the `containerClassDescription`. From that specifier the framework can identify the proper index. – vadian May 15 '16 at 14:46
  • I need the inverse function: I know the specifier and want to know its index, thru Cocoa Scripting. As the specifier may point to any number of different classes, I cannot query the objects directly unless I'd extend each of them with my own protocol to return their index in their container. That's overkill, IMHO. I need this in order to be able to tell if an array of such specifiers is in order (needed for creating NSRangeSpecifier, see http://stackoverflow.com/questions/37239287), though the suggestion in your 1st comment would help identifying the indices, but it'll be slow with large lists – Thomas Tempelmann May 15 '16 at 15:43
  • @vadian If you put your comment into an answer, I'll upvote it (though it won't be the accepted answer because it doesn't answer the question completely, i.e. how to return a non-consecutive list). – Thomas Tempelmann May 15 '16 at 15:48
  • No, thanks, it's indeed more a comment than an answer. – vadian May 15 '16 at 15:53

1 Answers1

0

Are you looking for NSArray's

- indexOfObjectIdenticalTo:self

by any chance? I use this in an -objectSpecifier method when I need to return a formIndex ospec.

Ron Reuter
  • 1,287
  • 1
  • 8
  • 14