Python Scripting In iTerm2
5 min readLately I’ve been working on personal projects that require A LOT of different services to be running at the same time for the development environment on my Mac. It became very tedious to open my terminal to the project working directory and run each script individually and manually opening a new tab/session for each script. I mean when you get past doing that 4 times how much time do you lose every time you have to restart all those scripts
I did some surfing on the inter tubes and found out that I could automate this using some internal features with iTerm2. One thing I quickly realized was all the information I was finding online was out of date so I would have to figure out how to use the new methods on my own. iTerm2 deprecated the use of AppleScript as the solution for this problem; however that has been deprecated in lieu of a Python Scripting API.
Going through the GUI Setup
iTerm makes this pretty easy at the beginning simply go to:
MenuBar > Scripts > Manage > New Python Script
You will be presented with two prompts and you will want to chose “Basic” and then “Simple” for what we are trying to achieve today. You can get hella fancy on your own time.
Into Python Land
The bootstrapped file will look similar to the below:
Your use case here might deviate from this post, but for now let’s say you have 4 scripts that you will need to run to get your application up and running. We can create a list of tuples to execute on in our main function with an API like so:
The Python for this is really not that difficult what was crazy difficult was understanding the API and getting this to work…some what as I would want it too.
Executing the Script
iTerm references a few different ways to execute our script here. I could never get the auto load method to work which kind of pissed me off because I wanted to have it execute when I changed profiles in the terminal. I ended up opting for aliasing the ridiculously long paths/command in my .zshrc
for faster usage. You could go as far as having the profile call the alias when switching to that profile if you wanted too.
The Problems I Encountered
Understanding what a Window, Tab, and Session are in the API is not hard by any means. What was insanely difficult was:
- Debugging the script.
- Understanding what methods on the classes/instances worked and under what circumstances.
There is a way to execute split_panes
in iTerm. This overall is not difficult to run either. The reason I opted not to do this was I needed to run 11 scripts. You can open, I am pretty certain, an unlimited amount of panes in a Session. However you need to tell iTerm how you want to open those panes and doing so inside my for
loop was getting way to damn confusing. Maybe in the future I will clean this up to present 6 panes in one tab and 5 in another for faster viewing.
Wrap Up
I hope this code helps someone out. Feel free to extend it or use it as a template as needed. This is still kind of a work-in-progress. I’d like to do more with it. Possibly make it more generic and a function I could execute from a project workspace that would take in the scripts and execute them without me needing to define them manually.
Related Articles
A Far Too In-Depth Guide To SSH For Web Developers
Everything I have learned when it comes to SSH after setting up my Raspberry Pi Cluster.
Migrating From Yarn To Pnpm
My experience moving from classic yarn to pnpm as my package manager in JavaScript land.
How To Kill Processes On MacOS
A quick posts on how to kill processes on macOS.
Writing A Connect Four Game Reader In Python
Writing a program that will read moves and populate a game board for the popular game Connect Four using Python.
Recursion & Memoization In Python
Working through Fibonacci using recursion and memoization in Python.
Getting Started With Python3
Installation and setup of Python3 on macOS & development with Visual Studio Code.
Building A Black Jack Advice Generator With Python
How to build a simple black jack advice generator using Python.
Up & Running With iTerm & Zsh
How to get started with iTerm2, ZSH, & VSCode.
Deploying With Now
How to deploy with Zeit's Now platform.
Cody is a Christian, USN Veteran, Jayhawk, and an American expat living outside of Bogotá, Colombia. He is currently looking for new opportunities in the tech industry.