How to open a Finder window even if one is open on a different space already, in Terminal

finderspacesterminal

I often use open . to open the current folder from Terminal.app. From time to time this doesn't work. The reason is that the folder in question is already open in some other Finder window on some other Space.

Hunting down this Finder window by searching all spaces takes too long. Neither do I really want to go a different space. I just want to open a new Finder window right there on the current space, regardless of other Finder windows open anywhere else.

I tried open -n . but this just hangs.

Best Answer

You might use a script like this:

#!/usr/bin/osascript

tell application "Finder"
    make new Finder window to POSIX file (system attribute "PWD")
    activate
end tell

The script above always creates a new window. This wouldn't create a new window if a window with the same name already exists on the current space:

#!/usr/bin/osascript

launch application "Finder"
set pwd to system attribute "PWD"
set text item delimiters to "/"
tell application "System Events" to tell process "Finder"
    set w to windows where name is text item -1 of pwd
    if w is {} then
        tell application "Finder" to make new Finder window to POSIX file pwd
    else
        perform action "AXRaise" of item 1 of w
    end if
    set frontmost to true
end tell

System Events only includes windows on the current space.