Synchronizing OSX, Terminal, and Emacs Dark/Light Appearance

Composing Shell, Applescript, and Emacs Eval

Throughout the day, I find myself toggling between Light and Dark modes. This evening I wrote up a shell script to keep Macintosh Operating System X (OS X 📖), Emacs (Emacs 📖), and my Terminal synchronized.

I named the script dark. Now, from the command-line, I can type dark to switch between Light and Dark mode.

Below is a copy of that script:


# This script toggles the Operating System, Terminal, and Emacs
# themes.  It uses the state of the Operating System to determine
# if the terminal and Emacs should be "dark" or "light".

# First, tell OS-x to toggle dark mode.
osascript -e 'tell application "System Events" to tell appearance preferences to set dark mode to not dark mode'

# Second, determine what is the newly set appearance.
appearance=`defaults read -g AppleInterfaceStyle 2>/dev/null`
if [ -z "$appearance" ]
  # No value for AppleInterfaceStyle, so the OS has us in light
  # mode, proceed accordingly.
  sh $HOME/.config/base16-shell/scripts/
  editor --eval "(disable-theme 'modus-vivendi)" \
    --eval "(modus-operandi-theme-load)" 1> /dev/null
  # AppleInterfaceStyle is set, and that means we're now in "Dark"
  # mode.
  sh $HOME/.config/base16-shell/scripts/
  editor --eval "(disable-theme 'modus-operandi)" \
    --eval "(modus-vivendi-theme-load)" 1> /dev/null

Nothing fancy.


Emacs Configuration

Later on , I started tinkering to see about Emacs synchronizing with the Dark/Light mode of OS X.

(defun jnf-dark ()
  "Toggle system-wide Dark or Light setting."
  (shell-command "osascript -e 'tell application \"System Events\" to tell appearance preferences to set dark mode to not dark mode'")

(defun jnf-emacs-theme-by-osx-appearance ()
  "Set theme based on OSX apperance state."
  (if (equal "Dark" (substring
                        "defaults read -g AppleInterfaceStyle") 0 4))
    (disable-theme 'modus-operandi)
    (disable-theme 'modus-vivendi)

;; Load the appropriate Emacs theme based on OSX appearance

I added this to my one of the files I load for Emacs.