Skip to content

Instantly share code, notes, and snippets.

@vanrijn
Last active February 3, 2016 02:30
Show Gist options
  • Save vanrijn/3fec2204f836806688b1 to your computer and use it in GitHub Desktop.
Save vanrijn/3fec2204f836806688b1 to your computer and use it in GitHub Desktop.
A simple python script to allow updating all of your desktop backgrounds, for all your screens/desktops/spaces at once.
#!/usr/bin/env python
# A simple python script to allow updating all of your desktop backgrounds,
# for all your desktops/spaces at once.
# Check out http://1klb.com/blog/desktop-background-on-os-x-109-mavericks.html
# for more information. His implementation was in Ruby. I wanted to do it
# in Python.
import sys
import os
import sqlite3
line = ""
# sys.argv[0] is the program name. sys.argv[1] is the first argument
if len(sys.argv) > 1:
line = sys.argv[1]
# if we don't already have a file to use, ask for one
if not line:
try:
line = raw_input(
"Drag and drop an image file here then press 'return' or press 'control-c' to cancel: ")
except KeyboardInterrupt:
sys.exit()
if not line:
print "Okay, well, we can't do this if you don't cooperate!"
sys.exit()
print "You entered '%s'" % line
background = os.path.abspath(line.strip())
if not os.path.isfile(background):
print "Sorry, this is not a valid file: '%s'" % background
sys.exit()
print "Changing all desktop backgrounds to: '%s'" % background
home = os.path.expanduser("~")
desktopdb = "%s/Library/Application Support/Dock/desktoppicture.db" % home
print "Opening desktop background database: '%s'" % desktopdb
# Make connection to existing db
conn = sqlite3.connect(desktopdb)
c = conn.cursor()
# Print current data
print "Current data:"
for row in c.execute('SELECT ROWID, value FROM data'):
print row
# We're just going to update all rows. "value" == "filename" in the database.
# This could be more complex and randomly select backgrounds from a directory
# or whatever. But personally I want all my desktop backgrounds to be the same
# for all screens and desktops/spaces.
t = (background, )
c.execute("UPDATE data SET value=?", t)
conn.commit()
# Print updated data
print "Updated data:"
for row in c.execute('SELECT ROWID,value FROM data'):
print row
conn.close()
# Force Dock to redraw all desktop backgrounds
os.system("Killall Dock")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment