【问题标题】:How to Extract AppleScript Data from a NSAppleEventDescriptor in Cocoa and Parse it如何从 Cocoa 中的 NSAppleEventDescriptor 中提取 AppleScript 数据并解析它
【发布时间】:2009-08-07 21:04:45
【问题描述】:

我正在做的是在 Cocoa 中执行 AppleScript。它以 NSAppleEventDescriptor 的形式返回一些数据,NSLog() 打印如下:

<NSAppleEventDescriptor: 'obj '{ 'form':'name', 'want':'dskp', 'seld':'utxt'("69671872"), 'from':'null'() }>

我想获取这些数据并将其转换为 NSDictionaryNSArray 或其他有用的东西,以便我可以从中提取内容(特别是我在该领域之后持有“69671872”号码)。它似乎是某种数组,但我对 Apple Events 的了解相当有限。关于如何做到这一点的任何想法?

这是创建上述数据的来源:

NSString *appleScriptSource = [NSString stringWithFormat:@"tell application\"System Events\"\n return desktop 1\n end tell"];
NSDictionary *anError;
NSAppleScript *aScript = [[NSAppleScript alloc] initWithSource:appleScriptSource];
NSAppleEventDescriptor *aDescriptor = [aScript executeAndReturnError:&anError];

NSLog (@"%@", aDescriptor);
[aScript release];

提前感谢您的帮助! :)

【问题讨论】:

    标签: objective-c cocoa applescript appleevents


    【解决方案1】:

    这是一个记录,而不是一个列表。尝试descriptorForKeyword:,传递与您想要的四字符代码匹配的常量。 (常量在 Apple 事件标头中声明。)

    【讨论】:

    • 啊,完美。非常感谢! :)
    【解决方案2】:
    [[aDescriptor descriptorForKeyword:keyAEKeyData] stringValue]
    

    【讨论】:

    • 非常感谢! :) 这看起来太容易了。我想我有很多东西要学。 ;)
    • @Peter Hosey 感谢您的不断更换(我应该明白这一点)。
    【解决方案3】:

    我无法让 Peter Hosey 的解决方案在包装为 NSAppleEventDescriptor 的 AppleScript 列表上工作。相反,我遇到了以下将列表强制为 ObjC 数组的解决方案:

               NSAppleEventDescriptor *listDescriptor = [result coerceToDescriptorType:typeAEList];
               NSMutableArray *thisArray = [[NSMutableArray alloc] init];
               for (NSInteger i = 1; i <= [listDescriptor numberOfItems]; ++i) {
                   NSAppleEventDescriptor *stringDescriptor = [listDescriptor descriptorAtIndex:i];
                   [thisArray addObject: stringDescriptor.stringValue];
               }
               NSLog(@"array result: %@", thisArray);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-28
      相关资源
      最近更新 更多