Last active
April 26, 2020 16:28
-
-
Save rrroyal/51b633b647fce2956ccedd6e6cefee0d to your computer and use it in GitHub Desktop.
Change iTerm2 colors based on window theme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Change iTerm2 colors based on window theme | |
# rrroyal@github 2020 | |
import asyncio | |
import iterm2 | |
# Change names here | |
themes = { | |
"dark": "Dark Background", | |
"light": "Light Background" | |
} | |
def colorsUnequal(profile_color, preset_color): | |
return (round(profile_color.red) != round(preset_color.red) or | |
round(profile_color.green) != round(preset_color.green) or | |
round(profile_color.blue) != round(preset_color.blue) or | |
round(profile_color.alpha) != round(preset_color.alpha) or | |
profile_color.color_space != preset_color.color_space) | |
def isPresetUsed(profile, preset): | |
for preset_color in preset.values: | |
key = preset_color.key | |
profile_color = profile.get_color_with_key(key) | |
if colorsUnequal(profile_color, preset_color): | |
return False | |
return True | |
async def getCurrentTheme(connection, profile): | |
presets = await iterm2.ColorPreset.async_get_list(connection) | |
for preset_name in presets: | |
preset = await iterm2.ColorPreset.async_get(connection, preset_name) | |
if isPresetUsed(profile, preset): | |
return preset_name | |
return None | |
async def setTheme(connection=None, preset=None): | |
# Iterate over profiles | |
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) | |
print("[*] Success!\n") | |
async def main(connection): | |
app = await iterm2.async_get_app(connection) | |
session = app.current_terminal_window.current_tab.current_session | |
profile = await session.async_get_profile() | |
currentTheme = await getCurrentTheme(connection, profile) | |
effectiveTheme = (await app.async_get_variable("effectiveTheme")).split(" ")[0] | |
print("[!] Started!") | |
print("[*] Current theme: \"%s\"" % currentTheme) | |
print("[*] Effective theme: %s (\"%s\")" % (effectiveTheme, themes[effectiveTheme])) | |
print("[*] Themes: %s" % themes) | |
print("[*] Monitoring...\n") | |
# Check on startup | |
if (currentTheme != themes[effectiveTheme]): | |
print("[!] Themes mismatch: %s != %s " % (currentTheme, themes[effectiveTheme])) | |
print("[!] Changing current theme to \"%s\"..." % themes[effectiveTheme]) | |
preset = await iterm2.ColorPreset.async_get(connection, themes[effectiveTheme]) | |
await setTheme(connection, preset) | |
# Monitor effectiveTheme changes | |
async with iterm2.VariableMonitor(connection, iterm2.VariableScopes.APP, "effectiveTheme", None) as mon: | |
while True: | |
# Block until theme changes | |
mode = await mon.async_get() | |
print("[!] Refreshed! Currect mode: %s" % mode) | |
# Themes have space-delimited attributes, one of which will be light or dark. | |
parts = mode.split(" ") | |
if "dark" in parts: | |
preset = await iterm2.ColorPreset.async_get(connection, themes["dark"]) | |
print("[!] Setting dark preset: \"%s\"" % themes["dark"]) | |
else: | |
preset = await iterm2.ColorPreset.async_get(connection, themes["light"]) | |
print("[!] Setting light preset: \"%s\"" % themes["light"]) | |
# Set color | |
await setTheme(connection, preset=preset) | |
iterm2.run_forever(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment