Skip to content

Instantly share code, notes, and snippets.

@plivox
Last active March 25, 2024 04:03
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save plivox/c5297dd8b350baa582895684bfb0aded to your computer and use it in GitHub Desktop.
Save plivox/c5297dd8b350baa582895684bfb0aded to your computer and use it in GitHub Desktop.
Automatic iTerm2 preset switching on MacOS
#!/usr/bin/env python3
import asyncio
import iterm2
THEME_LIGHT = "Tango Light"
THEME_DARK = "Tango Dark"
class AutoSwitchTheme:
def __init__(self, connection, light="Light Background", dark="Dark Background"):
self.connection = connection
self.light = light
self.dark = dark
async def get_app(self):
return await iterm2.async_get_app(self.connection)
async def get_theme(self) -> str:
parts = await (await self.get_app()).async_get_theme()
if len(parts) <= 1:
return parts[0]
return ""
async def set_color_preset(self, theme):
preset = await iterm2.ColorPreset.async_get(
self.connection, self.light if theme == "light" else self.dark
)
profiles = await iterm2.PartialProfile.async_query(self.connection)
for partial in profiles:
await (await partial.async_get_full_profile()).async_set_color_preset(
preset
)
async def quit(connection):
while True:
if not connection.websocket.open:
exit(0)
await asyncio.sleep(1)
async def main(connection):
asyncio.ensure_future(quit(connection), loop=asyncio.get_event_loop())
ast = AutoSwitchTheme(connection, THEME_LIGHT, THEME_DARK)
await ast.set_color_preset(await ast.get_theme())
async with iterm2.VariableMonitor(
connection, iterm2.VariableScopes.APP, "effectiveTheme", None
) as mon:
while True:
# Block until theme changes
theme = await mon.async_get()
# Set preset if theme has changed
await ast.set_color_preset(theme)
try:
iterm2.run_forever(main)
except:
print("Unable to connect on iTerm2 application")
@SinisterStairs
Copy link

Thanks @plivox, I've been using your script and it's worked well and, most importantly to me, behavesitself. (The previous auto-dark script I had been using spawned accumulating processes which consumed CPU and couldn't clean up.)

With this script, when dark mode is supposed to be active and I open a new terminal window, I may see a quick flash of white (light mode background) before it switches to black (dark mode); but that's a minor annoyance.

Thanks for the gist!

@plivox
Copy link
Author

plivox commented Jul 7, 2021

@SinisterStairs, with pleasure and thanks for the feedback.

@dasizeman
Copy link

dasizeman commented Aug 3, 2021

@plivox I am having an issue where the script is running if I do it manually through the scripts menu, but even though it is here: $HOME/Library/Application Support/iTerm2/Scripts/AutoLaunch/auto_switch_theme.py - it doesn't seem to be running automatically. Do you have any idea what I might be doing wrong?

EDIT: Solved. You have to check "Enable Python API in Preferences"->General->Magic

@plivox
Copy link
Author

plivox commented Aug 3, 2021

@dasizeman, Yes, you have to activate the Python API in the preferences 👍

@dasizeman
Copy link

Thanks @plivox! Another thing I noticed is that was giving me trouble is that it doesn't seem to work with iTerm's minimal theme (which I really like 😢 ). Do you have any ideas if this is something we could improve on the script side or is it possibly an issue with the API being inconsistent for that theme?

@c-alpha
Copy link

c-alpha commented Aug 10, 2021

Great stuff, and thanks for sharing your work! 🥇 👍

Just a small observation: THEME_LIGHT and THEME_DARK are actually used to select colour presets. What about renaming them to COLOUR_PRESET_LIGHT and COLOUR_PRESET_DARK?

@w33dw0r7d
Copy link

Thanks @plivox! Another thing I noticed is that was giving me trouble is that it doesn't seem to work with iTerm's minimal theme (which I really like 😢 ). Do you have any ideas if this is something we could improve on the script side or is it possibly an issue with the API being inconsistent for that theme?

Same issue. When switch to compact theme all work fine.

@zemahran
Copy link

Thanks for this!

@hamza72x
Copy link

Jazakallahu Khairan 💐❤️

@rnnyrk
Copy link

rnnyrk commented Sep 23, 2021

Thanks for this one!!

@plivox
Copy link
Author

plivox commented Oct 3, 2021

Thank you all for your feedback. As @stefanwascoding writes, the latest beta (3.5) includes separate color settings.

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