Sublime Forum

Automatic Theme Selection for ST2 on Mac

#1

After playing around with http://www.ifttt.com the other day I thought it would be pretty cool if I could set the Sublime Text 2 depending on the time of day. For example, at sunset I would like to have the Solarised (Dark) theme, and then at sunrise have the Solarized (Light) theme.

CAVEAT: try this at your own risk! I’ve tested it out and it works fine for me but as you can tell from my script, I’m certainly no expert. Any suggested improvements are welcome.

  1. Set up an IFTTT rule for sunset; ifttt.com/recipes/109559. Simply put I just use the weather trigger for sunset and then use a dropbox action to add a file to my IFTTT/Daylight folder called sunset.txt with ‘Dark’ in there. Do the same for sunrise with ‘Light’
  2. Add an automator folder action on your Dropbox/IFTTT/Daylight folder of the type ‘shell script’ with the following code in;

[code]DAYLIGHTDIR="/Users/luke/Dropbox/IFTTT/Daylight"
SUNSETFILE=“sunset.txt”
SUNRISEFILE=“sunrise.txt”

if -f “$DAYLIGHTDIR”/"$SUNRISEFILE" ] || -f “$DAYLIGHTDIR”/"$SUNSETFILE" ]] ;
then
CURRENTSETTING=grep tmTheme ~/Library/Application\ Support/Sublime\ Text\ 2/Packages/User/Preferences.sublime-settings | awk -F'(|)]' '{print $2}'
DAYLIGHTFILE=ls "$DAYLIGHTDIR"
NEXTSETTING=cat "$DAYLIGHTDIR"/"$DAYLIGHTFILE"
sed -i ‘.bak’ ‘s/(’$CURRENTSETTING’).tmTheme/(’$NEXTSETTING’).tmTheme/’ ‘/Users/luke/Library/Application Support/Sublime Text 2/Packages/User/Preferences.sublime-settings’
rm -f “$DAYLIGHTDIR”/"$DAYLIGHTFILE"
fi [/code]

  1. That’s it!

Note: this doesn’t change syntax specific theme settings but I am sure it could be expanded to do so.

0 Likes