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.

@ghost

This comment has been minimized.

Copy link

@ghost ghost 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!

@IsidroMar95

This comment has been minimized.

Copy link

@IsidroMar95 IsidroMar95 commented Apr 17, 2021

i got this error... why?
ended unexpectedly
image

@kazlauskis

This comment has been minimized.

Copy link

@kazlauskis kazlauskis commented May 11, 2021

@d9i thanks!

@thomashexton

This comment has been minimized.

Copy link

@thomashexton thomashexton commented May 28, 2021

i got this error... why?
ended unexpectedly
image

I was getting this error too. However I resolved it by choosing some of the base themes. Solarized Light and Solarized Dark. See if that fixes it for you too? They may not be the exact themes you want though, but at least my eyes aren't blown out at night now.

@pxlshpr

This comment has been minimized.

Copy link

@pxlshpr pxlshpr commented Jun 2, 2021

@d9i thank you, works like a charm

@arosca

This comment has been minimized.

Copy link

@arosca arosca commented Jun 12, 2021

👍 thanks

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