Skip to content

Instantly share code, notes, and snippets.

@dietrichmax
Forked from Aleksandr-ru/windows-spotlight-wp.sh
Last active February 1, 2025 23:23
Show Gist options
  • Save dietrichmax/8b97b4823dcad1ee846636c5f992767c to your computer and use it in GitHub Desktop.
Save dietrichmax/8b97b4823dcad1ee846636c5f992767c to your computer and use it in GitHub Desktop.
Windows spotlight wallpaper changer for Linux (Elementary OS) and macOS
#!/usr/bin/env bash
# Elementary OS specific:
# 1) edit file "/etc/lightdm/io.elementary.greeter.conf"
# uncomment line 3 "#default-wallpaper=/usr/share/backgrounds/elementaryos-default"
# 2) change symbolic link "/usr/share/backgrounds/elementaryos-default" to "~/windows-spotlight-wp/last.jpg"
function checkret
{
if [ $1 -ne 0 ]; then
echo $2
exit $1
fi
}
function set_wp_linux
{
path="$1"
title="$2"
copyright="$3"
PID=$(pgrep gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ | tr -d '\000' | cut -d= -f2-)
gsettings set "org.gnome.desktop.background" picture-options "zoom"
gsettings set "org.gnome.desktop.background" picture-uri "file://$path"
# Elementary OS remove current greeter wallpaper
user=$(whoami)
rm -f /var/lib/lightdm-data/$user/wallpaper/*
if [ ! -z "$title" ]; then
notify-send "New wallpaper" "$title\n$copyright" --icon=preferences-desktop-wallpaper --urgency=low
fi
}
function set_wp_mac
{
path="$1"
title="$2"
copyright="$3"
osascript -e 'tell application "Finder" to set desktop picture to POSIX file "$path"'
if [ ! -z "$title" ]; then
osascript -e 'display notification "$copyright" with title "New wallpaper" subtitle "$title"'
fi
}
JQ=$(which jq)
checkret $? "jq not found, please install it with 'apt install jq' for Linux or 'brew install jq' for macOS"
echo -n $(date +%Y-%m-%d\ %H:%M:%S)" "
DIR=~/windows-spotlight-wp
URL="https://arc.msn.com/v3/Delivery/Placement?pid=209567&fmt=json&cdm=1&lc=de,de-DE&ctry=GER"
json=$(curl -f -s $URL)
checkret $? "Failed to load JSON"
item=$(jq -r ".batchrsp.items[0].item" <<< $json)
checkret $? "Failed to parse item"
landscapeUrl=$(jq -r ".ad.image_fullscreen_001_landscape.u" <<< $item)
sha256=$(jq -r ".ad.image_fullscreen_001_landscape.sha256" <<< $item | base64 -d | hexdump -ve "1/1 \"%.2x\"")
title=$(jq -r ".ad.title_text.tx" <<< $item)
copyright=$(jq -r ".ad.copyright_text.tx" <<< $item)
path="$DIR/$sha256.jpg"
if [ -f $path ]; then
title=""
copyright=""
echo "File already exists!"
else
mkdir -p $DIR
curl -s -f "$landscapeUrl" --output "$path"
checkret $? "Failed to download wallpaper"
echo "New wallpaper file"
fi
ln -sf $path $DIR/last.jpg
uname=$(uname -s)
case "$uname" in
Darwin)
set_wp_mac "$path" "$title" "$copyright"
;;
Linux)
set_wp_linux "$path" "$title" "$copyright"
;;
*)
echo "Unknown OS '$uname'"
exit 1
;;
esac
echo $path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment