Skip to content

Instantly share code, notes, and snippets.

@zumikkebe
Last active March 22, 2024 13:48
Show Gist options
  • Save zumikkebe/ebdadf5ae49c7da8ab79ed375bd2fb19 to your computer and use it in GitHub Desktop.
Save zumikkebe/ebdadf5ae49c7da8ab79ed375bd2fb19 to your computer and use it in GitHub Desktop.
#! /bin/sh
# credits: https://stackoverflow.com/questions/10639914/is-there-a-way-to-get-bings-photo-of-the-day
preferred_location=${HOME} # this is where is stored the downloaded wallpaper, edit to your preferred location
# background's preflet app signature
app_sig="x-vnd.Haiku-Backgrounds"
# check if augiedoggie's tool 'bgswitch' is installed, hint: get it from https://github.com/augiedoggie/bgswitch
bg_switch=`which bgswitch`
# get local path to use the script's icon in notifications
local_path=`dirname "${0}"`
app_name=`basename "${0%}"`
[ "${local_path}" == "." ] && local_path="`pwd`"
## notification ##
notify --group $"Bing's Daily Image" --title $"Fetching…" --messageID ${app_name} --icon "${local_path}/${app_name}" ""
# fetching BING's RSS into /tmp
curl -s -o /tmp/bing_rss "https://www.bing.com/HPImageArchive.aspx?format=rss&idx=0&n=1&mkt=en-US"
# get the date of publication (to use for for the wallpaper's name)
bing_wp="bing_wp-$(date +%y%m%d -d "$(xmllint --xpath "/rss/channel/item/pubDate/text()" /tmp/bing_rss | awk '{print $1$2$3$4}')")"
# exit if this entry has been already set as the current wallpaper
[ ! -z ${bg_switch} ] && [[ `bgswitch --all list | grep ${bing_wp}` ]] || [[ `catattr be:bgndimginfo $(finddir B_DESKTOP_DIRECTORY) | grep ${bing_wp}` ]] && notify --group $"Bing's Daily Image" --title $"Nothing to update" --messageID ${app_name} --icon "${local_path}/${app_name}" "" && exit
# if does not already exist, download the daily bing wallpaper into your preferred location
[ -d "${preferred_location}" ] || mkdir -p "${preferred_location}"
if [ ! -e "${preferred_location}/${bing_wp}" ] ; then
## notification ##
notify --group $"Bing's Daily Image" --title $"Downloading…" --messageID ${app_name} --icon "${local_path}/${app_name}" ""
curl -s -o "${preferred_location}/${bing_wp}" "https://www.bing.com`xmllint --xpath "/rss/channel/item/link/text()" /tmp/bing_rss`"
# put the wallpaper's description into an attribute
addattr Media:Comment "`xmllint --xpath "/rss/channel/item/title/text()" /tmp/bing_rss`" "${preferred_location}/${bing_wp}"
fi
## notification ##
notify --group $"Bing's Daily Image" --title $"Applying…" --messageID ${app_name} --icon "${local_path}/${app_name}" ""
if [ ! -z ${bg_switch} ] ; then
bgswitch --all set --mode 3 --file "${preferred_location}/${bing_wp}"
else
# open backgrounds preflet with the dowloaded file
open application/${app_sig} "${preferred_location}/${bing_wp}"
# credits to Máximo Castañeda aka madmax for this hint (https://discuss.haiku-os.org/t/haiku-scripting/)
# apply it to all workspaces
hey -o application/${app_sig} do MenuItem "`dumpcatalog /boot/system/data/locale/catalogs/${app_sig}/${LC_MESSAGES%[._]*}.catalog 2>/dev/null | awk -F'\t' '/All workspaces/{getline;print}' | cut -f2`" of View 0 of View 'workspaceMenuField' of Window 0
# scale it to fit the whole screen
hey -o application/${app_sig} do MenuItem "`dumpcatalog /boot/system/data/locale/catalogs/${app_sig}/${LC_MESSAGES%[._]*}.catalog 2>/dev/null | awk -F'\t' '/Scale to fit/{getline;print}' | cut -f2`" of View 0 of View 'placement' of Window 0
# apply the selection
hey -o application/${app_sig} 'aply' of View 0 of Window 0
# quit Backgrounds preflet
hey -o application/${app_sig} 'quit'
fi
## notification ##
notify --group $"Bing's Daily Image" --title $"Done" --messageID ${app_name} --icon "${local_path}/${app_name}" ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment