A clean desktop is indicates a clear mind.


I’m a sucker for a clean desktop. Everything has a place and nothing belongs on your desktop. It may be easy to save a file to the desktop but file that away to a correct spot immediately! Anyway, with a clean desktop you can view the most basic personalization of a computer, the desktop picture. It’s boring to stare at the same picture over and over so why not change the picture daily?

First we need a source that provides a quality photo of the day. National Geographic has a photo of the day feature on their website at http://photography.nationalgeographic.com/photography/photo-of-the-day/. So first we can go and download the page.

$ curl "http://photography.nationalgeographic.com/photography/photo-of-the-day/" -o "Pictures/photo-of-the-day"

We output it to a directory and file name that is easy to recognize and remember. Once we have the file we have to find the image source. This is the tricky part so lets just throw the code out there and discuss it a bit.

$ let line=`cat Pictures/photo-of-the-day | grep -n '<div class="primary_photo">' | cut -d':' -f1`+5
$ url=`sed -n "$line p" Pictures/photo-of-the-day | cut -d' ' -f2 | cut -d '"' -f2`

The first line finds the line number where the image source address exists. For the national geographic page the only defining attribute to search for is the primary_photo class. We can not search for img src as there are many images on the page. After we have the line number of the primary_photo class we add 5 to it. This is an annoying magic number but from the page source we can see that the img src for the primary photo happens 5 lines after the class we just searched for. If they change the page design this will all break, but for now it works.

The next line grabs the url from the img src tag. We find the line number from the file and then cut down the output till we have the url. Next we download the actual image.

curl "${url:2}" -o "Pictures/wall.jpg"

Notice the ${url:2}. This takes the url string we have and removes the first two characters. The url from the previous step comes out in the form //images so we need to remove those two slashes. Now we have our image downloaded, we need to make it the background. This is surprisingly difficult to do on macs.

sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = 'Users/chrisbarthol/Pictures/wall.jpg'";
killall Dock;

Since Mavericks, background images are stored in a sqlite database. We basically have to manually insert the photo into the database. This will destroy the other photos there, and make the background image the same for all desktops. We have to kill the Dock for the image to reload. Now we run a script to fully fetch and update the desktop image everyday.
The full script is below with an additional line on how to set it in Ubuntu.

#!/bin/bash
# clear cache

rm -f Pictures/wall.jpg
rm -f Pictures/photo-of-the-day

# download photo-of-the-day page
curl "http://photography.nationalgeographic.com/photography/photo-of-the-day/" -o "Pictures/photo-of-the-day"

# get the photo url
let line=`cat Pictures/photo-of-the-day | grep -n '<div class="primary_photo">' | cut -d':' -f1`+5
url=`sed -n "$line p" Pictures/photo-of-the-day | cut -d' ' -f2 | cut -d '"' -f2`

# download the photo
curl "${url:2}" -o "Pictures/wall.jpg"

#Mac makes setting your background from bash a pain
sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = 'Users/chrisbarthol/Pictures/wall.jpg'";
killall Dock;

#For Ubuntu
#gsettings set org.gnome.desktop.background picture-uri 'file:///home/chrisbarthol/Pictures/wall.jpg'

Comments