Skip to content

Instantly share code, notes, and snippets.

@svanscho
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svanscho/1484c435bd80dbe0315f to your computer and use it in GitHub Desktop.
Save svanscho/1484c435bd80dbe0315f to your computer and use it in GitHub Desktop.
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
@svanscho
Copy link
Author

svanscho commented Oct 1, 2014

You will also need imagemagick if you want to use the shiftImage function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment