Skip to content

Instantly share code, notes, and snippets.

@mtking2
Last active September 1, 2022 23:40
Show Gist options
  • Save mtking2/c38948f731d9017fee4edb36667c2a70 to your computer and use it in GitHub Desktop.
Save mtking2/c38948f731d9017fee4edb36667c2a70 to your computer and use it in GitHub Desktop.
NASA APOD wallpaper script

NASA APOD wallpaper for Unix systems

script

Save to a file wherever it please you (I saved it to ~/scripts/apod.sh)
Maybe make sure it is executable: chmod 755 ~/scripts/apod.sh

#!/bin/bash
# apod.sh

green="\033[32m"
white="\033[0m"
blue="\033[34m"

path="$( cd "$(dirname "$0")" ; pwd -P )/apod.d"
# echo $path
mkdir -p $path
rm -f $path/wallpaper.jpg $path/index.html
# rm -f /Library/Caches/com.apple.desktop.admin.png # clear cached image for lockscreen on OSX
wget https://apod.nasa.gov/apod/ -P $path
img="$(grep "IMG SRC=" $path/index.html | cut -d\" -f2)"

while getopts d: flag; do
  case "${flag}" in
    d) DATE=${OPTARG};;
  esac
done

if [[ $DATE ]]; then
  # get APOD from a specific date (YYMMDD) with the -d flag. e.g. -d 201107
  previous_apod="https://apod.nasa.gov/apod/ap$DATE.html"
  echo -e "${blue}using APOD from $DATE ${white}"
  wget $previous_apod -O $path/index.html
  img="$(grep "IMG SRC=" $path/index.html | cut -d\" -f2)"
elif [[ -z "$img" ]]; then
  # sometimes the APOD is an iframe containing a video/flash/js element
  # this condition should detect the absence of an image and loop through previous APODs until it finds an image
  days=0
  while
    days=$((days+1))
    yesterday="https://apod.nasa.gov/apod/$(grep "&lt" $path/index.html | cut -d\" -f2)"
    echo -e "${blue}Trying image from $days day(s) ago: $yesterday ${white}"
    wget $yesterday -O $path/index.html
    img="$(grep "IMG SRC=" $path/index.html | cut -d\" -f2)"
    echo "image: $img"
    [ -z "$img" ]
  do true; done
fi

# deal with external images
if [[ $img =~ ^https?://.*$ ]]; then
  url=$img
else
  url="https://apod.nasa.gov/apod/$img"
fi

wget $url  -O $path/wallpaper.jpg

###############################################################################
# uncomment whichever commands below apply to your desktop environment

# Mac OSX 10.12/10.13 (Sierra/High Sierra)
# sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '$path/wallpaper.jpg'" && killall Dock

# GNOME
# possible values for gsettings: "wallpaper", "centered", "scaled", "stretched", "zoom", "spanned"
gsettings set org.gnome.desktop.background picture-uri "File://$path/wallpaper.jpg"
gsettings set org.gnome.desktop.background picture-options scaled
gsettings set org.gnome.desktop.background primary-color '#111'

# Cinnamon
# gsettings set org.cinnamon.desktop.background picture-uri "file://$path/wallpaper.jpg"
# gsettings set org.cinnamon.desktop.background picture-options zoom

# Xfce
# xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitor0/image-path --set $path/wallpaper.jpg

# feh
# feh --bg-scale $path/wallpaper.jpg

echo -e "$(date "+%Y-%m-%d %H:%M:%S") done ${green}${white}"

Advanced background options for OSX

Per this stack overflow answer, the command below will set the background image for all of the spaces with a 7% black fill color and a scale setting of "Fit to Screen". Check out the SO post, customize to your liking, and replace in the script above :)

sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db " \
  DELETE FROM data; DELETE FROM displays; DELETE FROM pictures; \
  DELETE FROM preferences; DELETE FROM prefs; DELETE FROM spaces; \
  INSERT INTO pictures (space_id, display_id) VALUES (null, null); \
  INSERT INTO data (value) VALUES ('$path/wallpaper.jpg'); \
  INSERT INTO data (value) VALUES (5); \
  INSERT INTO data (value) VALUES (0.07); \
  INSERT INTO preferences (key, data_id, picture_id) VALUES (1, 1, 1); \
  INSERT INTO preferences (key, data_id, picture_id) VALUES (2, 2, 1); \
  INSERT INTO preferences (key, data_id, picture_id) VALUES (3, 3, 1); \
  INSERT INTO preferences (key, data_id, picture_id) VALUES (4, 3, 1); \
  INSERT INTO preferences (key, data_id, picture_id) VALUES (5, 3, 1); \
" && killall Dock

cron

Edit crontab to add a job to run everyday.
crontab.guru may be useful for you cron noobs

crontab -e

# everyday @ 9am
0 9 * * * /bin/bash ~/scripts/apod.sh
# on startup
@reboot /bin/bash ~/scripts/apod.sh

If you want some logging:
mkdir -p ~/scripts/apod.d && touch ~/scripts/apod.d/cron.log and update your crontab with the stdout redirect:

# everyday @ 9am
0 9 * * * /bin/bash ~/scripts/apod.sh >> ~/scripts/apod.d/cron.log 2>&1
# on startup
@reboot /bin/bash ~/scripts/apod.sh >> ~/scripts/apod.d/cron.log 2>&1

Important note for OSX

On OSX I had to add a PATH variable to the top of my crontab so cron could have visibility to certain tools like wget and sqlite3 (wget was installed with brew install wget btw). Then crontab should look like:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# everyday @ 9am
0 9 * * * /bin/bash ~/scripts/apod.sh
# on startup
@reboot /bin/bash ~/scripts/apod.sh

Relevant article that gave me the idea and gist that got it working in OSX.

@T31337
Copy link

T31337 commented Apr 27, 2018

No Command For KDE Users?


https://www.reddit.com/r/kde/comments/65pmhj/change_wallpaper_from_terminal/

*Replace /PATH/TO/IMAGE.png with appropriate path to wallpaper.

dbus-send --session --dest=org.kde.plasmashell --type=method_call /PlasmaShell org.kde.PlasmaShell.evaluateScript 'string:
var Desktops = desktops();
for (i=0;i<Desktops.length;i++) {
d = Desktops[i];
d.wallpaperPlugin = "org.kde.image";
d.currentConfigGroup = Array("Wallpaper",
"org.kde.image",
"General");
d.writeConfig("Image", "file:///PATH/TO/IMAGE.png");
}'

@vegerot
Copy link

vegerot commented Sep 1, 2022

I recently had my own take on this for macOS https://github.com/vegerot/APODesktop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment