Search code examples
macosfinder

is it possible to open a folder in Finder in the *current* space from the command line?


i want to issue a terminal command (invoking AppleScript is fine) to open a path in a Finder window in the current space.

if the path is open in Finder in another space, i don't want to switch to that space or move that finder window to the current space. if the path is already open in a Finder window in the current space, either focusing on it or opening a new Finder window to it would be fine.

i run a lot of spaces that are setup for different tasks, and i don't want to remove the Finder window from any other space i might be using it or switch to any other space.


Solution

  • In Applescript, for example opening the Applications folder: -

    tell application "Finder"
        make new Finder window to folder "Applications" of startup disk
    end tell
    

    You can save that as a script and call it from the command line with the osascript command, or you can execute it directly with the -e argument to osascript: -

    osascript -e 'tell application "Finder" to make new Finder window to folder "Applications" of startup disk'
    

    If you would like to open a specific path, instead of a named folder, you can reference the path with this:-

    POSIX file "/some/directory/path"
    

    So, a complete script would be: -

    tell application "Finder"
        activate
        make new Finder window to (POSIX file "/some/directory/path")
    end tell
    

    Note: the activate command focuses the Finder window, which is optional.