从NSAppleEventDescriptor中解析一段简单的文本

时间:2011-07-21 18:32:07

标签: cocoa applescript

所以我有简单的AppleScript返回(返回“test”)以下内容:

NSAppleEventDescriptor:'utxt'(“test”)

我发现this问题,并尝试使用以下代码复制它正在做的事情

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);

但它没有返回任何东西:

返回描述符, 找到utxt Discriptor :( null) 找到utxt字符串:(null)

1 个答案:

答案 0 :(得分:0)

在我看来,returnDescriptor已经是描述符,你不需要这样:[returnDescriptor descriptorForKeyword:'utxt'] ;.我没有测试这段代码,但试一试......

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