
async def main ( connection ): # Get a reference to the iterm2.App object - a singleton that provides access to iTerm2’s windows, # and in turn their tabs and sessions. This is required because # iTerm2 communicates with the script over a websocket connection, # any time the script sends/receives info from iterm2, it has to wait for a few milliseconds.

I've added a few comments to explain what each part does: #!/usr/bin/env python3.7 # Import the iterm2 python module to provide an interface for communicating with iTerm import iterm2 # All the script logic goes in the main function # `connection` holds the link to a running iTerm2 process # `async` indicates that this function can be interrupted. This will open the script in an editor with a basic skeleton already filled in. Next choose the Basic environment (Full Environment is only for more complex scripts that need to install other packages, that won't be necessary for simply opening tabs and running commands):Ĭhoose Simple script template (because the script we will write will simply run its commands and then exit, it doesn't need to stay open listening for iTerm events):įinally, give your script a name to save it, I name mine daily-startup.py but you can choose any name you like: If prompted, go ahead and download the Python runtime: To get started, make sure iTerm is open, then select from the menu: Scripts -> Manage -> New Python Script as shown below:


#Iterm open tab in same directory how to#
This post will walk you through how to achieve this automation using iTerm and Python. Do you find yourself opening many iTerm tabs every day to do the same daily routine such as opening editors, running a build, starting up various services etc? If yes, good news, this tedious daily startup routine can be automated, saving you precious minutes each day.
