【发布时间】:2021-03-25 02:54:40
【问题描述】:
我正在尝试使我的应用程序可编写脚本,我想做的一件事是从我的应用程序返回一个数组并进入 AppleScript,以便可以在那里进一步处理它。我想我可以通过返回数组的计数来做到这一点,然后让 AppleScript 从 1 迭代到 n 以返回数组中的每个项目 - 但我认为这不会非常有效。
我的代码目前如下所示:
sdef文件的相关部分
<command name="List" code="lflecont" description="List file types found in data stream">
<cocoa class="ListDataContentCommand"/>
<result description="an array containing the types of all the data blocks in the File">
<type type="any" list="yes"/>
</result>
</command>
ListDataContentCommand.h
@interface ListDataContentCommand : NSScriptCommand
@end
ListDataContentCommand.m
@implementation ListDataContentCommand
-(id)performDefaultImplementation {
return @[@"Jam",@"Fish",@"Eggs"];
}
@end
为了对此进行测试,我创建了以下简单的 AppleScript……
tell application "Data Dump"
open "/Volumes/Test Data/test.dat"
set theList to List
end tell
这会返回一个错误 - error "Data Dump got an error: Can’t continue List." number -1708
如何让我的数组输出?
就此而言,如何返回 NSDictionary?或者那是不可能的? NSString 是否可以直接返回文本 - 还是需要先转换为 Cstring?
恐怕是新手问题,但关于 AppleScript 的好信息似乎很难获得!
【问题讨论】:
-
通常标准类型
NSString、NSNumber和NSDate隐式桥接到 AppleScript 类型。默认情况下,AppleScript 同步工作,ListDataContentCommand脚本命令返回nil(AppleScript 端为missing value)。如果您需要异步处理,您必须在当前脚本命令上调用suspend和resume。真正的返回类型是什么?如果您使用any,则必须返回NSAppleEventDescriptor。要返回字典,您必须在 sdef 文件中创建record-type。 -
所以要返回一个 NSArray,我必须将它设置为返回一个列表(在 sdef 中)?我理解正确吗?
-
不,定义真正的返回类型并添加
list="yes"。id返回类型也涵盖NSArray
标签: objective-c cocoa applescript