Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Iterm2 Python Script to match MacOS System Theme
#!/usr/bin/env python3
# To Use: In iterm2, select Manage -> New Python Script, Basic script, daemon mode. Install python runtime if necessary and then paste in script, replace my themes with your chosen light and dark themes, save. In Scripts, uncheck and recheck your new script.
import asyncio
import iterm2
from subprocess import run
from time import sleep
async def main(connection):
while True:
# Block until theme changes
p = run(['defaults', 'read', '-g', 'AppleInterfaceStyle'])
# Themes have space-delimited attributes, one of which will be light or dark.
if p.returncode == 0:
preset = await iterm2.ColorPreset.async_get(connection, "Night Owl")
preset = await iterm2.ColorPreset.async_get(connection, "Solarized Light")
# Update the list of all profiles and iterate over them.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment