Skip to content

Instantly share code, notes, and snippets.

@urbushey
Created October 9, 2012 22:35
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save urbushey/3861892 to your computer and use it in GitHub Desktop.
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.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment