How can I programmatically change the background in Mac OS X?

From python, if you have appscript installed (sudo easy_install appscript), you can simply do

from appscript import app, mactypes
app('Finder').desktop_picture.set(mactypes.File('/your/filename.jpg'))

Otherwise, this applescript will change the desktop background

tell application "Finder"
    set desktop picture to POSIX file "/your/filename.jpg"
end tell

You can run it from the command line using osascript, or from Python using something like

import subprocess

SCRIPT = """/usr/bin/osascript<<END
tell application "Finder"
set desktop picture to POSIX file "%s"
end tell
END"""

def set_desktop_background(filename):
    subprocess.Popen(SCRIPT%filename, shell=True)

Leave a Comment