Posts tagged mdls
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.