It turns out that changing the desktop picture of the main display is very, very simple if using either Applescript or Automator.
The Desktop Preferences
The first thing I had to figure out was where the preference for the desktop image was stored. Using Spotlight, I found this quickly. It's simply an entry in the preferences file ~/Library/Preferences/com.apple.desktop.plist. In that file, there is an entry containing some information for each file, for each display. The Property List Editor can be used to inspect the contents of this file.
A Short Wrong Turn
I did initially try to write some C code to do this using the Preferences API in the Core Foundation Framework. It turns out that this does not work. It's not too difficult to change the image file name in the desktop preferences file, but what is missing in the API is a way to cause an update using the new value.
The Finder Owns the Desktop
After a bit of investigation (mostly thanks to the article listed in the next section), I found out that the desktop is owned by the Finder application. Therefore changing the desktop image should be done through the Finder's API.
The Automator Solution
The big clue as to how this all works came from the article Change desktop wallpaper with an Automator action. This is my first encounter Automator, and it resulted in one of those "Wow, it just works!" moments that are only found in the Apple world. In the Finder library, there is the action Set the Desktop Picture that takes a file name as input. To build a trivial example, the file name can be supplied by the Ask for Finder Items action. When this is run, we have an application that asks for an image filename and sets the desktop image to it. It is really that simple, and works very reliably.
The AppleScript Solution
The exact same thing can be done using AppleScript. All that has to be done is to send a message to the Finder application. After a bit of searching and tinkering, I came up with
tell application "Finder"
set pFile to file "Macintosh HD:Users:colin:Pictures:2007_02_23:IMG_7119.JPG" as string
set desktop picture to file pFile
end tell
Just like the Automator example, this works reliably and is conveniently simple.
So, I now have to easy ways to change the desktop of the main display to a specific image. This is one piece of the project in place. However, these examples only change the picture for the main display. I still need to find out how to change pictures of the other displays.
Sunday, March 4, 2007
Saturday, March 3, 2007
FlickrDesktop
Hello! you've found the journal for my latest project.
I've decided to develop a Mac application to change the desktop picture using photos from Flickr. For some reason, a good one just doesn't seem to exist yet, so the only good solution is to write my own.
As this is my first real foray into development specifically for the Mac and for web services, there's likely to be a lot of experiments and lessons to keep track of. This journal is all about that. This journal is not yet about an existing application.
I've decided to develop a Mac application to change the desktop picture using photos from Flickr. For some reason, a good one just doesn't seem to exist yet, so the only good solution is to write my own.
As this is my first real foray into development specifically for the Mac and for web services, there's likely to be a lot of experiments and lessons to keep track of. This journal is all about that. This journal is not yet about an existing application.
Subscribe to:
Posts (Atom)