5

I'm wondering if there is a means of opening a new finder tab (not window) from the command line. I can do, e.g.,

open .

but this opens a new window. It would be nice to open a new tab in the most-recently active existing finder window.

  • Welcome to Ask Different :) Can you please open Finder preferences and check "Open folders in tabs instead of new windows" under General tab is enabled. – Nimesh Neema Jan 22 at 18:04
  • Yes, it is enabled. – msailor Jan 22 at 19:25
2

You really can't via any “normal” shell commands, but you can run AppleScript via the shell to accomplish the task.

Here's a Bash function with the command fnt for “Finder New Tab”:

function fnt()
{
    osascript -e 'tell application "System Events" to perform action "AXPress" of menu item "New Tab" of menu "File" of menu bar item "File" of menu bar 1 of application process "Finder"'
}

Add the above bash function to your ~/.bash_profile or ̃/.profile file to use it. After adding, restart Terminal or use the source command in the current Terminal window to have access to the fnt function.

This should work on any version macOS that has the “New Tab” command in the “File” menu. You'll also need to add Terminal to: System Preferences > Security & Privacy > Privacy Accessibility

I understand some improvement to AppleScript has been made in Mojave relative to tabs in the Finder, but I'm still using macOS Sierra 10.12.6 and can't test.

  • Sadly, this doesn't seem to be working for me. When I run the command, it opens a new Finder window, at the default starting directory. (It also prints out action AXPress of menu item New Tab of menu File of menu bar item File of menu bar 1 of application process Finder). – msailor Jan 30 at 18:51
  • The first issue is odd – what version of macOS are you using? – Christopher Stone Jan 31 at 3:39
  • The second issue can be eliminated by appending ' 1> /dev/null' to the command in the function. – Christopher Stone Jan 31 at 3:51
  • I'm on 10.12.6. – msailor Jan 31 at 18:50
  • If you haven't added the Terminal.app to System Preferences > Security & Privacy > Privacy > Accessibility try doing that and see if it makes a difference. – Christopher Stone Feb 1 at 20:24
0

Here is the solution I came up with, based on the answer at How do you duplicate current open Finder view in new tab (Mavericks)?

    function newtab()
{
osascript -e "tell application \"Finder\"
    activate
    set t to target of Finder window 1
    set toolbar visible of window 1 to true
end tell
tell application \"System Events\"
    keystroke \"t\" using command down
end tell
tell application \"Finder\"
    set target of Finder window 1 to POSIX file \"`pwd`\"
end tell" 1> /dev/null
}

All the escaped quotes isn't very elegant, but seemed to be necessary to allow the command substitution.

Thanks to Christopher Stone for pointing me in the direction of AppleScript! Unfortunately his solution did not work for me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .