Search code examples
macostabsterminal

Open new Terminal Tab from command line (Mac OS X)


Is it possible to open a new tab in Mac OS X's terminal from the command line in a currently opened tab?

I know that the keyboard shortcut to open a new tab in Terminal is "CMD+t" but I am looking for a script-based solution executed in the command line.


Solution

  • Try this:

    osascript -e 'tell application "Terminal" to activate' \
      -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' \
      -e 'tell application "Terminal" to do script "echo hello" in selected tab of the front window'
    

    But if you need to run dynamic command, set myCommand variable, and replace last line with:

    -e "tell application \"Terminal\" to do script \"${myCommand};\" in selected tab of the front window";