gpt4 book ai didi

objective-c - 将 AppleScript 响应转换为 JSON

转载 作者:行者123 更新时间:2023-12-03 16:41:27 25 4
gpt4 key购买 nike

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

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

如果我在脚本调试器中运行它,我可以得到如下表所示的结果,所以这显然是可能的。

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

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

谢谢

最佳答案

作为答案晚了好几个月,但如果您或其他人关心的话,这就是我对类似问题所做的处理。

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

我在JSON Helper中使用过它Mac AppStore 上免费。您还可以在 Google 代码 here 上查看早期版本的源代码,如果您想使用 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

关于objective-c - 将 AppleScript 响应转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19888406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com