Selection property

What’s the best way to implement a “selection” property in a class? In my case, it always refers to a list of selected objects, and they will always be of class “asset”. I also want to be able to do “set someProperty of selection”, where someProperty is a property of “asset”.

Do I need to make an actual Objective-C class for a Selection object, then handle each property and command that “asset” handles? Or is there an easier way?

@alldritt used to have a blog post on implementing a selection property — maybe he will pipe up.

I presume you are implementing this using Cocoa Scripting.

Implementing a selection property is fairly simple. If the selection property is application global, add a selection property definition to the application object in your SDEF. If the selection is per-document, then add it to your document object in your SDEF.

The selection property should indicate that its value is a list. The data type can be any or if the value is always going to be an asset then use that data type.

As for being able to manipulate the referenced asset objects through the selection property, you’ll need to extend Cocoa Scripting to support this. Out of the box, Cocoa Scripting does not support property-of-property or array-reference-of-property object specifiers.

I think @ShaneStanley is referring to this post which does not really explain how to implement a selection property in a general sense.

Yes, it’s using Cocoa scripting. I know how to add the selection property, it’s the implementation that has me stumped. Do I implement the selection as its own object in Cocoa, and then it handles the “get” and “set” verbs? That’s how we did it way back in Carbon land.

Sorry for the delay in responding.

For FaceSpan, I did it using a simple property that returned a list of object references and then hacked Cocoa Scripting’s object resolution to allow property-of-property access throughout the application’s object model.

Both approaches (as a simple property returning a list of object references, and an object with properties) can be made to work.

I’m going to have to dig up the old FaceSpan code to figure out how I did this. I’ll get back to you when I find the code I used and can factor it into something you can employ.

Had any time to look into this? I’ve been messing with all the different Cocoa scripting classes, trying to find some combination of junk that works, but no luck so far. This is such a common thing that an app would do, I’m surprised Apple didn’t document how to go about it.

I used a DTS incident for this so Apple could tell me how this is supposed to work, and they say it can’t be done, but offered 3 different workaround that are pretty lame (like adding a “selected” property to the asset class, so then the scripter would have to use a whose clause and lookup would be slower). W. T. F.

I’m implementing a scriptable text editor using Cocoa Scripting, and have encountered the dreaded lack of property-of-a-property accessor. Mark referred to this earlier in this thread in relation to how he solved the problem in FaceSpan. I’m hoping someone can share some Objc or Swift code that solves the problem.

My document has a property “default paragraph style” which is a record type in my SDEF, and is implemented by returning a NSParagraphStyle converted to an NSDictionary. This all works fine. However, getting a property of the record in one line does not work as I had hoped.

tell application "Ted"
	tell document 1
		-- desired statement:
		set paraAlignment to alignment of default paragraph style --> error

		-- workaround:
		set paraProps to default paragraph style --> {alignment:4, ...}
		set paraAlignment to alignment of paraProps --> 4
	end tell
end tell

I’d like to avoid the workaround.

I think it’s an inherent weakness in Cocoa Scripting — the only apps I know where it works don’t use Cocoa Scripting. If it’s any consolation, I suspect that most scripters are, sadly, expecting it these days.

About the best you can do is:

set paraAlignment to alignment of (get default paragraph style)