Question

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?

Was it helpful?

Solution

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".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top