Skip to content

Instantly share code, notes, and snippets.

@artcommacode
Forked from plivox/auto_switch_theme.py
Last active August 10, 2021 10:47
Show Gist options
  • Save artcommacode/baa6e6ad416b80394a3b2b5de650b3d6 to your computer and use it in GitHub Desktop.
Save artcommacode/baa6e6ad416b80394a3b2b5de650b3d6 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 = "serendipity-light-iTerm2"
THEME_DARK = "serendipity-dark-iTerm2"
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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment