2

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?

Hundley
  • 3,167
  • 3
  • 23
  • 45

2 Answers2

1

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • This works well; I had hoped to find a "native" AppKit API, but that doesn't seem to exist so this is the next best thing. – Hundley Apr 28 '21 at 04:42
  • Using Apple Script for this will bring up a one time security dialog prompt on macOS 10.14 and newer. – Stefanf May 04 '21 at 08:45
1

Just call this:

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

In Swift it should be this:

NSWorkspace.shared.activateFileViewerSelecting([url]) 
Stefanf
  • 1,663
  • 13
  • 15