Search code examples
swiftobjective-cmacoscocoaappkit

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?


Solution

  • 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.