简体   繁体   English

强制 *new* Finder window 以编程方式打开

[英]Force a *new* Finder window to open programmatically

I'm implementing a "show in Finder" feature, but it's important that a new Finder window should always appear - regardless of which windows are already open.我正在实现“在 Finder 中显示”功能,但重要的是的 Finder window 应该始终出现 - 无论哪个 windows 已经打开。

It seems that Finder always prefers to activate an existing window sharing the same directory;似乎 Finder 总是更喜欢激活共享同一目录的现有 window; in some cases it will even re-navigate an existing window to highlight the requested path.在某些情况下,它甚至会重新导航现有的 window 以突出显示请求的路径。

I've tried NSWorkspace.shared.selectFile() and NSWorkspace.shared.open() , as well as running open via shell script.我试过NSWorkspace.shared.selectFile()NSWorkspace.shared.open() ,以及通过 shell 脚本运行open All have the same effect.都有相同的效果。

Is there a way to force Finder to open new windows - even with duplicate paths?有没有办法强制 Finder 打开新的 windows - 即使路径重复?

I tried this (AppleScript):我试过这个(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

Works fine: every time it runs, it creates a new window and shows and selects the target file in that window.工作正常:每次运行时,它都会创建一个的 window 并显示并选择 window 中的目标文件。

Just call this:只需调用这个:

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

In Swift it should be this:在 Swift 中应该是这样的:

NSWorkspace.shared.activateFileViewerSelecting([url]) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM