Open new terminal tab and execute script

5

2

I need to merge two osascript scripts, so that with one command I can open a new terminal tab and execute a script like echo hello into that new tab.

This one opens a new terminal tab.

osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'

This one opens a new window and runs echo hello

osascript -e 'tell app "Terminal" do script "echo hello" end tell'

ThomasReggi

Posted 2012-08-26T17:49:08.947

Reputation: 443

Answers

0

The easiest way is by using "ttab"

https://www.npmjs.com/package/ttab

Just run ttab 'ls -la' here ls -la is the command that I want to execute in a new tab.

Note: ttab needs the installation of NPM

Raheel

Posted 2012-08-26T17:49:08.947

Reputation: 178

10

I don't know any better way to make a new tab, but you can run a command in the frontmost tab or window with do script "" in window 1.

tell application "Terminal"
    activate
    tell application "System Events" to keystroke "t" using command down
    repeat while contents of selected tab of window 1 starts with linefeed
        delay 0.01
    end repeat
    do script "echo a" in window 1
end tell

Lri

Posted 2012-08-26T17:49:08.947

Reputation: 34 501

Do you know if there's a way besides using the do script command? Want to run command using Hyper Terminal which doesn't support that syntax – Borat.sagdiyev – 2017-06-15T18:25:43.323

1

Here is a snippet we use. I would suggest:

  • Put this into a function, like one called "tab"
  • for the command use a variable that you pass into the function

    osascript 
        -e "tell application \"Terminal\" to activate" \
            -e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
            -e "do script \"echo hello\" in front window" \
        -e "end tell"
        > /dev/null
    

bbezanson

Posted 2012-08-26T17:49:08.947

Reputation: 11

0

Similar to adayzdone's answer, but slightly different:

osascript -e 'tell application "Terminal" to activate' \
  -e 'tell application "System Events" to keystroke "t" using {command down}' \
  -e 'tell application "Terminal" to do script "echo hello" in front window'

Hauke Hell

Posted 2012-08-26T17:49:08.947

Reputation: 101

0

Try

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 tab 2 of window 1'

adayzdone

Posted 2012-08-26T17:49:08.947

Reputation: 592