Skip to content

Instantly share code, notes, and snippets.

Last active Sep 15, 2018
What would you like to do?
A simple python3 script to automatically download the latest Astronomy Picture of the Day (APOD) as an image and set it as the desktop background for all spaces in OS X. Because OS X is a bit unfriendly in how it lets you change the background, python launches an applescript that steps through each space individually. Note that this assumes you …
#!/usr/bin/env python3
# Download the daily APOD picture and set it as the system wallpaper on OS X
# @author Alex Zylstra
# @date 2013/10/05
__author__ = 'Alex Zylstra'
__date__ = '2013-10-05'
import urllib.request
import os
import subprocess
import sys
# Define the available mirrors. The dict is keyed by location
# Each location is then mapped to a list of mirrors. Each mirror must
# have the base url and the relative url for the page in a tuple
# Base URL must have a trailing slash
all_mirrors = dict( US=[('', 'astropix.html'),
('', '')],
UK=[('', 'astropix.html')])
# Parse the command line arguments
locale = 'US' # default
spaces = 1 # default
for i in range(len(sys.argv)):
# Check for the localization flag:
if sys.argv[i] == '--locale' or sys.argv[i] == '-l':
if len(sys.argv) > i+1:
locale = sys.argv[i+1]
if not locale in all_mirrors.keys():
keystr = ''
for key in all_mirrors.keys():
keystr += key + ','
print('Invalid entry for locale. Available options: ' + keystr)
# Check for the number of spaces:
if sys.argv[i] == '--spaces' or sys.argv[i] == '-s':
if len(sys.argv) > i+1:
spaces = int(sys.argv[i+1])
if spaces <= 0:
raise ValueError()
except ValueError:
print('Invalid entry for number of spaces, must be a positive non-zero integer.')
# Set the mirrors:
mirrors = all_mirrors[locale]
# loop over mirrors:
download = False
timeout = 1
for mirror in mirrors:
# main page for APOD for this mirror:
URL = mirror[0] + mirror[1]
if not download:
html_content = urllib.request.urlopen(URL, timeout=timeout).read()
# split into lines
source = html_content.split()
# look for the link to the image
img_url = ""
for rawline in source:
line = str(rawline)
if 'jpg' in line or 'JPG' in line:
if 'href' in line:
img_url = line
# now we have to parse it:
for substr in img_url.split('''"'''):
if 'jpg' in substr:
img_url = substr
# concatenate:
img_url = mirror[0] + img_url
# get name of the image itself:
img_name = img_url.split('/')[-1]
# do the download:
u = urllib.request.urlopen(img_url, timeout=timeout)
raw =
print("read {0} KB".format(int(len(raw)/1024)))
# close the connection:
# save to a temp file:
tmp_dir = '/tmp'
fname = os.path.join(tmp_dir, img_name)
f = open(fname, 'wb')
download = True
print('Downloaded image from ' + mirror[0])
except Exception as e:
print('Unable to download from ' + mirror[0])
if download:
# now to set the background via somewhat clunky script
SCRIPT = """/usr/bin/osascript << END
tell application "System Events" to key code 18 using {control down} -- ⌃1
repeat %i times
tell application "Finder" to set desktop picture to POSIX file "%s"
delay 1.0
tell application "System Events" to key code 124 using {control down} -- ⌃→
delay 1.0
end repeat
tell application "System Events" to key code 18 using {control down} -- ⌃1
subprocess.Popen(SCRIPT%(spaces,fname), shell=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment