Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Switch iTerm2 color preset automatic base on macOS dark mode.
  1. Add switch_automatic.py to ~/Library/ApplicationSupport/iTerm2/Scripts/AutoLaunch with:
#!/usr/bin/env python3

import asyncio
import iterm2

async def main(connection):
    async with iterm2.VariableMonitor(connection, iterm2.VariableScopes.APP, "effectiveTheme", None) as mon:
        while True:
            # Block until theme changes
            theme = await mon.async_get()

            # Themes have space-delimited attributes, one of which will be light or dark.
            parts = theme.split(" ")
            if "dark" in parts:
                preset = await iterm2.ColorPreset.async_get(connection, "Dark Background")
            else:
                preset = await iterm2.ColorPreset.async_get(connection, "Light Background")

            # 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)

iterm2.run_forever(main)

Change Dark Background and Light Background to color presets you like.

  1. Enabled switch_automatic.py from Scripts menu.
@jirikrepl

This comment has been minimized.

Copy link

@jirikrepl jirikrepl commented Jun 2, 2020

@iodic

This comment has been minimized.

Copy link

@iodic iodic commented Jan 28, 2021

Thanks a lot! :) Working great for me on MacOS Big Sur and iTerm v3.4.4beta4.

@anezhyvyi

This comment has been minimized.

Copy link

@anezhyvyi anezhyvyi commented Feb 22, 2021

Thank you!

@d9i

This comment has been minimized.

Copy link

@d9i d9i commented Mar 1, 2021

Thank you so much for this!

I noticed that this script doesn't check if iTerm is the correct color when the app is started, meaning that you could get the wrong colors if you quit iTerm2 during the day and opened it at night, after the theme switch.

I added a check on app start to the script to address this:

#!/usr/bin/env python3

import asyncio
import iterm2

async def changeTheme(theme_parts, connection):
    # Themes have space-delimited attributes, one of which will be light or dark.
    if "dark" in theme_parts:
        preset = await iterm2.ColorPreset.async_get(connection, "Dark Background")
    else:
        preset = await iterm2.ColorPreset.async_get(connection, "Light Background")

    # 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)

async def main(connection):
    # Set color scheme correctly at app start
    app = await iterm2.async_get_app(connection)
    parts = await app.async_get_theme()
    await changeTheme(parts, connection)


    async with iterm2.VariableMonitor(connection, iterm2.VariableScopes.APP, "effectiveTheme", None) as mon:
        while True:
            # Block until theme changes
            theme = await mon.async_get()
            parts = theme.split(" ")
            await changeTheme(parts, connection)


iterm2.run_forever(main)

Hope this is helpful!

@FradSer

This comment has been minimized.

Copy link
Owner Author

@FradSer FradSer commented Mar 2, 2021

👍Really helpful, thanks.

@jirikrepl

This comment has been minimized.

Copy link

@jirikrepl jirikrepl commented Mar 4, 2021

👍 @d9i seems to be working great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment