background/changebackground.sh

54 lines
1.8 KiB
Bash
Raw Normal View History

#!/bin/bash
# This script is used to change the background of the desktop
2025-05-23 16:19:51 +02:00
# 2025, Raoul Branten
2025-05-23 16:19:51 +02:00
# Function to show a spinning cursor
spinner() {
local pid=$1
local delay=0.1
local spinstr='|/-\'
while [ "$(ps a | awk '{print $1}' | grep $pid)" ]; do
local temp=${spinstr#?}
printf " [%c] " "$spinstr"
local spinstr=$temp${spinstr%"$temp"}
echo -en "\b\b\b\b\b\b"
sleep $delay
done
printf " \b\b\b\b"
}
2025-05-23 16:19:51 +02:00
echo "[1/4] Getting screen resolution..."
# get screen resolution using xdpyinfo to get info from X server
SCREENWIDTH=$(xdpyinfo | awk '/dimensions/{print $2}' | grep -oE ^[[:digit:]]\+)
SCREENHEIGHT=$(xdpyinfo | awk '/dimensions/{print $2}' | grep -oE [[:digit:]]\+$)
SIZE="${SCREENWIDTH}x${SCREENHEIGHT}"
2025-05-23 16:19:51 +02:00
echo " Screen resolution: $SIZE"
2025-05-23 16:19:51 +02:00
echo "[2/4] Checking for xstarfish..."
# check if xstarfish is installed
if [ -f /usr/bin/xstarfish ]; then
2025-05-23 16:19:51 +02:00
echo "[3/4] Generating background image..."
# Generate a background image using xstarfish
/usr/bin/xstarfish -g $SIZE -o /tmp/background.jpg &
spinner $!
else
2025-05-23 16:19:51 +02:00
echo "ERROR: xstarfish not found"
exit 1
fi
2025-05-23 16:19:51 +02:00
echo "[4/4] Setting background on all displays..."
# get all possible displays
# use xfconf-query to generate a list of all possible displays
# then use grep to cut out the correct path
# sort is used for unique results
BACKDROP=$(xfconf-query --channel xfce4-desktop --list | grep -E -o '/backdrop/screen0/monitor[a-zA-Z0-1-]+/workspace0/last-image' | sort -u)
# foreach display set the background image to the generated image
while IFS= read -r line; do
# set the background image
xfconf-query -c xfce4-desktop -p $line -s /tmp/background.jpg
echo " Set background for display: $line"
done <<< "$BACKDROP"
echo "✓ Background change completed successfully!"