Skip to content

Instantly share code, notes, and snippets.

@letam
Created August 10, 2023 14:09
Show Gist options
  • Save letam/3590b7902ccae95508d34f462c5a4529 to your computer and use it in GitHub Desktop.
Save letam/3590b7902ccae95508d34f462c5a4529 to your computer and use it in GitHub Desktop.
iTerm2 console script to change color preset based on time
#!/usr/bin/env python3.7
# Daemon script for iTerm2, to change color preset based on time of day
# Simpler than https://iterm2.com/python-api/examples/darknight.html#darknight-example
import asyncio
import datetime
import iterm2
HALFHOUR_IN_SECONDS = 30 * 60
SECONDS_UNTIL_NEXT_CHECK = HALFHOUR_IN_SECONDS
# Clock time to change colors.
LIGHT_TIME_HOUR = 7
DARK_TIME_HOUR = 20
# Color presets to use
LIGHT_PRESET_NAME = "Tango Light"
DARK_PRESET_NAME = "Pastel (Dark Background)"
# Profiles to update
# PROFILES=["Default"]
PROFILES = ["Copy of Default"]
def get_current_hour():
return datetime.datetime.now().hour
async def set_colors(connection, preset_name):
print("Change to preset {}".format(preset_name))
preset = await iterm2.ColorPreset.async_get(connection, preset_name)
for partial in await iterm2.PartialProfile.async_query(connection):
if partial.name in PROFILES:
await partial.async_set_color_preset(preset)
async def main(connection):
last_preset_name = None
while True:
print("Checking time for color preset")
if LIGHT_TIME_HOUR <= datetime.datetime.now().hour < DARK_TIME_HOUR:
preset_name = LIGHT_PRESET_NAME
else:
preset_name = DARK_PRESET_NAME
if preset_name != last_preset_name:
await set_colors(connection, preset_name)
last_preset_name = preset_name
await asyncio.sleep(SECONDS_UNTIL_NEXT_CHECK)
iterm2.run_forever(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment