Skip to content

Instantly share code, notes, and snippets.

@f-ilic
Forked from klamouri/README.md
Created February 22, 2024 02:04
Show Gist options
  • Save f-ilic/ae85b0373160268bab83abc6fc76a8df to your computer and use it in GitHub Desktop.
Save f-ilic/ae85b0373160268bab83abc6fc76a8df to your computer and use it in GitHub Desktop.
Iterm2 Auto Dark/Light mode

Most of it is taken from this gist. I just tweaked the script to check the right theme at startup in case the theme change occured when iTerm2 was closed.

Make sure the theme is the same as the themes you have installed (Preferences > Profiles > Colors > Color Presets...). I personally use Solarized Dark and Solarized Light

From someone on the linked gist:

First-time installation steps for me:

  1. Download the script from GitHub by right-clicking on Raw button and save as...
  2. copy the script to $HOME/Library/Application Support/iTerm2/Scripts/AutoLaunch
  3. create AutoLaunch folder if it does not exist
  4. go to iTerm2 > Scripts > AutoLaunch
  5. You will be prompted to download the Python runtime, do it.
  6. Switch the dark/light mode in macOS, iTerm2 should change color if your script does not work after you restart iTerm2, make sure that Preferences -> Magic -> Enable Python API is checked and this setting is saved
#!/usr/bin/env python3.7
import asyncio
import iterm2
import logging
async def main(connection):
logger = logging.getLogger('macos_theme_sync')
app = await iterm2.async_get_app(connection)
window = app.current_window
initial_theme = await app.async_get_theme()
initial_parts = initial_theme[0].split(" ")
if "dark" in initial_parts:
initial_preset = await iterm2.ColorPreset.async_get(connection, "Solarized Dark")
logger.info('Initial theme is dark')
else:
initial_preset = await iterm2.ColorPreset.async_get(connection, "Solarized Light")
logger.info('Initial theme is light')
# Update the list of all profiles and iterate over them.
initial_profiles=await iterm2.PartialProfile.async_query(connection)
for partial in initial_profiles:
# Fetch the full profile and then set the color preset in it.
initial_profile = await partial.async_get_full_profile()
await initial_profile.async_set_color_preset(initial_preset)
async with iterm2.VariableMonitor(connection, iterm2.VariableScopes.APP, "effectiveTheme", None) as mon:
while True:
# Block until theme changes
theme = await mon.async_get()
print(theme)
# 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, "Solarized Light")
# 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment