Skip to content

Instantly share code, notes, and snippets.

@jamesmacfie
Created October 22, 2019 02:53
Star You must be signed in to star a gist
Save jamesmacfie/2061023e5365e8b6bfbbc20792ac90f8 to your computer and use it in GitHub Desktop.
iTerm 2 - script to change theme depending on Mac OS dark mode

How to use

In iTerm2, in the menu bar go to Scripts > Manage > New Python Script

Select Basic. Select Long-Running Daemon

Give the script a decent name (I chose auto_dark_mode.py)

Save and open the script in your editor of choice.

Copy and paste the script below and save

Go back to iTerm2, go to Scripts in the menu bar and select the script you just saved.

Try toggling Dark mode to see what happens! Reminder it's under Appearance in the System Settings

Changing what themes this uses

Note in the script below on lines 15 and 17 that we have a string that we're passing to iTerm2. Change these to whatever you like. The text is whatever appears in the dropdown in the iTerm2 settings under Profile/Color for when you'd want to change the theme manually.

After you change the script you'll have to stop and start the script if it's running already

#!/usr/bin/env python3
import asyncio
import iterm2
async def main(connection):
async with iterm2.VariableMonitor(connection, iterm2.VariableScopes.APP, "effectiveTheme", None) as mon:
while True:
# Block until theme changes
theme = await mon.async_get()
# Themes have space-delimited attributes, one of which will be light or dark.
parts = theme.split(" ")
if "dark" in parts:
preset = await iterm2.ColorPreset.async_get(connection, "Solarized Dark")
else:
preset = await iterm2.ColorPreset.async_get(connection, "Light Background")
# Update the list of all profiles and iterate over them.
profiles=await iterm2.PartialProfile.async_query(connection)
for partial in profiles:
# Fetch the full profile and then set the color preset in it.
profile = await partial.async_get_full_profile()
await profile.async_set_color_preset(preset)
iterm2.run_forever(main)
@gnachman
Copy link

@fabianSorn Go get the beta and give it a try and report any bugs you find. I'll release it when there are no more significant bugs :)

@Kamik423
Copy link

I have been using it since the day it was released. So far I have had no bugs. Seems rock solid to me, instant response; beautiful.

@doublethefish
Copy link

Thanks for this. It didn't update my iTerm colors immediately, so I've created a version that updates all Sessions and Profiles.

I don't use Profiles much so my version makes all terminal sessions update immediately.

It also has a bit more error handling and documentation.

Hope it helps someone:
https://gist.github.com/doublethefish/c339b659738fdb652e820e3b40f97f36

@fredrikaverpil
Copy link

Does auto-switching between light/dark themes (based on OS setting) still require beta or nightly?

@tshu-w
Copy link

tshu-w commented Jun 17, 2022

Does auto-switching between light/dark themes (based on OS setting) still require beta or nightly?

Not anymore.

@bartekpacia
Copy link

bartekpacia commented Jun 23, 2022

Does auto-switching between light/dark themes (based on OS setting) still require beta or nightly?

Not anymore.

Do you mean that this feature is available on stable? I can't find it.

@tshu-w
Copy link

tshu-w commented Jun 23, 2022

Does auto-switching between light/dark themes (based on OS setting) still require beta or nightly?

Not anymore.

Do you mean that this feature is available on stable? I can't find it.

Yes, I'm using iTerm2 Build 3.4.15 download by homebrew (not iterm-beta cask).

Update:
Sorry I may have misunderstood, snippets is working fine, "separate light/dark mode color settings." is not there yet.

@gnachman
Copy link

No, not on stable; only in beta.

@petrmvala
Copy link

This is great! Does iTerm export some env variable indicating in which mode it is? I'm asking because I use bat for browsing code and want to set syntax highlight based on the current background.

Thanks!

@majutsushi
Copy link

Since the mode is based on the system-wide mode you can use defaults read -g AppleInterfaceStyle to determine if dark mode is enabled. The command will return Dark when in dark mode and nothing when not in dark mode.

@fosemberg
Copy link

fosemberg commented Sep 14, 2022

If you want the script to run itself when opening iterm2, you need to put it in the folder, that is not exist by default:
~/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch

mkdir -p ~/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch
mv ~/Library/ApplicationSupport/iTerm2/Scripts/auto_dark_mode.py ~/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch/auto_dark_mode.py

https://iterm2.com/python-api/tutorial/running.html#auto-run-scripts

@lpolon
Copy link

lpolon commented Dec 4, 2023

Thank you!

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