【问题标题】:How to retrieve the list of selected items in Finder?如何在 Finder 中检索选定项目的列表?
【发布时间】:2013-07-20 11:56:56
【问题描述】:

在我的 Cocoa 应用程序中,我试图检索已在 Finder 窗口中选择的文件列表。我找到了以下方法,但是速度慢得让人难以忍受。

FinderApplication * finder =
    [SBApplication applicationWithBundleIdentifier:@"com.apple.finder"];

SBElementArray * selection = [[finder selection] get];

NSArray<NSString*> * items = [selection arrayByApplyingSelector:@selector(URL)];
// now items contains the URLs as strings of all selected items

从时间上讲,最昂贵的电话是

SBElementArray * selection = [[finder selection] get];

当对大量文件进行选择时,该方法会长时间挂起,在处理少量文件时甚至表现不佳。

有没有更好的方法在最前面的 Finder 窗口中获取所有选定文件的 URL?

我需要有文件 URL,以便我可以对这些文件执行操作。

【问题讨论】:

    标签: objective-c macos cocoa finder


    【解决方案1】:

    你可以使用苹果脚本

    tell application "Finder"
        get selection
    end tell  
    

    osascript -e "tell application \"Finder\" to return selection"
    

    【讨论】:

    • 这与我发布的代码相同,只是在脚本中 :) 在我把这个问题放在这里之前,我测试了第一个示例,它具有相同的延迟。该脚本仍会转到 Finder 并运行 Get 命令。在我看来,这就是它,但它仍然很慢。
    • 然后你需要在查找器中注入代码来获取选择的项目。看看 mach Inject。
    猜你喜欢
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 2014-04-10
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 2012-08-21
    相关资源
    最近更新 更多