Cocoa 使用NSAppleScript从活动查找器窗口检索选定文件的列表

Cocoa 使用NSAppleScript从活动查找器窗口检索选定文件的列表,cocoa,applescript,osx-mountain-lion,applescript-objc,Cocoa,Applescript,Osx Mountain Lion,Applescript Objc,有人知道我如何从active finder窗口检索选定文件的列表吗? 我对AppleScript一点经验都没有,非常感谢您的帮助 我试着从另一个答案中使用下面的代码,但是我可以让它返回任何东西。没有错误,没有结果,什么都没有。我知道,即使它真的起作用,那么它只会给我第一个文件,我需要完整的文件选择 NSAppleScript *script = [[NSAppleScript alloc] initWithSource: @"tell appl

有人知道我如何从active finder窗口检索选定文件的列表吗? 我对AppleScript一点经验都没有,非常感谢您的帮助

我试着从另一个答案中使用下面的代码,但是我可以让它返回任何东西。没有错误,没有结果,什么都没有。我知道,即使它真的起作用,那么它只会给我第一个文件,我需要完整的文件选择

NSAppleScript *script = [[NSAppleScript alloc] initWithSource:
                          @"tell application \"Finder\"\n"
                          "set selectedItems to selection\n"
                          "if ((count of selectedItems) > 0) then\n"
                          "set selectedItem to (item 1 of selectedItems) as alias\n"
                          "container window of selectedItem\n"
                          "end if\n"
                          "end tell\n"];

if (script == nil) {
    NSLog(@"failed to create script!");
}

NSAppleEventDescriptor *result = [script executeAndReturnError:&errorMessage];
if (result) {
    // POSIX path returns trailing /'s, so standardize the path
    NSString *path = [[result stringValue] stringByStandardizingPath];
}

for (id key in errorMessage) {
    NSLog(@"key: %@, value: %@", key, [errorMessage objectForKey:key]);
}
编辑

我在执行脚本之前打印了错误字典,这就是为什么没有错误。 这是我在finder窗口中选择两个文件时得到的结果:

2013-07-23 13:36:14.817 myAPP[12959:303] key: NSAppleScriptErrorMessage, value: Finder got an error: Can’t get container window of document file "myAPP-logo-white-n-black.png" of folder "untitled folder" of folder "Desktop" of folder "Shumais" of folder "Users" of startup disk.
2013-07-23 13:36:14.817 myAPP[12959:303] key: NSAppleScriptErrorRange, value: NSRange: {151, 16}
2013-07-23 13:36:14.817 myAPP[12959:303] key: NSAppleScriptErrorBriefMessage, value: Can’t get container window of document file "myAPP-logo-white-n-black.png" of folder "untitled folder" of folder "Desktop" of folder "Shumais" of folder "Users" of startup disk.
2013-07-23 13:36:14.818 myAPP[12959:303] key: NSAppleScriptErrorNumber, value: -1728
2013-07-23 13:36:14.818 myAPP[12959:303] key: NSAppleScriptErrorAppName, value: Finder
谢谢大家!!
Shumais

更新2。 所以我才意识到,我的另一个答案是给你第一个文件的容器。因为就我而言,我将你提供并更正的代码改为红色,而不是注意你说你需要一个路径列表

下面是一些代码,用于获取查找器中所有选定项的列表(作为posix路径)

 NSMutableArray * pathArray =[[NSMutableArray alloc ] initWithCapacity:10];
    NSDictionary* errorMessage = [NSDictionary dictionary];
    NSString *code = @"set biglist to {}\n tell application \"Finder\" to set theSeletion to (get selection)\n if (count of theSeletion) > 0 then\n repeat with i from 1 to number of items in theSeletion\n set this_item to POSIX path of (item i of theSeletion as alias)\n copy this_item to end of biglist\n end repeat\n return biglist\n  end if\n  ";
    NSLog(@"code =  %@ ",code);
    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:code];
    NSAppleEventDescriptor *result = [script executeAndReturnError:&errorMessage];

    count = [result numberOfItems];
    for (int i = 1; i <= count; ++i) {
    NSAppleEventDescriptor *desc =  [result descriptorAtIndex:i]  ;
    id thisPath = [desc stringValue];


        [pathArray addObject:thisPath];


    }

    if (script == nil) {
        NSLog(@"failed to create script!");
    }

    for (id key in errorMessage) {
        NSLog(@"key: %@, value: %@", key, [errorMessage objectForKey:key]);
    }


      NSLog(@"pathArray =  %@ ",pathArray);

    [pathArray release];
返回-->“Macintosh HD:Users:Shumais:Desktop:”

或者获取POSIX路径:

 NSDictionary* errorMessage = [NSDictionary dictionary];
    NSString *code = @"tell application \"Finder\"\n set theSeletion to (get selection)\n if (count of theSeletion) > 0 then\n return POSIX path of (container of item 1 of theSeletion as alias)\n end if\n end tell";
     NSLog(@"code =  %@ ",code);
    NSAppleScript *script = [[NSAppleScript alloc] initWithSource:code];
    NSAppleEventDescriptor *result = [script executeAndReturnError:&errorMessage];

    if (script == nil) {
        NSLog(@"failed to create script!");
    }
    if (result) {
        // POSIX path returns trailing /'s, so standardize the path
        NSString *path = [[result stringValue] stringByStandardizingPath];

          NSLog(@"path =  %@ ",path);
    }

    for (id key in errorMessage) {
        NSLog(@"key: %@, value: %@", key, [errorMessage objectForKey:key]);
    }

返回->“/Users/Shumais/Desktop/untitled folder”

容器窗口似乎不起作用。请改用Parent:

set parentFolder to ""
tell application "Finder"
    set selectedItems to selection
    if ((count of selectedItems) > 0) then
        set selectedItem to (item 1 of selectedItems) as alias
        set parentFolder to the parent of selectedItem
    end if
end tell
return parentFolder

我在选择桌面文件夹中的几个文件时尝试了您的脚本,这是我得到的::key:NSAppleScriptErrorMessage,value:Finder出错:无法获取文件夹“用户”的文件夹“桌面”的文件夹“Shumais”的文件夹“untitled folder”的文档文件“app preferences.png”的容器窗口关于启动盘…我必须告诉你,我根本没有使用苹果脚本的经验。我在第一行用两个双引号替换了两个单引号。这有什么区别吗?如果你复制脚本,粘贴到脚本编辑器中,然后像那样运行,会发生什么?虽然你的脚本没有问题,但它只得到尽管如此,它还是帮助我完成了自己的脚本。谢谢它获取所选内容的父文件夹。如果这不是你想要的,那么你需要更好地解释。嗨,我已经更新了我的代码。测试和工作。嗨,我刚刚意识到我的和另一个答案是给你第一个文件的容器。因为就我而言,我把你提供的代码改成红色并更正了它,而不是注意你说你需要一个路径列表,所以我用一些代码更新了答案,得到了列表谢谢你的帮助!甚至在最后一次编辑之前,你的答案和另一个答案都很有帮助,使我能够编写一个脚本,完成我所需要的。它几乎和你的一样,但也有一些不同,但最终结果是一样的。AppleScript是一个真正的难题,现在我正试图弄清楚如何使用NSTask运行它,因为脚本会冻结整个应用程序,直到它完成,但我想这是另一个问题:)我希望有更多关于这些主题的帮助,可用的通常是非常基本的介绍性内容。再次感谢。
set parentFolder to ""
tell application "Finder"
    set selectedItems to selection
    if ((count of selectedItems) > 0) then
        set selectedItem to (item 1 of selectedItems) as alias
        set parentFolder to the parent of selectedItem
    end if
end tell
return parentFolder