简体   繁体   中英

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.

It seems that Finder always prefers to activate an existing window sharing the same directory; in some cases it will even re-navigate an existing window to highlight the requested path.

I've tried NSWorkspace.shared.selectFile() and NSWorkspace.shared.open() , as well as running open via shell script. All have the same effect.

Is there a way to force Finder to open new windows - even with duplicate paths?

I tried this (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.

Just call this:

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

In Swift it should be this:

NSWorkspace.shared.activateFileViewerSelecting([url]) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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