Skip to content

Instantly share code, notes, and snippets.

@kaskavalci
Created June 22, 2025 06:42
Show Gist options
  • Save kaskavalci/f9bc1dd6a7f67e2daf5ff1e82d9b5345 to your computer and use it in GitHub Desktop.
Save kaskavalci/f9bc1dd6a7f67e2daf5ff1e82d9b5345 to your computer and use it in GitHub Desktop.
iterm2 profile color switcher
#!/usr/bin/env python3
# Go to Scripts > Manage > New Python Script.
# Install python runtime
# Save the script below.
# Change presets if needed.
# Debug by Scripts > Manage > Console
import asyncio
import iterm2
async def main(connection):
app = await iterm2.async_get_app(connection)
# The names of your desired light and dark color presets.
# Make sure these match the names in iTerm2 exactly.
light_preset_name = "Light Background"
dark_preset_name = "Dark Background"
async def set_profile_colors(theme):
if "dark" in theme:
preset = await iterm2.ColorPreset.async_get(connection, dark_preset_name)
else:
preset = await iterm2.ColorPreset.async_get(connection, light_preset_name)
# Get all profiles and update their color presets.
profiles = await iterm2.PartialProfile.async_query(connection)
for partial in profiles:
await partial.async_set_color_preset(preset)
# Set the initial theme when the script starts.
await set_profile_colors(await app.async_get_theme())
# Monitor for theme changes and update accordingly.
async with iterm2.VariableMonitor(connection, iterm2.VariableScopes.APP, "effectiveTheme", None) as mon:
while True:
theme = await mon.async_get()
await set_profile_colors(theme)
if __name__ == "__main__":
iterm2.run_forever(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment