5

How can I update Mac OS X Mavericks desktop background from a script?

Update Mac OS X desktop background from a script worked up to OS X 10.8, but doesn't work anymore. The desktop defaults to the default "Wave" background.

6

The document Jan-Yves referenced appears to be toast now.

I use SQLite Database Browser 2.0 b1.app on Mavericks. It's a little old but still works.

If you open up ~/Library/Application Support/Dock/desktoppictures.db, you can view the crucial "data" table. It's basically just a path to whatever the current desktop pic should be. However there can be multiple rows depending on how many monitors you are running. Apparently you can set different desktop pics per monitor and this improvement is part of Mavericks I believe.

You could manually alter the row(s) in your sqlite editor and then do a 'killall Dock' as long as you remember to save your changes in the editor. That's not really any easier than popping open System Prefs but I tried it out just to ensure how the system worked. Probably makes sense to do a backup of your .db file before manually editing.

Anyway, I use a couple small shell scripts to change my desktop as a warning for when I've altered the hosts file.

The full script looks like:

#!/bin/sh
sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '/Users/user/Documents/My Pictures/dlanham_Phobos.sitechange.jpg'";
killall Dock;

You could add an argument easily to allow more control over the picture but this is the basic logic.

You'll see some posts recommend an extra "where rowid=1". For my purposes, I allow the desktop to be changed for all monitors so I leave out the rowid. It'll update multiple rows.

hope this helps.

3

Mavericks has changed where it stores desktop background information. It used to be in ~/Library/Preferences/com.apple.desktop.plist (as a binary plist file) and it's now in ~/Library/Application Support/Dock/desktoppictures.db (as an SQLite file). If you open it up (I think sqlite3 comes as standard with OS X? Otherwise you should be able to find a SQLite DB app - I use Base but there may be nicer out there) you'll notice that you have desktop background file names in the data table. The other tables are interesting, but a bit more cryptic. You can basically run an UPDATE query on the table to change the desktop background of a given space, e.g.:

UPDATE data SET value=~/Pictures/foo.png WHERE ROWID=1

I only just worked this out myself: a little more detail is available here.

[ETA: Fixed link]

0

I couldn't get the somewhat deprecated appscript to work anymore, so I switched to PyObjc.

Using python 2.7 from Macports with the py27-pyobjc-cocoa package installed:

#!/opt/local/bin/python2.7

from AppKit import NSWorkspace, NSScreen
from Foundation import NSURL

def change_desktop_background(file, desk_id):
    file_url = NSURL.fileURLWithPath_(file)
    ws = NSWorkspace.sharedWorkspace()
    screen = NSScreen.screens()[desk_id]
    ws.setDesktopImageURL_forScreen_options_error_(file_url, screen, {}, None)

# Change my second screen desktop
change_desktop_background("myfile.jpg", 1)
0

I've been trying to get this working myself on osx Yosemite, and through a combo of @yelling_monster answer and the old bash script that used to work on mountain lion, I made this:

#! /bin/bash

echo -n "Drag and drop an image file here and hit 'return' (or ctrl-c to cancel)
"
 read -e WLPR;
function change_wallpaper
 {
#!/bin/sh
sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '$WLPR'";
killall Dock;
 }
 change_wallpaper

Backup your desktoppicture.db with a simple

cp ~/Library/Application\ Support/Dock/desktoppicture.db{,-bkp}

Follow the instructions on the site I linked to make the script executable (call it something nicer like wpset) and you're good to go.

Once executed, you can still individually change a monitor's background via Desktop & screensaver in system preferences, this is handy for a second monitor (the old right-click picture in finder -> services context menu doesn't seem to do anything though)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.