public
Last active

Swap color themes (between Solarized Light and Solarized Dark) in Sublime Text 2 based on the time of day. Meant to be scheduled by the Windows Task Scheduler or cron.

  • Download Gist
switch_profile.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
from os import path, environ
import datetime
import json
import sys
 
appdata = environ['APPDATA']
sublime_path = 'Sublime Text 2'
settings_path = 'Packages\User\Preferences.sublime-settings'
day_profile = 'Packages/Color Scheme - Default/Solarized (Light).tmTheme'
night_profile = 'Packages/Color Scheme - Default/Solarized (Dark).tmTheme'
daylight_hours = range(7,19) # hours in 24 hour time, note endpoint is reduced by 1
def switch_profile(profile):
"""
Opens Preferences.sublime-settings file, reads the JSON, replaces the
JSON and writes the new JSON, all without closing, to avoid race
conditions.
"""
print profile
f = open(file_path, 'r+')
settings_json = json.loads(f.read())
print settings_json['color_scheme']
if settings_json['color_scheme'] != profile:
settings_json['color_scheme'] = profile
f.seek(0)
print settings_json['color_scheme']
json.dump(settings_json, f, indent=4) # pretty print and write to file
f.truncate()
f.close()
 
now = datetime.datetime.now()
file_path = path.join(appdata, sublime_path, settings_path)
 
if now.hour in daylight_hours:
switch_profile(day_profile)
else:
switch_profile(night_profile)
sys.exit()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.