CocoaDev

Edit AllPages

See also HowToSupportAppleScript.

Many Cocoa developers who have tried to support custom object classes in AppleScript have run into the problem of coercion. Apple has a very vague class that seems like it would handle this for us, NSScriptCoercionHandler. NSScriptCoercionHandler lets you register handlers for your custom classes. The only problem with this class: it isn’t called every where you would expect it to be used. It seems to only be used when “coerceValue:forKey:” (called when you set a property with a different class than KVC expects.) On the other side of things is the get command. You would expect it to use the same handlers to coerce to the requested class, but this is not the case.

I have subclasses NSGetCommand and tacked on to the performDefaultImplementation method the code required to use NSScriptCoercionHandler and some extra logic to do general coercion for common classes. To get this to work you will need to call this early in you application load sequence: [JVGetCommand poseAsClass:[NSGetCommand class]]. – TimothyHatcher

@implementation JVGetCommand

The latest version of this implementation is also here: http://project.colloquy.info/trac/file/trunk/JVGetCommand.m (incase I make future modifications.) Comments and suggestions are welcome.