How do I interprete a record returned as a NSAppleScript result

£可爱£侵袭症+ 提交于 2019-12-11 10:53:24

问题


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?


回答1:


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

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];
}

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



来源:https://stackoverflow.com/questions/13180653/how-do-i-interprete-a-record-returned-as-a-nsapplescript-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!