Automatic Switching Between Light and Dark Solarized Modes

I've recently switched to using the solarized color theme. At first it took getting used to the lower contrast; however, I was persuaded to keep trying based on the research behind the choice of the colors. I'm now quite happy with it.

With solarized there are 2 mode of operation, light mode and dark mode. These modes have the somewhat obvious meanings, light mode uses a light background with darker text, and dark mode uses a dark background with light text. I like both modes, and find that the dark mode is well suited to nighttime use and likewise the light mode is good for daytime.

So the next question was how to make this automatic. I use iTerm on the Mac so I created 2 profiles 1 for each mode and gave each profile a hot key. But what about when the sun sets or rises and I've got windows open? Also I use different colors for the TMUX status bar depending on the mode, I'd like those long running sessions to update as well depending on the time of the day.

In order to update running programs I created a shell script (which also uses applescript if iTerm is running) to update both iTerm and TMUX based on an hour range. I then execute this script once an hour using crontab.

One of the features of solarized is that the only difference between the light and dark modes is the choices for foreground color and background color. So we only have to swap those two values. I also change the color of the text behind the cursor.

Update color mode shell script.

#!/bin/bash

# Set Color Values

darkend=600             # 6am
darkstart=2000          # 10pm
timeval="$(date +%k%M)" # current time

if [ $timeval -gt $darkend -a $timeval -lt $darkstart ]; then
    # Dark Mode
    itermcolors='{{64842, 62778, 56626}, {21257, 26684, 28737}, {60037, 58326, 52284}}'
    tmux_cur_fg=black
    tmux_bg=colour7
else
    # Light Mode
    itermcolors='{{0, 7722, 9941}, {28873, 33398, 33872}, {0, 10207, 12694}}'
    tmux_cur_fg=white
    tmux_bg="#124741"
fi

# Adjust iTerm

if [ -n "$(ps x | grep iTerm | grep -v grep)" ]; then
    osascript <<- EOF
    tell application "iTerm"
	repeat with aSession in sessions of current terminal
	    tell aSession
		set theColors to $itermcolors
		set background color to item 1 of theColors
		set foreground color to item 2 of theColors
		set cursor_text color to item 3 of theColors
	    end tell
	end repeat
    end tell
EOF
fi

# Adjust TMUX

sessions="$(tmux ls 2>/dev/null | cut -f1 -d:)"
for s in $sessions; do
    tmux set-window-option -t $s -g window-status-current-fg $tmux_cur_fg > /dev/null
    tmux set-window-option -t $s -g status-bg $tmux_bg > /dev/null
done

Crontab entry

0 * * * * /users/chopps/bin/update-color-mode.sh