summaryrefslogtreecommitdiff
path: root/scripts/random-wallpaper.sh
blob: 03fb160228241d5d6fde8e0de10d2078f195c4d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/sh
# Randomly sets a GNOME desktop wallpaper from image files in a given directory.

WALLPAPER_DIR="$HOME/Pictures/Pixel7ProDCIM/Irregular Ninja/irregular.ninja"

if [ ! -d "$WALLPAPER_DIR" ]; then
    printf 'Directory not found: %s\n' "$WALLPAPER_DIR" >&2
    exit 1
fi

# Pick a random image file (common extensions)
IMAGE=$(find "$WALLPAPER_DIR" -maxdepth 1 -type f \
    \( \
    -iname '*.jpg' -o \
    -iname '*.jpeg' -o \
    -iname '*.png' -o \
    -iname '*.bmp' -o \
    -iname '*.webp' -o \
    -iname '*.gif' \
    \) | sort -R | head -n 1)

if [ -z "$IMAGE" ]; then
    printf 'No images found in %s\n' "$WALLPAPER_DIR" >&2
    exit 1
fi

# GNOME Shell caches wallpaper textures keyed by URI.
# If the path is always the same, Shell won't reload even when the file
# contents change. Use a unique filename on every run so the URI changes
# and Shell is forced to load the new image.
CLEAN_DIR="$HOME/.local/share/wallpapers"
UNIQUE="random-wallpaper-$(date +%s)"
CLEAN_PATH="$CLEAN_DIR/$UNIQUE.jpg"

mkdir -p "$CLEAN_DIR"
cp "$IMAGE" "$CLEAN_PATH"

# Clean up old copies to avoid filling the directory
# Keep the last 5 wallpapers around
ls -1t "$CLEAN_DIR"/random-wallpaper-*.jpg 2> /dev/null | sed -n '6,$p' | xargs -r rm -f

# dconf-service on Fedora 50 has a bug where gsettings set keeps changes in
# memory but never syncs them to the on-disk database.  GNOME Shell reads
# directly from disk, so it never sees wallpaper changes made with gsettings.
# Writing via dconf write bypasses the buggy service and hits the database
# directly, making GNOME Shell pick up the change immediately.
dconf write /org/gnome/desktop/background/picture-uri "'file://$CLEAN_PATH'"
dconf write /org/gnome/desktop/background/picture-uri-dark "'file://$CLEAN_PATH'"

# Show me what was set
printf 'Set wallpaper to: %s (copied from %s)\n' "$CLEAN_PATH" "$IMAGE"