0

So I have simple applescript that returns (return "test") the following:

NSAppleEventDescriptor: 'utxt'("test")

I found this question and tried to replicate what it was doing with the following code

NSAppleScript *scriptObject = [[NSAppleScript alloc]initWithContentsOfURL:[NSURL fileURLWithPath: scriptPath]
                                                                    error:&error];
returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
NSLog(@"Return Discriptor,%@",returnDescriptor);

NSAppleEventDescriptor *utxt = [returnDescriptor descriptorForKeyword:'utxt'];
NSLog(@"Found utxt Discriptor: %@",utxt);

NSString *scriptReturn = [utxt stringValue];
NSLog(@"Found utxt string: %@",scriptReturn);

but it is not returning anything:

Return Discriptor, Found utxt Discriptor: (null) Found utxt string: (null)

Community
  • 1
  • 1
acidprime
  • 279
  • 3
  • 18

1 Answers1

0

It seems to me that returnDescriptor is already the descriptor and you wouldn't need this: [returnDescriptor descriptorForKeyword:'utxt'];. I didn't test this code but give it a try...

NSDictionary *errorDict = nil;
NSAppleScript *scriptObject = [[NSAppleScript alloc]initWithContentsOfURL:[NSURL fileURLWithPath:scriptPath] error:&errorDict];
if (errorDict) {
   NSLog(@"Error: %@", errorDict);
   return;
}
NSAppleEventDescriptor *returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
if (errorDict) {
   NSLog(@"Error: %@", errorDict);
   [scriptObject release];
   return;
}

NSString *scriptReturn = [returnDescriptor stringValue];
NSLog(@"Found utxt string: %@",scriptReturn);
[scriptObject release];
regulus6633
  • 18,848
  • 5
  • 41
  • 49
  • So that works, I guess I was thinking from a previous example that I could break out the return into multiple objects. In my case a single value will work well enough. – acidprime Jul 22 '11 at 23:15
  • You are returning a string so it is only 1 value. If you want to return multiple values then you return a list of values from applescript and you wouldn't use "stringValue" to convert the list in objective-c. There's a different approach for working with lists. You would get a discriptorAtIndex from the returned descriptor, and then the stringValue of that. And remember, the first index is 1 not 0. – regulus6633 Jul 23 '11 at 08:54
  • I decided to just return a plist value as that was more easily parsed and could be contained in a single string with the code above. – acidprime Aug 22 '11 at 20:36