Last active
August 29, 2015 14:07
-
-
Save svanscho/1484c435bd80dbe0315f 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
from appscript import app | |
from datetime import datetime | |
import urllib2 | |
import os | |
import uuid | |
import sh | |
def removeOldPicture(): | |
try: | |
sh.rm(sh.glob('/tmp/background-*.jpg')) | |
#os.remove("/tmp/background.jpg") | |
except: | |
pass | |
def getCurrentUTCtime(): | |
now = datetime.utcnow() | |
return now.strftime("%Y%m%dT%H%M") | |
def downloadImage(url): | |
response = urllib2.urlopen(url) | |
filename = "/tmp/background-"+str(uuid.uuid4())+".jpg" | |
f = open(filename,'wb') | |
f.write(response.read()) | |
f.close() | |
return filename | |
#only works if you have imagemagick installed | |
def shiftImageToLeft(filename,pixels): | |
os.system(" ".join(["/usr/local/bin/convert ",filename,"-roll","-"+str(pixels),filename])) | |
try: | |
removeOldPicture() | |
#example url 1 (dynamic): http://www.timeanddate.com/scripts/sunmap.php?iso=20140929T0700 | |
#url = "http://www.timeanddate.com/scripts/sunmap.php?iso=" | |
#datestring = getCurrentUTCtime() | |
#url=url+datestring | |
#example url 2 (static): http://static.die.net/earth/mercator/1600.jpg | |
url = "http://static.die.net/earth/mercator/1600.jpg" | |
filename=downloadImage(url) | |
#Australia centric map | |
#shiftImageToLeft(filename,650) | |
se = app('System Events') | |
desktops = se.desktops.display_name.get() | |
for d in desktops: | |
desk = se.desktops[d] | |
desk.picture.set(filename) | |
except Exception as e: | |
print e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You will also need imagemagick if you want to use the shiftImage function.