A simple python script for setting the theme of iterm2 from where ever you want to invoke it.
You will need to allow third party applications to talk to the iterm api. This can be done with cookies and security BUT the api is horribly documented thus there is no security here (Smile and wave boys). To do this go to
Preferences -> General -> Magic -> Toggle "Enable Python API"
-
iTerm2 3.3.10 (because macOS and python 2.7 rip)
-
python3
-
pip3
git clone https://github.com/brorbw/iterm2-theme-toggle.git [preferred location]
cd /where/you/installed/iterm2-theme-toggle
pipenv install
I have a line like this in my .zshrc but you can invoke the script with hammerspoon or whatever is to your liking.
function toggle-theme() {
python3 ~/<install-path>/main.py "theme-name"
}
If you are experiencing issues like 'iTerm2' module not found
, you can try the following solution
function toggle-theme() {
BAK_PIPFILE=$PIPENV_PIPFILE
(PIPENV_PIPFILE=~/Projects/iterm2-theme-toggle/Pipfile exec pipenv run python3 ~/Projects/iterm2-theme-toggle/main.py "theme-name")
PIPENV_PIPFILE=$BAK_PIPFILE
disown
}
First time you run the script iTerm2 will prompt you for permissions to you the API. Press always, if you don't want to allow it every time
Restart iTerm2 and you are good to go
If you need to change the themes that it toggles between you just need to set the environment variables