Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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'
share|improve this question
up vote 5 down vote accepted

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
share|improve this answer

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
    
share|improve this answer

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'
share|improve this answer

You must log in to answer this question.

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