Put Unsplash on Ubuntu Desktop

Unsplash features not only a fine selection of photos you can use for free, but also a simple API (Application Programming Interface) that allows you to put the images to a variety of creative uses with a minimum of effort. For example, if you happen to use Ubuntu, you can whip up a simple Bash script that fetches a random photo from a specified Unsplash user and sets it as a desktop background:

uuser="Unsplash username"
size=$(xdpyinfo | grep dimensions | awk '{print $2}')
curl "https://source.unsplash.com/user/$uuser/$size" --location --output $path/unsplash-random.jpeg
gsettings set org.gnome.desktop.background picture-uri file://$path/unsplash-random.jpeg

The key part of the script is the command that uses the cURL tool to fetch a random photo via an HTTP call. The call requires two parameters: the Unsplash user name and image height and width. The obtain the latter, the xdpyinfo | grep dimensions | awk '{print $2}' command uses a combination of xdpyinfo grep and awk tools.

The https://source.unsplash.com/user API call redirects to the actual URL of a random photo. Fortunately, cURL supports the --location option that can handle this. To save the fetched photo under the unsplash-random.jpeg name, cURL uses the --output option. Finally, the last command in the script sets the fetched photo as the desktop background.

To deploy the script, paste the code above into a new text file, replace the placeholders of the user and path variables with the actual Unsplash user name and the target directory path, then save the file under the unsplash-random.sh name. Make then the script executable using the chmod +x unsplash-random.sh command. Run then the ./unsplash-random.sh command in the terminal and behold the new desktop background.

Tech writer covering Linux and open source software

Tagged with: , , ,
Posted in Software
%d bloggers like this: