我该如何跨preTE记录返回一个NSAppleScript结果 [英] How do I interprete a record returned as a NSAppleScript result
问题描述
我在运行使用NSAppleScript的executeAndReturnError方法的Objective-C的应用程序的AppleScript。这将返回包含脚本的结果的NSAppleEventDescriptor对象。我的脚本返回一个AppleScript记录。如何跨preT在Objective-C的记录?例如,如果返回的脚本记录是{名字:雅各布,电话12345678}我如何获得字符串雅各布中的name属性。
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,或在酒吧使用它|名称|。
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:实例方法词典中得到雅各布
Of course after the record is in dictionary format you can just use the "valueForKey:" instance method on the dictionary to get "Jacob".
这篇关于我该如何跨preTE记录返回一个NSAppleScript结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!