我有这个脚本,它使用与 cron 作业关联的 gsettings 每隔几分钟将我的背景图像更改为随机变暗的图像。
#!/bin/bash
# Define the directory containing the images
IMAGE_DIR="/home/me/images"
# Select a random image from the directory
IMAGE_FILE=$(find "$IMAGE_DIR" -type f | shuf -n 1)
# Define the output path for the processed image
OUTPUT_IMAGE="/tmp/background.png"
# Adjust the brightness (reducing it, e.g., by 70%)
convert "$IMAGE_FILE" -fill black -colorize 70% "$OUTPUT_IMAGE"
# For Wayland sessions (common in Ubuntu 22.04 and later)
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$(id -u)/bus
# Set the processed image as the background
gsettings set org.gnome.desktop.background picture-uri-dark "file://$OUTPUT_IMAGE"
现在,一旦系统启动并运行,这通常就可以正常工作,但每次我重新启动系统时,背景都是黑色的。一旦我在设置应用程序中手动设置背景,它就会再次开始工作,因此 cron 作业和脚本都很好。脚本也运行正常,桌面只是保持黑色。
我发现的解决方案是首先使用相同的设置但使用一个空参数,如下所示:
# Set the processed image as the background
gsettings set org.gnome.desktop.background picture-uri-dark ""
gsettings set org.gnome.desktop.background picture-uri-dark "file://$OUTPUT_IMAGE"
但是,这种解决方法并不是很好,因为图像之间的切换现在以奇怪的方式中断了。我不得不求助于两个脚本解决方案,在启动时将背景设置为无,然后让 cron 作业处理其余部分。但在我看来,这似乎是一个错误。有什么想法是故意的吗?
PS:目前我已经修改了解决方法,以便它检查临时文件夹中是否已经存在背景图像,尽管我还没有真正测试它。
#!/bin/bash
# Define the directory containing the images
IMAGE_DIR="/home/me/images"
# Select a random image from the directory
IMAGE_FILE=$(find "$IMAGE_DIR" -type f | shuf -n 1)
# Define the output path for the processed image
OUTPUT_IMAGE="/tmp/background.png"
# Check if the script is running for the first time in the session
if [ ! -f "$OUTPUT_IMAGE" ]; then
# Set the background to nothing first
gsettings set org.gnome.desktop.background picture-uri-dark ''
# Wait a bit for the reset to take effect
sleep 1
fi
# Adjust the brightness (reducing it, e.g., by 70%)
convert "$IMAGE_FILE" -fill black -colorize 70% "$OUTPUT_IMAGE"
# Set the actual background image
gsettings set org.gnome.desktop.background picture-uri-dark "file://$OUTPUT_IMAGE"