【问题标题】:Force a *new* Finder window to open programmatically强制 *new* Finder 窗口以编程方式打开
【发布时间】:2021-07-20 10:50:35
【问题描述】:

我正在实现“在 Finder 中显示”功能,但重要的是要始终显示一个 新的 Finder 窗口 - 无论哪些窗口已经打开。

Finder 似乎总是更喜欢激活共享同一目录的现有窗口;在某些情况下,它甚至会重新导航现有窗口以突出显示请求的路径。

我尝试过NSWorkspace.shared.selectFile()NSWorkspace.shared.open(),以及通过shell 脚本运行open。都是一样的效果。

有没有办法强制 Finder 打开新窗口——即使路径重复?

【问题讨论】:

  • 使用 AppleScript 告诉 Finder 创建一个新的 Finder 窗口?
  • @matt 有同样的行为。奇怪的是,在我的机器上,它会为 ~/Documents 创建无数个窗口,但对于其他每个路径,它只会创建一个。
  • 你不能在 macOS 中这样做。
  • AppleScript 是否创建窗口?
  • 也许你会在这里找到一些有用的东西:stackoverflow.com/questions/7652928/…

标签: swift objective-c macos cocoa appkit


【解决方案1】:

我试过这个(AppleScript):

tell application "Finder"
    set f to document file "eStmt_2021-01-14.pdf" of folder "Downloads" of folder "mattmobile" of folder "Users" of startup disk
    set fol to folder "Downloads" of folder "mattmobile" of folder "Users" of startup disk
    activate
    set w to make new Finder window
    set target of w to fol
    select f
end tell

工作正常:每次运行时,它都会创建一个窗口,并在该窗口中显示和选择目标文件。

【讨论】:

  • 这很好用;我曾希望找到一个“原生”AppKit API,但它似乎不存在,所以这是下一个最好的事情。
  • 为此使用 Apple Script 将在 macOS 10.14 及更高版本上弹出一次性安全对话框提示。
【解决方案2】:

就这样称呼吧:

[[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs:@[url]];

在 Swift 中应该是这样的:

NSWorkspace.shared.activateFileViewerSelecting([url]) 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    相关资源
    最近更新 更多