Background Image Manager

2024-01-01

Space. Something every American loves, and if you get a little too close we'll let you know. Wait — That's personal space... wrong subject. I mean outer space.

I recently began to get annoyed by the standard Linux background everytime I watch youtube or log into my VM. So I decided to learn how to set a background image.

If you're using GNOME, this simple script can update your background for you. If you have a different environment, a quick google search should get you going.

gsettings set org.gnome.desktop.background picture-uri <path>

I stumbled across NASA's APIs and noticed that they have one that published images of space daily (APOD). With a couple curl commands and a jq command I have the image downloaded to my computer. First create an account on NASA's API website and put your key in a file called NASA.key

# Load the API key.
API_KEY=$(cat ./NASA.key)

# Retrieves metadata about the image and a url containing the path to the image.
JSON_RESP=$(curl https://api.nasa.gov/planetary/apod?api_key=$BG_SCRIPT_API_KEY)

# Utilize jq to deserialize the JSON response and extracts the `url` field.
IMG_URL=$(echo $JSON_RESP | jq -r '.url')

# Fetch the images and place it in a file
curl -o <path/to/save/image/at> $<IMG_URL>

gsettings set org.gnome.desktop.background picture-uri <path>

Sick! now I've got the image... But I want to get a new image every day, thats what the intent of the API is after all. Lets create a cron task.

crontab -e allows us to create a new cron job, and we can enter the job with the format:

* * * * path/to/script.sh

I decided on using * 4 * * * /path/to/script.sh which will update the background every day at 4am.

Bash usually keeps variables scoped to the script that is running it. However, when we run this script, the session will be persisted across the entire login session. So if we rewrite an existing variable, it will changee the session's IMG*URL or another set variable. An easy way around this is to prefix the variables with something like BG_IMG_SCRIPT_ to avoid name collisions.

API_KEY=$(cat ./NASA.key)
JSON_RESP=$(curl https://api.nasa.gov/planetary/apod?api_key=$API_KEY)
IMG_URL=$(echo $JSON_RESP | jq -r '.url')
curl -o <path/to/save/image/at> $<IMG_URL>
gsettings set org.gnome.desktop.background picture-uri <path>

# this sets the background when you're in dark mode
gsettings set org.gnome.desktop.background picture-uri-dark $IMG_PATH

Hold on... When using a VM I can't run a cron job if the computer is turned off... What if I set the script to run on login as well? Okay... So now let's do some more changes. run cd ./config/autostart and copy paste this in. Be sure to update the Exec variable with the path to the file you're working in.

[Desktop Entry]
Name=bg_img_update
Comment=Fetches the NASA APOD image for today and changes that image to the background image.
X-GNOME-Autostart-enabled=true
Type=Application
Exec=/home/<user>/<path_to_directory>/script.sh
Hidden=false
NoDisplay=false