Skip to content

Instantly share code, notes, and snippets.

@ecspresso
Created October 30, 2023 14:08
Show Gist options
  • Save ecspresso/61927c3650f4e6070190e7b8f71207d5 to your computer and use it in GitHub Desktop.
Save ecspresso/61927c3650f4e6070190e7b8f71207d5 to your computer and use it in GitHub Desktop.
Change background image with cron, using images from a folder where the current background image is.
#!/bin/bash
PID=$(pgrep gnome-session | tail -n1)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$PID/environ | tr '\0' '\n' |cut -d= -f2-)
current_image=$(gsettings get org.gnome.desktop.background picture-uri | xargs echo)
if [ "$1" == "echo" ]; then
echo $current_image
exit 0
fi
folder=$(dirname "$current_image")
filename=$(basename "$current_image")
last_image=$(ls -v $folder | tail -n 1)
if [ "$1" == prev ]; then
first_image=$(ls -v $folder | head -n 1)
if [ "$first_image" == "$filename" ]; then
next_image=$folder/$(ls -v $folder | head -n 1)
else
next_image=$folder/$(ls -v $folder | grep -B 1 $filename | awk 'NR==1')
fi
elif [ "$filename" == "$last_image" ]; then
next_image=$folder/$(ls -v $folder | head -n 1)
else
next_image=$folder/$(ls -v $folder | grep -A 1 $filename | awk 'NR==2')
fi
gsettings set org.gnome.desktop.background picture-uri $next_image
if [ "$1" == "trash" ]; then
rm $current_image
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment