Getting the Current Desktop Background in OS X

March 30th 2009 at 5:00am
One comment

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).  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, and it’s 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 convenient 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 it’s 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 actually has to be changed by the OS.

I have not tested it when the background is changed upon logging in or waking from sleep mostly because I don’t care since I’m very happy with it changing every 15 minutes. I’ve also only tested this on OS X 10.5.6.

1 Comments
Jeff says:
Sep 30, 2009

thanks this is pretty sweet. now I need an application that links to the pic, records a rotation list, and allows you to iphoto rate the pic from the desktop.

Comments for this entry are closed.