无法解析NSAppleScript方法executeAndReturnError返回的结果

时间:2011-10-20 09:21:03

标签: objective-c cocoa applescript

这是我正在使用的代码:

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=[[NSAppleEventDescriptor alloc]init];
aDescriptor=[run executeAndReturnError:&errorInfo];
[aDescriptor coerceToDescriptorType:'utxt'];
NSLog(@"result:%@",[aDescriptor stringValue]);

我得到的输出: 结果:(空)

请帮助我这个人。提前谢谢:)

1 个答案:

答案 0 :(得分:3)

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
    }
}