-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_switch_theme.py
79 lines (56 loc) · 2.17 KB
/
auto_switch_theme.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
import asyncio
import iterm2
THEME_LIGHT = "Tango Light"
THEME_DARK = "Tango Dark"
'''
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
'''
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")