How to open multiple terminal tabs in same window via apple script or bash [duplicate]

I want to be able to open the terminal application and have it either automatically run a script that opens several tabs and run an arbitrary set of commands in each. For example I would like one tab to open, change directories, and run a rails server; have another open, change directories, and tail a log file; etc..

Is there a way to do this either with a shell script or applescript?

I found this question but it seems to hang if I want to open tabs with commands that don't end (like running the rails server).


Solution 1:

This is a little bit hacky, but you can achieve this in AppleScript. If there is a predetermined number of tabs you want, and preset commands you wish to run, this isn't difficult.

tell application "Terminal"
    -- Activate it.
    activate

    set targetWindow to 0

    -- Get a window that's not busy.
    -- If there are no open windows, open one.
    if count of windows is greater than 0 then
        repeat with currentWindow in windows
            if currentWindow is not busy then
                set targetWindow to currentWindow
            end if
        end repeat
    else
        do script ""
        set targetWindow to window 1
    end if

    -- Do command 1.
    set firstCommand to "cd ~/Desktop; clear"
    do script firstCommand in targetWindow

    -- Open a new tab.
    tell application "System Events" to tell process "Terminal" to keystroke "t" using command down

    if count of windows is greater than 0 then
        repeat with currentWindow in windows
            if currentWindow is not busy then
                set targetWindow to currentWindow
            end if
        end repeat
    else
        do script ""
        set targetWindow to window 1
    end if

    -- Do command 2.
    set secondCommand to "cd /Applications; clear"
    do script secondCommand in targetWindow

    -- And so on...
end tell

Of course, replace firstCommand with whichever command you actually want to run, and so on. For whatever reason, Terminal doesn't really have an accessible way to create new tabs through AppleScript, so the long, hacky looking line in the middle just tells Terminal to type T to open that new tab, and then new commands will execute in it.

You can run this AppleScript as is, or use it in Automator to create a new service, which you can then execute from anywhere using a keyboard shortcut if you'd like.

Extras - If you want to fire some script/command in the newly opened terminal session, you can refer this

Solution 2:

Here is a shell script that will do what you want - for both Apple's Terminal or iTerm (we have users of both).

For your tabs that you open, this will execute another batch file in each tab, such as one for tomcat, one for you db, etc.

#!/bin/bash

function tab () {
    local cmd=""
    local cdto="$PWD"
    local args="$@"

    if [ -d "$1" ]; then
        cdto=`cd "$1"; pwd`
        args="${@:2}"
    fi

    if [ -n "$args" ]; then
        cmd="; $args"
    fi

    if [ $TERM_PROGRAM = "Apple_Terminal" ]; then
        osascript 
            -e "tell application \"Terminal\"" \
                -e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
                -e "do script \"cd $cdto; clear $cmd\" in front window" \
            -e "end tell"
            > /dev/null
    elif [ $TERM_PROGRAM = "iTerm.app" ]; then
        osascript
            -e "tell application \"iTerm\"" \
                -e "tell current terminal" \
                    -e "launch session \"Default Session\"" \
                    -e "tell the last session" \
                        -e "write text \"cd \"$cdto\"$cmd\"" \
                    -e "end tell" \
                -e "end tell" \
            -e "end tell" \
            > /dev/null
    fi
}

tab path_to_script1 sh script1
tab path_to_script2 sh script2
tab path_to_script3 sh script3