her.esy.fun/src/posts/0007-switch-iterm-profile-catalina/index.org

70 lines
2 KiB
Org Mode
Raw Normal View History

2019-11-10 21:05:12 +00:00
#+TITLE: Catalina iTerm Theme switch
#+AUTHOR: Yann Esposito
#+EMAIL: yann@esposito.host
#+DATE: [2019-11-10 Sun]
2021-04-27 13:02:02 +00:00
#+KEYWORDS: self-hosting chat irc
2020-05-02 13:30:40 +00:00
#+DESCRIPTION: Change the profile of iTerm in sync with macOS preferences.
2019-11-21 15:21:15 +00:00
#+OPTIONS: auto-id:t toc:nil
2019-11-10 21:05:12 +00:00
2019-11-10 21:15:00 +00:00
How to have dark/light profile selected when opening a new iTerm.
2019-11-10 21:05:12 +00:00
* The script
:PROPERTIES:
:CUSTOM_ID: the-script
:END:
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=:
2021-05-24 13:40:47 +00:00
#+begin_src sh
2019-11-10 21:15:00 +00:00
function setItermProfile
echo -ne "\033]50;SetProfile=$argv\a"
end
2019-11-10 21:05:12 +00:00
2019-11-10 21:15:00 +00:00
function sync_appearance
2019-11-10 21:05:12 +00:00
2019-11-10 21:15:00 +00:00
set -x MacOSThemeAutoSwitch \
(defaults read -g AppleInterfaceStyleSwitchesAutomatically 2>/dev/null)
test -z $MacOSThemeAutoSwitch; and set -x MacOSThemeAutoSwitch 0
2019-11-10 21:05:12 +00:00
2019-11-10 21:15:00 +00:00
set -x MacOSTheme (defaults read -g AppleInterfaceStyle 2>/dev/null)
test -z $MacOSTheme; and set -x MacOSTheme 'nil'
2019-11-10 21:05:12 +00:00
2019-11-10 21:15:00 +00:00
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
2019-11-10 21:05:12 +00:00
2019-11-10 21:15:00 +00:00
if status --is-login
if status --is-interactive
sync_appearance
end
end
2019-11-10 21:05:12 +00:00
#+end_src
If the appearance change, you can call =sync_appearance= function in your
shell to sync with the OS dark/light appearance.