Created
January 12, 2015 18:07
-
-
Save pedramamini/9d4406b268778c30a134 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Desktop Background Rotater | |
Background images: | |
http://bitday.me | |
Crontab entry: | |
# min hr mday month wday command | |
*/15 * * * * /Users/pedram/Utils/rotate_desktop_background.py | |
Locale information: | |
http://www.wolframalpha.com/input/?i=sunrise+in+austin | |
http://www.wolframalpha.com/input/?i=sunset+in+austin | |
""" | |
import shlex | |
import datetime | |
import subprocess | |
CURRENT_BG = "/tmp/current_desktop_background.dat" | |
CHANGE_CMD = """/usr/bin/osascript -e 'tell application "Finder" to set desktop picture to POSIX file "%s/%s"'""" | |
BG_DIR = "/Users/pedram/Pictures/Backgrounds/BitDay-2560x1600/" | |
BG_MAP = \ | |
[ | |
"_night.png", # 0000 | |
"_late_night.png", # 0300 | |
"_morning.png", # 0600 | |
"_late_morning.png", # 0900 | |
"_afternoon.png", # 1200 | |
"_late_afternoon.png", # 1500 | |
"_evening.png", # 1800 | |
"_late_evening.png", # 2100 | |
] | |
######################################################################################################################## | |
def launch_command (args, stdin=""): | |
""" | |
Launch the specified command with args (each as a separate item of a list). | |
""" | |
# if a list was not provided, encode the string in UTF-8 and convert to list. | |
if type(args) is not list: | |
args = shlex.split(args.encode("utf-8", "ignore")) | |
process = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
out, err = process.communicate(stdin) | |
return out, err | |
######################################################################################################################## | |
if __name__ == "__main__": | |
# what's the current background set to? | |
current_bg = "" | |
with open(CURRENT_BG, "a+") as fh: | |
current_bg = fh.read() | |
# what time is it? | |
now = datetime.datetime.now() | |
# hours divided by 3 = index into our BG map table | |
desired_bg = BG_MAP[now.hour/3] | |
# if the desired and current don't match, set the background. | |
if current_bg != desired_bg: | |
launch_command(CHANGE_CMD % (BG_DIR, desired_bg)) | |
# update the current background on disk. | |
with open(CURRENT_BG, "w+") as fh: | |
fh.write(desired_bg) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment