2

我有一个 Cocoa 应用程序,它的一部分在 Web 视图中从用户那里获取 AppleScript。我目前可以从命令中传递单个字符串(例如当前的 iTunes 歌曲名称),但是,如果我运行返回记录的命令(我相信这是基于我的研究,可能是错误的),例如下面我得到'(null)'作为stringValue。

tell application "iTunes"
    get properties of current track
end tell

如果我在 Script Debugger 中运行它,我可以得到如下所示的表格,所以这显然是可能的。

但是,我尝试过的任何方法似乎都不起作用。基于许多 SO 答案,我尝试了不同的方法,例如循环遍历每个描述符索引,如在这个问题中。但是,这不再起作用,因为似乎密钥未包含在数组中。

所以,基本上,我需要能够将 AppleScript 输出转换为 JSON。我有一个序列化程序,所以这不是问题,只要我可以将它们放入我设置的 Cocoa 对象中。最好的方法是什么?

谢谢

4

1 回答 1

1

作为答案晚了很多个月,但如果你或其他人关心,这就是我对类似问题所做的事情。

查看(可悲的是已不复存在的)AppScript 框架。结合轻微修改的 SBJSON,我可以通过 Cocoa 对象将任何 AppleScript 记录转换为 JSON。

我在 Mac AppStore 上免费的JSON Helper中使用了它。您还可以在此处查看Google 代码的早期版本的源代码,如果您想使用 SBJSON 的修改版本,这可能会很有用。

在下面的示例中,AppleScript 记录是通过脚本命令提供的。

@implementation makeJSONFromRecord


- (id)performDefaultImplementation {

    NSDictionary    *asRecord;
    NSString        *result;
    AEMCodecs       *codecs = [[AEMCodecs alloc] init];


    // Use appscript framework to unpack the event into an object we can use

    asRecord =[codecs unpack:[self directParameter]];
    [codecs release];

    // Use the JSON framework to convert the object to JSON notation

    result = [asRecord JSONRepresentation];

    if (result==nil) {

    //We failed to create any valid JSON so return nothing
        NSLog(@"Failed to make JSON from: %@", asRecord);
        result=@"";
    }

    // Return the result to the applescript

    return result;

}

@end
于 2014-02-09T17:07:24.727 回答