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 "<" $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 OS 15 Sequoia
# osascript -e 'tell application "System Events" to set picture of every desktop to "'"$path/wallpaper.jpg"'"'
# 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}"
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
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
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.
Thanks for this! I created APODesktop with support for macOS, and recently X+Linux and Windows. This also has some more features than your script, but also misses some features.
The main feature this adds is support for multiple desktops 🙂