我正在 Cocoa 应用程序中添加一个动词优先的 AppleScript 命令。 sdef 命令定义表明该命令返回文本字符串列表:
<command name="list names" code="ABCDLstN" description="return a list of names">
<cocoa class="ListNamesCommand"/>
<result type="text" list="yes" description="some names"/>
</command>
ListNamesCommand
类的 performDefaultImplementation
方法返回 NSString
的 NSArray
:
- (id)performDefaultImplementation {
return @[@"name 1", @"name 2"];
}
结果是异常:
2013-12-17 17:22:37.474 ListNames[31907:303] Error while returning the result of a script command: the result object...
(
name1,
name2
)
...could not be converted to an Apple event descriptor of type 'text'. This instance of the class '__NSArrayI' doesn't respond to -scriptingTextDescriptor messages.
一个workaround就是更改 sdef 将结果类型指定为 any
而不是 text
,并返回 NSAppleEventDescriptor 而不是 NSArray:
- (id)performDefaultImplementation {
NSAppleEventDescriptor *list = [NSAppleEventDescriptor listDescriptor];
[list insertDescriptor:[NSAppleEventDescriptor descriptorWithString:@"name 1"] atIndex:1];
[list insertDescriptor:[NSAppleEventDescriptor descriptorWithString:@"name 2"] atIndex:2];
return list;
}
但是,这会带来不幸的副作用,即将事件(在 AppleScript 编辑器字典查看器中)记录为返回任何类型。
有没有一个解决方案没有这个缺点?
最佳答案
我认为当您定义命令时,类型应该是“文本列表”。我从未见过有关“list=yes”的部分。我不是专家,但我会删除它。祝你好运。
关于cocoa - 如何从 NSScriptCommand 子类的 PerformDefaultImplementation 返回对象列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20646796/