Iterm2 AppleScript: Opening Iterm2 with multiple named tabs

I always open multiple tab to launch server, mysql shell, ipython shell etc. Every time, setting the virtualenv etc in the shell and start server etc is a pain, which resulted in the following iterm2 applescript.

The following iterm2 applescript helps to launch the multiple tabs in iterm2 . And you can name the tabs. In addition to that, You can launch the tab with some start commands too.

tell application "iTerm"
    activate
    set startCmdList to {"renametab 'tab1' && ls -l", "renametab 'tab2' && ls"} as list
        -- probably you can add default commands to be executed
    set defaultcmd to "cd ~ &&"
    -- Create a new terminal window...
    set myterm to (make new terminal)

    tell myterm
        set number of columns to 150
        set number of rows to 30

        repeat with cmd in startCmdList
            launch session "Default"
            tell current session
                write text defaultcmd & cmd
                --set name to cmd
            end tell
        end repeat
    end tell

end tell

iterm2 “renametab” alias:

Add the following snippet in your ~/.bash_profile file. It add the function to rename your tab.

function renametab () {
    echo -ne "\033]0;"$@"\007"
}

If the renaming tab does not work, check whether your .bash_profile modifies the PS1 variable. You might need to comment out the following snippet.

#case "$TERM" in
#xterm*|rxvt*)
#    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
#    ;;
#*)
#    ;;
#esac

Installing the iterm2 applescript as an application

  • Open “AppleScript Editor” (/Applications/Utilities)
  • In a new document, paste the AppleScript code from the above snippet
  • Change the icon by opening bundle contents, and replacing with new applet.icns
  • Export the script to /Applications/Utilities, use the file format “Application” (do not select “run only” if you want to be able to change the script later on)
This entry was posted in Mac OS and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *