Skip to content

Instantly share code, notes, and snippets.

@maclunar
Created September 21, 2023 09:11
Show Gist options
  • Save maclunar/c740893ffed523303c4d3a7bb8c6ac8e to your computer and use it in GitHub Desktop.
Save maclunar/c740893ffed523303c4d3a7bb8c6ac8e to your computer and use it in GitHub Desktop.
Automatic theme switching in macOS

Automatic theme switching in macOS

iTerm2

Base on this guide:

  1. In iTerm2 go to Scripts > Manage > New Python Script (python runtime may need installing)
  2. Choose Basic script type
  3. Chose Long-Running Daemon script interval
  4. Name the script in some descriptive way like auto_theme_switch.py and paste this as content:
#!/usr/bin/env python3.7

import asyncio
import iterm2
# This script was created with the "basic" environment which does not support adding dependencies
# with pip.

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, "Smoooooth")
            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)

# This instructs the script to run the "main" coroutine and to keep running even after it returns.
iterm2.run_forever(main)

the file lives in /Users/$USER/Library/Application\ Support/iTerm2/Scripts/.

NeoVim

Use auto-dark-mode.nvim plugin by adding following to ~/.config/nvim/lua/plugins.lua:

{
  -- ...
  -- automatic theme switching
  {
    "f-person/auto-dark-mode.nvim",
    config = {
      update_interval = 1000,
      set_dark_mode = function()
        vim.api.nvim_set_option("background", "dark")
        vim.cmd("colorscheme everforest")
      end,
      set_light_mode = function()
        vim.api.nvim_set_option("background", "light")
        vim.cmd("colorscheme everforest")
      end,
    },
  },
}

then Lazy install and restart nvim.

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