Keith Smiley | About | RSS | Mastodon

iTerm theme based on the time of day

One of the great things about Vim's textual configuration is it's ability to contain logic based on outside factors. For the purpose of this post I'm referring to the ability to set your colorscheme based on the time of day with something like this.

Having this functionality in Vim with the Solarized theme at night really made me want this in iTerm as well. Unfortunately iTerm's conifguration doesn't allow anything similar to this. The closest you get is profiles which you can assign keyboard shortcuts to for quickly opening windows with different colorschemes. Luckily, thanks to this pull request two years ago from Piet Jaspers, support was added for scripting iTerm's entire colorscheme with AppleScript. Using these AppleScript bindings I was able to create a script that changes the entire colorscheme of iTerm based on the time of day between Solarized light and dark. As you can see the bulk of this script is just setting different color attributes based on the theme you want. While you could do this conversion by hand to 65535 flavored RGB, I made a tiny Objective-C app to automate the process which is on Github. You can download the signed binary here.

Using this newly created AppleScript I then made a zsh function so that I could call colorize from anywhere to update the color scheme of the current terminal. I also chose to do this at the end of my .zshrc here. This way everytime I open a new session my theme is automatically set.

If you have any input on how I could optimize this let me know.