cocoa - 如何从 NSScriptCommand 子类的 PerformDefaultImplementation 返回对象列表?

标签 cocoa applescript

我正在 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 方法返回 NSStringNSArray:

- (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/

相关文章:

macos - 如何在 Interface Builder 中对 OS X 切换按钮进行分组

objective-c - 在 Cocoa 中创建小部件的自定义组合

applescript - 如何告诉 Finder 在 AppleScript 中选择/显示多个文件

pdf-generation - 使用 Automator 或 Applescript 或两者递归地将文档打印为 PDF

php - 如何从 php 执行 applescript

macos - 在使用 Applescript 单击之前验证复选框

objective-c - 使用 Grand Central Dispatch 设置变量不可检索

html - 如何在 Cocoa 中将 RTF 文本转换为 Markdown 语法的纯文本?

objective-c - Applescript 应用程序中使用的 Apple 代码

iphone - xPath 分组如何?