GeekTool/AppleScript Display of Desktop Picture Info
So… I am the world’s biggest dork. I don’t dispute it. And when I get stressed out, my inner librarian comes out, and comes out big.
I have a large folder of desktop wallpaper that I rotate through with some regularity (every five minutes or so). They all come from the web. And I project my desktop with some regularity. Which means that I get asked about where my desktop photo comes from pretty often (a few times a week). And I teach about media literacy, intellectual property and citation. Which means that I want to give credit where credit is due.
A few years ago, I started actually adding the URL of the page that I had downloaded an image from as its Finder Get Info comment. And then, in one of the Leopards, Apple started including this metadata in the Spotlight database for all downloaded files, too. Which means that, if I know the name of the image on the desktop right now, I can look up the URL that I got it from. That’s a big if.
I spent a little while last night tinkering, and came up with the following command that gets me the name of the currently displayed desktop wallpaper:
defaults read ~/Library/Preferences/com.apple.desktop | grep 'LastName' | tail -n 1 | grep '[^"]*\.[^"]*' -o |
This extracts the final LastName entry out of the com.apple.desktop.plist file and pulls just the file name out of it. Then, because I knew that I actually want the URL that I stored in the Get Info comments field, I extended this to the following:
cd ~/Pictures/Wallpapers/Single\ Display/; defaults read ~/Library/Preferences/com.apple.desktop | grep 'LastName' | tail -n 1 | grep '[^"]*\.[^"]*' -o | xargs mdls -name kMDItemFinderComment | grep 'http[^"]*' -oi |
You’ll note that I assumed that all my wallpaper comes from a specific folder (~/Pictures/Wallpapers/Single Display/) — but this information could also be extracted from com.apple.desktop.plist (run the defaults command without the grepping to see what I mean — look for ChangePath). The big breakthrough for me was realizing that I needed to use xargs to extract meaning from the result of my grepping, which was presented to mdls with a newline at the end — xargs then runs mdls on all of the one lines of resulting output.
In the end, I presented the output of each of those shell scripts as overlays on my desktop (using GeekTool) and I wrapped all of this into a little AppleScript that opens the web page from whence my desktop image comes:
do shell script "cd ~/Pictures/Wallpapers/Single\\ Display/; defaults read ~/Library/Preferences/com.apple.desktop | grep 'LastName' | tail -n 1 | grep '[^\"]*\\.[^\"]*' -o | xargs mdls -name kMDItemFinderComment | grep 'http[^\"]*' -oi | xargs open" |
The real addition is that I use the xargs to pass the URL to open, which opens the URL in the default browser.
In all of this, it is worth noting that Bwana has been enormously useful. As was this article from O’Reilly on using Spotlight from the command line and this page on the defaults command.
No related posts.
This entry was posted by Seth Battis on June 4, 2010 at 10:34 am, and is filed under Educational Technology, How To. Follow any responses to this post through RSS 2.0.You can leave a response or trackback from your own site.
I’m still wondering how to do this on multiple displays. I’m noticing that the entries under Root.Background in com.apple.desktop.plist seem to indicate different display setups. I’m noticing that the last two entries (69670592 and 686310657) represent the settings for my left and right displays and I can extract the information as described in this post. But I don’t see how I would be able to determine which set of settings (there are 6 listed under Root.Background — the two mentioned above, two other seemingly random numbers, 0 and default) applies to the particular display in question.
Hmm.