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

Cocoa 从NSAppleEventDescriptor解析一段简单的文本,cocoa,applescript,Cocoa,Applescript,因此,我有一个简单的applescript,它返回(返回“test”)以下内容: NSAppleEventDescriptor:“utxt”(“测试”) 我发现了这个问题,并尝试用下面的代码复制它在做什么 NSAppleScript *scriptObject = [[NSAppleScript alloc]initWithContentsOfURL:[NSURL fileURLWithPath: scriptPath]

因此,我有一个简单的applescript,它返回(返回“test”)以下内容:

NSAppleEventDescriptor:“utxt”(“测试”)

我发现了这个问题,并尝试用下面的代码复制它在做什么

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描述符:(空)
找到utxt字符串:(null)

在我看来,returnDescriptor已经是描述符,您不需要它:[returnDescriptor DescriptorWorkyWord:'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];

那么
错误
errorDict
呢?它们包含什么?这样就行了,我想我在前面的一个例子中想,我可以将返回分解成多个对象。在我的例子中,一个值就足够了。您返回的是一个字符串,所以它只有一个值。如果要返回多个值,那么可以从applescript返回一个值列表,而不必使用“stringValue”在objective-c中转换该列表。使用列表有一种不同的方法。您将从返回的描述符中获得一个descriptoratindex,然后是该描述符的stringValue。请记住,第一个索引是1而不是0。我决定只返回一个plist值,因为它更容易解析,并且可以用上面的代码包含在单个字符串中。