Skip to content

Instantly share code, notes, and snippets.

@saagarjha
Last active October 25, 2021 16:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save saagarjha/9a99ed4bc68a37bab097cc6ee83a75ab to your computer and use it in GitHub Desktop.
Save saagarjha/9a99ed4bc68a37bab097cc6ee83a75ab to your computer and use it in GitHub Desktop.
iTerm2 Script to change the theme automatically based on the system appearance
#!/usr/bin/env python3
import iterm2
async def set_theme(connection, 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, "Fixed Solarized Dark")
else:
preset = await iterm2.ColorPreset.async_get(connection, "Fixed Solarized Light")
# Update the list of all profiles and iterate over them.
profiles = await iterm2.PartialProfile.async_get(connection)
for profile in profiles:
await profile.async_set_color_preset(preset)
async def main(connection):
app = await iterm2.async_get_app(connection)
theme = await app.async_get_variable("effectiveTheme")
await set_theme(connection, theme)
async with iterm2.VariableMonitor(connection, iterm2.VariableScopes.APP, "effectiveTheme", None) as mon:
while True:
# Block until theme changes
theme = await mon.async_get()
await set_theme(connection, theme)
iterm2.run_forever(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment