2

有谁知道我如何从活动的查找器窗口中检索选定文件的列表?我对 AppleScript 完全没有经验,我们将不胜感激!

我尝试使用 SO 上另一个答案中的以下代码,但我可以让它返回任何东西。没有错误,没有结果,什么都没有。我知道即使它确实有效,也只会给我第一个文件,我需要完整的文件选择..

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]);
}

编辑

我在执行脚本之前打印出错误字典,这就是没有错误的原因。这是我在查找器窗口中选择两个文件时得到的结果:

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

谢谢你!舒迈斯

4

2 回答 2

0

更新 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];

这将返回一个数组:

pathArray = ("/Users/Shumais/Desktop/newFolder/testdrop/image1.jpg", "/Users/Shumais/Desktop/newFolder/testdrop/image2.jpg", "/Users/Shumais/Desktop/newFolder/testdrop/image3 .jpg")

我留下了其他代码,因为它可能有用

更新代码。测试和工作。

 NSDictionary* errorMessage = [NSDictionary dictionary];
    NSString *code = @"tell application \"Finder\"\n set theSeletion to (get selection)\n if (count of theSeletion) > 0 then\n return   (container of item 1 of theSeletion as alias)  as string \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]);
    }

返回 --> "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 文件夹”

于 2013-07-23T08:56:11.583 回答
0

看起来容器窗口不起作用。改为尝试父母:

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
于 2013-07-23T09:02:09.640 回答