Last active
November 6, 2018 08:30
-
-
Save lukauskas/9654997 to your computer and use it in GitHub Desktop.
Download and set a random wallpaper on Mac OSX Mavericks
This file contains 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 | |
""" | |
Sets a random wallpaper on Mac OSX Mavericks. | |
The wallpaper is fetched from interfacelift's random page for Retina resolution | |
""" | |
import urllib2 | |
import re | |
import os | |
# We need to pretend we are Chrome, otherwise HTTP 403 Forbidden is thrown | |
HEADERS={'User-Agent': "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36"} | |
# Set this to your resolution's random page | |
URL='http://interfacelift.com/wallpaper/downloads/random/widescreen/5120x2880/' | |
def fetch_random_wallpaper(filename): | |
r = urllib2.Request(URL, headers=HEADERS) | |
response = urllib2.urlopen(r).read() | |
match = re.search("<a\s+href=['\"](/wallpaper/[^'\"]*?\.jpg)['\"]", response) | |
wallpaper_url = match.group(1) | |
wallpaper_url = ''.join(['http://www.interfacelift.com', wallpaper_url]) | |
wallpaper_request = urllib2.Request(wallpaper_url, headers=HEADERS) | |
wallpaper_response = urllib2.urlopen(wallpaper_request).read() | |
with open(filename, 'w') as f: | |
f.write(wallpaper_response) | |
def set_as_wallpaper(filename): | |
# From: http://derflounder.wordpress.com/2013/10/26/mavericks-desktop-background-picture-settings-moved-from-librarypreferencescom-apple-desktop-plist/ | |
full_path = os.path.abspath(filename) | |
cmd = 'osascript -e "tell application \\"System Events\\" to set picture of every desktop to \\"{0}\\""'.format(full_path) | |
os.system(cmd) | |
# This seems to be necessary | |
os.system('killall Dock') | |
def main(filename): | |
fetch_random_wallpaper(filename) | |
set_as_wallpaper(filename) | |
if __name__ == '__main__': | |
import sys | |
if len(sys.argv) >= 2: | |
filename_ = sys.argv[1] | |
else: | |
filename_ = os.path.join(os.path.dirname(__file__), 'wallpaper.jpg') | |
main(filename_) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment