In order to support binary data exchange in my scriptable Mac app, I like to make it possible to receive and deliver data as NSData, using the AS-ObjC bridge, if that's possible.
For instance, I like to make this code possible in AppleScript:
use framework "Foundation"
set theData to current application's NSData's dataWithContentsOfFile:"/some/binary/file"
tell application "MyApp"
set raw value to theData
end tell
The sdef contains a value-type and property for this:
<suite name="My Suite" code="Demo">
<value-type name="ObjCNSData" code="NSDa">
<cocoa class="NSData"/>
</value-type>
<class name="application" code="capp">
<property name="raw data" code="rawD" type="ObjCNSData">
<cocoa key="rawData"/>
</property>
I then implement the conversion handler as an extension to NSData
, similarly to how the Sketch example converts NSColor to the value-type "RGB Color":
@implementation NSData(DemoScripting)
+ (NSData *)scriptingObjCNSDataWithDescriptor:(NSAppleEventDescriptor *)desc {
id res = [desc coerceToDescriptorType:'NSDa'];
// -> res is NULL, which is not getting me any further
}
The desc's description is:
<NSAppleEventDescriptor: 'obj '{
'form':'ID ',
'want':'ocid',
'seld':'optr'($E0A8430080600000$),
'from':null()
}>
Similarly, invoking [NSScriptObjectSpecifier _scriptingSpecifierWithDescriptor:descriptor]
returns NULL as well.
So, how do I get to the actual NSData object inside my app code?
And how do I return a NSData object to the AppleScript?