可可脚本:接受并返回NSData [英] Cocoa Scripting: Accept and return NSData
问题描述
为了支持可编写脚本的Mac应用程序中的二进制数据交换,我希望尽可能使用AS-ObjC桥接器以NSData的形式接收和传递数据。
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.
例如,我想在AppleScript中使此代码成为可能:
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
sdef包含以下值类型和属性:
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>
然后我将转换处理程序实现为 NSData $ c的扩展$ c>,类似于Sketch示例将NSColor转换为值类型 RGB颜色的方式:
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
}
desc的描述为:
The desc's description is:
<NSAppleEventDescriptor: 'obj '{
'form':'ID ',
'want':'ocid',
'seld':'optr'($E0A8430080600000$),
'from':null()
}>
类似地,调用 [NSScriptObjectSpecifier _scriptingSpecifierWithDescriptor:descriptor]
也返回NULL。
Similarly, invoking [NSScriptObjectSpecifier _scriptingSpecifierWithDescriptor:descriptor]
returns NULL as well.
那么,如何获取应用程序代码中的实际NSData对象?
So, how do I get to the actual NSData object inside my app code?
以及如何将NSData对象返回到AppleScript?
And how do I return a NSData object to the AppleScript?
推荐答案
Shane Stanley 确实知道一种方法,它甚至不需要我的应用程序中的额外代码-相反,可以使用以下两个转换函数在AppleScript中完成所有操作:
Shane Stanley did indeed know a way, and it does not even require extra code in my app - instead, it can all be done in AppleScript, with these two conversion functions:
use framework "Foundation"
set nsData1 to current application's NSData's dataWithContentsOfFile:"/etc/hosts"
set asData to my ASDataFromNSData(nsData1)
set nsData2 to my NSDataFromASData(asData)
on ASDataFromNSData(theData)
set theCode to current application's NSHFSTypeCodeFromFileType("'rdat'")
return (current application's NSAppleEventDescriptor's descriptorWithDescriptorType:theCode |data|:theData) as data
end ASDataFromNSData
on NSDataFromASData(asData)
return (current application's NSArray's arrayWithObject:asData)'s firstObject()'s |data|()
end NSDataFromASData
看来 rdat
是用于此目的的特殊AppleScript类型,该框架会自动处理NSData的转换。但是,我找不到在AE.framework标头中声明的类型。
It appears that rdat
is a special AppleScript type for this purpose, with the framework automatically handling the conversion with NSData. I can't find that type declared in the AE.framework's headers, though.
然后我仍然必须处理此 rdat $ c不过,$ c>会在我的应用代码中明确输入。但是我不需要sdef中的值类型,并且可以将属性更改为:
I then still have to handle this rdat
type explicitly in my app's code, though. But I won't need the value-type in the sdef, and can change the property to:
<property name="raw data" code="rawD" type="any">
<cocoa key="rawData"/>
</property>
将数据作为 rdat
返回是相似的。我的 -rawData
方法:
Returning data as rdat
is similar. My -rawData
method:
return [NSAppleEventDescriptor descriptorWithDescriptorType:'rdat' data:myNSData];
这仅在我将属性类型声明为 any时有效。如果我使用 type = rdat
,脚本调试器会将该类型显示为专用的原始数据类型,但是当我尝试在其中设置或获取属性时,会出现-10000错误脚本。
This only works if I declare the property type as "any", though. If I use type="rdat"
, Script Debugger shows the type as a dedicated raw data type, but then I get -10000 errors when trying to set or get the property in a script.
这篇关于可可脚本:接受并返回NSData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!