Getting the Current Desktop Background in OS X

March 30, 2009

I have a lot of desktop backgrounds (a.k.a. “wallpapers”) on my Mac. I keep them all in one folder and set them to change every 15 minutes in random order. Currently, this folder has 1,329 backgrounds in it, and every once in a while I see a background that I either love and want to make it the permanent background for a few days, or hate and want to delete it. The problem is there’s no quick and easy way in OS X to find out the exact path and file name of the currently used background, so browsing through all those files is a major time waster.

Luckily, though, there is one place that the current background file is listed, in ~/Library/Preferences/com.apple.desktop.plist. If you have the development tools installed, you can use the Property List Editor to look at the /Background/default/LastName value, which, along with the /Background/default/NewChangePath value, make up the full path and file name of the current desktop background that OS X has changed. The value of /Background/default/NewImageFilePath is the full path and file name of the last manually set background, but I’m not interested in that right now.

The problem with the above solution is that it’s still not the most elegant solution for my problem. What I really want is a quick way to get the path and file name without having to open a program and a file and plus out some values. I guess I could just leave a Finder window open to ~/Library/Preferences with com.apple.desktop.plist selected and open it up each time I want to see the background’s name, but that isn’t a very elegant solution. What I really want is an AppleScript that could be quickly launched via Quicksilver or LaunchBar and display the path and file name in a dialog box.

image

I looked around for five minutes, couldn’t find one, so I decided to write it myself. Presented below, in all its glory, is an AppleScript that will display the full path and file name of the currently used desktop background in a dialog box.

set plistFolderPath to path to preferences folder from user domain as string
set plistPath to plistFolderPath & "com.apple.desktop.plist"
tell application "System Events"
  tell property list file plistPath
    tell contents
      set theResult to value of property list item "NewChangePath" of property list item "default" of property list item "Background" & "/" & value of property list item "LastName" of property list item "default" of property list item "Background"
    end tell
  end tell
end tell
display dialog theResult buttons {"Done"} default button 1

There are a few minor catches…it will only work if the current desktop background was automatically changed by OS X using the “Change picture” setting in the Desktop & Screen Saver preference pane. Additionally, it doesn’t work for the first desktop background displayed after you set the check box, it has to be changed by the OS based on the time setting.

This script works on OS X 10.5.6. It may not work on later versions of OS X.