无法解析由NSAppleScript方法executeAndReturnError返回的结果
问题描述:
这是我使用的代码:无法解析由NSAppleScript方法executeAndReturnError返回的结果
NSDictionary *errorInfo=nil;
NSString *[email protected]"tell application \"Mail\"\nget name of mailbox of every account\nend tell";
NSAppleScript *run = [[NSAppleScript alloc] initWithSource:source];
NSAppleEventDescriptor *aDescriptor=[[NSAppleEventDescriptor alloc]init];
aDescriptor=[run executeAndReturnError:&errorInfo];
[aDescriptor coerceToDescriptorType:'utxt'];
NSLog(@"result:%@",[aDescriptor stringValue]);
输出,我得到: 结果:(空)
请帮我人在这。提前感谢:)
答
IIRC将返回列表描述符填充列表描述符。你需要遍历它们并提取你想要的信息。您还正在初始化一个描述符,然后立即覆盖它的指针。做一些像(未经测试):
NSDictionary *errorInfo = nil;
NSString *source = @"tell application \"Mail\"\nget name of mailbox of every account\nend tell";
NSAppleScript *run = [[NSAppleScript alloc] initWithSource:source];
NSAppleEventDescriptor *aDescriptor = [run executeAndReturnError:&errorInfo];
NSInteger num = [aDescriptor numberOfItems];
// Apple event descriptor list indexes are one-based!
for (NSInteger idx = 1; idx <= num; ++idx) {
NSAppleEventDescriptor *innerListDescriptor = [aDescriptor descriptorAtIndex:idx];
NSInteger innerNum = [innerListDescriptor numberOfItems];
for (NSInteger innerIdx = 1; innerIdx <= innerNum; ++innerIdx) {
NSString *str = [[innerListDescriptor descriptorAtIndex:innerIdx] stringValue];
// Do something with str here
}
}