如何解释作为 NSAppleScript 结果返回的记录 [英] How do I interprete a record returned as a NSAppleScript result

查看:22
本文介绍了如何解释作为 NSAppleScript 结果返回的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 NSAppleScript 的 executeAndReturnError 方法从objective-c 应用程序运行applescript.这将返回一个包含脚本结果的 NSAppleEventDescriptor 对象.我的脚本返回一个applescript 记录.如何解释objective-c中的记录?例如,如果返回的脚本记录是 { name:"Jakob", phone:"12345678" } 如何在 name 属性中获取字符串Jakob"?

I run an applescript from an objective-c application using NSAppleScript the executeAndReturnError method. This returns an NSAppleEventDescriptor object containing the result of the script. My script returns an applescript record. How do interpret the record in objective-c? For instance if the returned script record is { name:"Jakob", phone:"12345678" } how do I get the string "Jakob" in the name property?

推荐答案

这是一个将记录转换为字典的方法.请注意,我没有尝试过,但它应该可以工作.

Here's a method to convert the record to a dictionary. Note that I didn't try this but it should work.

此外,您的名称"键不是在 applescript 中使用的好键,因为名称"对 applescript 有其他含义.我经常在记录中使用它时遇到问题.我建议将其更改为theName"之类的内容或在栏 |name| 中使用它.

Also, your "name" key is not a good key to use in applescript because "name" has other meanings to applescript. I've often had problems using that in a record. I would suggest changing it something like "theName" or using it in bars |name|.

-(NSDictionary*)recordToDictionary:(NSAppleEventDescriptor*)theDescriptor {
    NSUInteger j,count;
    id thisDescriptor = [theDescriptor descriptorAtIndex:1];
    count = [thisDescriptor numberOfItems];
    NSMutableDictionary* thisDictionary = [NSMutableDictionary dictionaryWithCapacity:count/2];
    for (j=0; j<count; j=j+2) {
        NSString* theKey = [[thisDescriptor descriptorAtIndex:(j+1)] stringValue];
        NSString* theVal = [[thisDescriptor descriptorAtIndex:(j+2)] stringValue];
        [thisDictionary addEntriesFromDictionary:[NSDictionary dictionaryWithObject:theVal forKey:theKey]];
    }
    return (NSDictionary*)[[thisDictionary retain] autorelease];
}

当然,在记录为字典格式后,您可以直接使用字典上的valueForKey:"实例方法来获取Jacob".

Of course after the record is in dictionary format you can just use the "valueForKey:" instance method on the dictionary to get "Jacob".

这篇关于如何解释作为 NSAppleScript 结果返回的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆