Skip to content

Instantly share code, notes, and snippets.

@kimslawson
Forked from ameintjes/change_wallpaper
Last active July 19, 2022 09:53
Show Gist options
  • Save kimslawson/8891d809507a9a5df2f19cc027c37eed to your computer and use it in GitHub Desktop.
Save kimslawson/8891d809507a9a5df2f19cc027c37eed to your computer and use it in GitHub Desktop.
Use the recent "Astronomy Picture of the Day" by NASA as your desktop wallpaper (Updated to use Python3, tested on macOS 12.4 Monterey)
#!/usr/bin/python
# Downloads the current "Astronomy Picture of the Day" from nasa.gov and sets as wallpaper
#
# Installation steps:
# - store this file somewhere and take note of the path
# - change WALLPAPER_DIR to some folder in your home directory
# and create the folder
# - make it executable:
# chmod a+x /path/to/change_wallpaper
# - follow instructions given in the .plist file to make it run daily
FEED_URL = "http://apod.nasa.gov/apod/"
WALLPAPER_DIR = "/Users/mw/Pictures/Wallpapers/APOD"
import subprocess, re, datetime
from urllib import urlopen, urlretrieve
import os.path, os
def main():
xml = urlopen(FEED_URL).read()
m = re.search(r"a href=\"(image/.*?)\"", xml)
#m = re.search(r"img src=\"(.*?)\"", xml)
first_img = FEED_URL + m.group(1)
name = re.sub(r"^.*/", "", first_img)
filename = "%s/%s-%s" % (WALLPAPER_DIR, str(datetime.date.today()), name)
print "Image URL: ", first_img
print "Filename: ", name
print "Target file: ", filename
if os.path.isfile(filename):
print "File already exists."
else:
print "Downloading..."
urlretrieve(first_img, filename)
set_desktop_background(filename)
SCRIPT = """/usr/bin/osascript -s o<<END
tell application "Finder"
set desktop picture to POSIX file "%s"
end tell
END"""
def set_desktop_background(filename):
print "Changing wallpaper..."
#subprocess.Popen(SCRIPT%filename, shell=True)
subprocess.check_call(SCRIPT%filename, shell=True)
main()
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd >
<plist version="1.0">
<dict>
<key>Label</key>
<string>io.weller.max.changewallpaper</string>
<!--
store this file in ~/Library/LaunchAgents/
edit the path to change_wallpaper below
-->
<key>Program</key>
<string>/path/to/change_wallpaper</string>
<key>RunAtLoad</key>
<true/>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>3</integer>
<key>Minute</key>
<integer>15</integer>
</dict>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment