💾 Archived View for her.esy.fun › posts › 0007-switch-iterm-profile-catalina › index.gmi captured on 2021-12-04 at 18:04:22. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-11-30)

-=-=-=-=-=-=-

Catalina iTerm Theme switch

author: Yann Esposito

email: yann@esposito.host

gpg

date: [2019-11-10 Sun]

keywords: self-hosting chat irc

description: Change the profile of iTerm in sync with macOS preferences.

How to have dark/light profile selected when opening a new iTerm.

The script

For the script to work you need to have two iTerm profiles, one named

=Dark= and the other one =Light=. Just go to =Preferences= then =Profiles= then

=Duplicate profile=.

I use =fish= but you can easily adapt that in your =.bashrc=,

=.bash_profile= etc...

Here is what I have in my =~/.config/fish/config.fish=:

function setItermProfile
    echo -ne "\033]50;SetProfile=$argv\a"
end

function sync_appearance

    set -x MacOSThemeAutoSwitch \
        (defaults read -g AppleInterfaceStyleSwitchesAutomatically 2>/dev/null)
    test -z $MacOSThemeAutoSwitch; and set -x MacOSThemeAutoSwitch 0

    set -x MacOSTheme (defaults read -g AppleInterfaceStyle 2>/dev/null)
    test -z $MacOSTheme; and set -x MacOSTheme 'nil'

    if test -n $ITERM_PROFILE # check if we are using iTerm2
        switch $MacOSThemeAutoSwitch
            case 1 # autoswitch on
                switch $MacOSTheme
                    case 'nil'
                        setItermProfile Light
                    case '*'
                        setItermProfile Dark
                end
            case 0 # autoswitch off
                switch $MacOSTheme
                    case 'Dark'
                        setItermProfile Dark
                    case 'Light'
                        setItermProfile Light
                    case 'nil'
                        setItermProfile Light
                end
        end
    end
end

if status --is-login
    if status --is-interactive
        sync_appearance
    end
end

If the appearance change, you can call =sync_appearance= function in your

shell to sync with the OS dark/light appearance.

Home

Feed

Slides

About

code

bookmarks

notes