Objective c NSAppleEventDescriptor到NSArray

Objective c NSAppleEventDescriptor到NSArray,objective-c,xcode,macos,cocoa,applescript,Objective C,Xcode,Macos,Cocoa,Applescript,我正在尝试使用NSAppleEventDescriptor中的列表值创建一个NSArray。几年前有一个问题,尽管解决方案返回一个NSString NSString *src = [NSString stringWithFormat: @"return {\"foo\", \"bar\", \"baz\"}\n"]; NSAppleScript *exe = [[NSAppleScript alloc] initWithSource:src]; NSAppleEventDescriptor *d

我正在尝试使用
NSAppleEventDescriptor
中的列表值创建一个
NSArray
。几年前有一个问题,尽管解决方案返回一个
NSString

NSString *src = [NSString stringWithFormat: @"return {\"foo\", \"bar\", \"baz\"}\n"];
NSAppleScript *exe = [[NSAppleScript alloc] initWithSource:src];
NSAppleEventDescriptor *desc = [exe executeAndReturnError:nil];

NSLog(@"%@", desc);

// <NSAppleEventDescriptor: [ 'utxt'("foo"), 'utxt'("bar"), 'utxt'("baz") ]>
NSString*src=[NSString stringWithFormat:@“return{\'foo\'、\'bar\'、\'baz\'}\n];
NSAppleScript*exe=[[NSAppleScript alloc]initWithSource:src];
NSAppleEventDescriptor*desc=[exe executeAndReturnError:nil];
NSLog(@“%@”,描述);
// 

我不确定需要哪个描述符函数将值解析为数组。

返回的事件描述符必须强制为列表描述符。
然后可以通过重复循环获得值

NSString *src = [NSString stringWithFormat: @"return {\"foo\", \"bar\", \"baz\"}\n"];
NSAppleScript *exe = [[NSAppleScript alloc] initWithSource:src];
NSAppleEventDescriptor *desc = [exe executeAndReturnError:nil];
NSAppleEventDescriptor *listDescriptor = [desc coerceToDescriptorType:typeAEList];
NSMutableArray *result = [[NSMutableArray alloc] init];
for (NSInteger i = 1; i <= [listDescriptor numberOfItems]; ++i) {
    NSAppleEventDescriptor *stringDescriptor = [listDescriptor descriptorAtIndex:i];
    [result addObject: stringDescriptor.stringValue];
}
NSLog(@"%@", result);
NSString*src=[NSString stringWithFormat:@“return{\'foo\'、\'bar\'、\'baz\'}\n];
NSAppleScript*exe=[[NSAppleScript alloc]initWithSource:src];
NSAppleEventDescriptor*desc=[exe executeAndReturnError:nil];
NSAppleEventDescriptor*listDescriptor=[desc强制描述符类型:typeAEList];
NSMUTABLEARRY*结果=[[NSMUTABLEARRY alloc]init];

对于(NSInteger i=1;i我编写了一个扩展来简化这个过程

请注意,
atIndex()
/
descriptorAtIndex:
有一个基于索引的索引

extension NSAppleEventDescriptor {

    func listItems() -> [NSAppleEventDescriptor]? {
        guard descriptorType == typeAEList else {
            return nil
        }

        guard numberOfItems > 0 else {
            return []
        }

        return Array(1...numberOfItems).compactMap({ atIndex($0) })
    }

}

如果有任何改进,请进行评论或编辑!

Objective-C版本也很好,谢谢!请确保在AppleScript中返回数组