5

What I'm doing is executing an AppleScript inside of Cocoa. It returns some data as a NSAppleEventDescriptor, which NSLog() prints like so:

<NSAppleEventDescriptor: 'obj '{ 'form':'name', 'want':'dskp', 'seld':'utxt'("69671872"), 'from':'null'() }>

I want to take that data and turn it into a NSDictionary or NSArray, or something useful so I can extract stuff from it (specifically I'm after the field holding the "69671872" number). It appears to be an array of some sort, but my knowledge with Apple Events is fairly limited. Any idea on how to do this?

Here's the source creating the above data:

NSString *appleScriptSource = [NSString stringWithFormat:@"tell application\"System Events\"\n return desktop 1\n end tell"];
NSDictionary *anError;
NSAppleScript *aScript = [[NSAppleScript alloc] initWithSource:appleScriptSource];
NSAppleEventDescriptor *aDescriptor = [aScript executeAndReturnError:&anError];

NSLog (@"%@", aDescriptor);
[aScript release];

Thanks in advance for any help! :)

3 Answers3

3

That's a record, not a list. Try descriptorForKeyword:, passing the constant matching the four-character code you want. (The constants are declared in the Apple Events headers.)

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
2
[[aDescriptor descriptorForKeyword:keyAEKeyData] stringValue]
Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Wevah
  • 28,182
  • 7
  • 83
  • 72
  • Thanks so much! :) That looks too easy. I think I have quite a lot to learn. ;) –  Aug 08 '09 at 18:43
  • @Peter Hosey Thanks for the constant replacement (I should have caught that). – Wevah Jul 30 '13 at 11:32
0

I was unable to get Peter Hosey's solution to work on my AppleScript list wrapped as a NSAppleEventDescriptor. Instead, I came upon the following solution that coerces the list to an ObjC array:

           NSAppleEventDescriptor *listDescriptor = [result coerceToDescriptorType:typeAEList];
           NSMutableArray *thisArray = [[NSMutableArray alloc] init];
           for (NSInteger i = 1; i <= [listDescriptor numberOfItems]; ++i) {
               NSAppleEventDescriptor *stringDescriptor = [listDescriptor descriptorAtIndex:i];
               [thisArray addObject: stringDescriptor.stringValue];
           }
           NSLog(@"array result: %@", thisArray);
Antony
  • 41
  • 4