Skip to content

Instantly share code, notes, and snippets.

@russkociuba
Created November 28, 2016 21:12
Show Gist options
  • Save russkociuba/7020726871cbbacd7640ebda3de025de to your computer and use it in GitHub Desktop.
Save russkociuba/7020726871cbbacd7640ebda3de025de to your computer and use it in GitHub Desktop.
Opens a new iTerm2 window with multiple tabs each executing a command set in the script
# The list of commands to run in each tab of the terminal window
set cmdList to {"ssh user@host1.example.com", "ssh user@host2.example.com"}
# You can append to the list just to break up really long lines
# set cmdList to cmdList & {"ssh user@host3.example.com"}
tell application "iTerm2"
# Not sure how to create an "empty" window so for the window we pick the
# first command in the list
set newWindow to (create window with default profile command item 1 of cmdList)
# Personal preference - adjust as you see fit. You can get fancier
# by getting the desktop size and setting these relative to that if you want
tell newWindow
# bounds are top left X, Y, width, height
set bounds to {75,150,1500,800}
end tell
# Create new tabs for the rest of the commands. If there is no
# item 2+ this simply doesn't do anything (i.e. it doesn't throw an error)
repeat with x from 2 to count of cmdList
tell newWindow
create tab with default profile command item x of cmdList
end tell
end repeat
end tell
@russkociuba
Copy link
Author

Usage

Copy the script to your ~/Library/Application Support/iTerm/Scripts directory (you may have to create this) and give it a meaningful name (e.g dev_servers.scpt). As stated in the iTerm2 documentation the script must end with either .scpt or .app

Change the cmdList list to execute whatever commands you want to run in each tab. The example script is setup to ssh into a different server in each tab.

In the Scripts menu of iTerm2 ( you may have to restart the app if you don't have any other previous scripts) you should see all of your scripts and can execute them from there.

You can also run them manually from the command line
osascript dev_servers.scpt

@ankit167
Copy link

This is super helpful. Thanks! 👍

@erwinrr
Copy link

erwinrr commented Oct 25, 2019

Awesome!

@etiennnr
Copy link

etiennnr commented Apr 25, 2024

I had some weird errors with this script. However, I just it to create a new one that works beautifully

I also enhanced it to also change the session titles

# The list of commands to run in each tab of the terminal window
# You can append to the list just to break up really long lines
set cmdList to {"cd ~/Documents"}
set cmdList to cmdList & {"cd ~/Documents/Downloads"}
# ...

set titleList to {"Documents"}
set titleList to titleList & {"Download"}
# ...

tell application "iTerm2"
  tell current window
    set firstCmd to item 1 of cmdList
    set firstTitle to item 1 of titleList
    tell current session to write text firstCmd
    tell current session to set name to firstTitle
    repeat with x from 2 to count of cmdList
      set currentTitle to item x of titleList
      set currentCmd to item x of cmdList
      set myNewTab to create tab with default profile
      tell current session of myNewTab to write text currentCmd
      tell current session to set name to currentTitle
    end repeat
  end tell
end tell

Just make sure to manually set the Title (in profile settings -> general) to only Name and uncheck Applications in terminal may change the title

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment