[SOLVED] [Xfce4.6] A script to rotate the backdrop image
Linux - DesktopThis forum is for the discussion of all Linux Software used in a desktop context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My nice, dual-processor, 64bit laptop decided a few days ago to decline to respond to the power-on button. Being of somewhat strained means, I couldn't afford a new laptop (nor the repair cost of the old one). So I decided to resurrect an ten year old XP desktop system by installing Fedora 14 on it with, of course, Xfce 4.6 as the desktop system. One of the features I miss from the KDE desktop I was using on the laptop was the ability to automatically rotate the backdrop image. After a little search I found a script which purported to do the rotation, but it was written for Xfce 4.4, and failed on 4.6. So I tweaked it a bit, and got the following script to work.
Code:
#!/bin/bash
# Wallpaper changer for Xfce 4.6-${HOME}/Pictures/:/usr/share/backgrounds/
#
# Usage:
#
# Changer {monitor {interval {source1{:source2{:...}}}}}
#
# Where:
# Changer is the name you give to the script
# monitor is the device number of the monitor for which you want to rotate the background images.
# Default: 0 (Which is the only possibility if you have only one monitor on your system.)
# interval is the amount of time a backdrop should be displayed before it's replaced.
# Default: Set as ${Display_Time}, below (One minute in this instance.)
# source is a colon-separated list of directories (which may have sub-directories) containing
# the images to be used as backdrops.
# Default: Set in ${Wallpapers}, below. (/Wallpapers/ in this instance.)
# (Try Wallpapers="${2:-${HOME}/Pictures/:/usr/share/backgrounds/"} to use your Pictures
# directory and the system backdrop images.)
#
# Note: Some debugging output can be produced by setting ${debug} to a non-null value. (See near the end of
# of this code where the setting of that value is commented out.)
#
# This script may be used as a command by copying it into the ~/Desktop directory. (Xfce will generate a
# script icon, and execute the script when you double-click on the icon.) The default values you set will
# be used unless you create an actual Changer.desktop file to run the script with some arguments. But, if
# you want to try it out before installing it, just run it from a terminal with whatever test arguments you
# want to try. (I found "./Changer 0 5s "${HOME}/Pictures:/usr/share/backgrounds/nature" useful.)
#
# Function to do the actual work
wallpaper_loop() {
# Default the display time to one minute if not specified
Display_Time="${1:-1m}"
# Default the background directory list to /Wallpapers/
Wallpapers="${2:-/Wallpapers/}"
[ -n "${debug}" ] && echo "Starting wallpaper_loop of \"${Wallpapers}\" for ${Display_Time}"
# Make sure that the property we want to change is actually a property
PROPERTY="/backdrop/screen0/monitor${MONITOR:-0}/image-path"
IMAGE_PATH="$(xfconf-query -c xfce4-desktop -p ${PROPERTY})"
[ $? -ne 0 ] && echo "${PROPERTY} is not a recognized xfconf-query parameter." && exit 1
# Clean up any old wallpaper lists
if [ -n "${debug}" ]
then
LIST="$(find /tmp/ -maxdepth 1 -name wallpaper_list_${MONITOR:-0}*)"
[ -n "${LIST}" ] && echo "Deleting the following temporary file: ${LIST}"
fi
rm -f /tmp/wallpaper_list_${MONITOR:-0}*
# Get a new temporary file name for the new list
LIST=$(mktemp /tmp/wallpaper_list_${MONITOR:-0}_XXXX)
[ -n "${LIST}" ] && echo "Creating the following temporary file: ${LIST}"
# Create the new list.
IFS=":"
for dir in ${Wallpapers}
do
[ ! -d "${dir}" ] && echo "\"${dir}\" is not a directory. Skipping." && bad=$((${bad}+1)) && continue
[ -n "${debug}" ] && echo " Adding ${dir} to ${LIST}"
find "${dir}" -type f -printf "%h/%f\n" >> ${LIST}
done
[ -n "${bad}" ] && echo "Aborting because of bad directory name(s)."&& exit 1
# Since the file names may contain blanks or other special characters
# set the field delimiter to a new line
IFS=$'\n'
# Start an infinite loop
while true
do
# Get a random file name
NAME="$(shuf -n 1 ${LIST} 2>/dev/null)"
[ $? -ne 0 ] && exit 1
# Verify that the file is a JPEG or PNG file
[ -z "$(file "${NAME}" | grep -iwE '(jpeg|png)')" ] && continue
# Set it as the background image
xfconf-query -c xfce4-desktop -p ${PROPERTY} -s ${NAME}
# and do it again after displaying it for ${Display_Time}
sleep ${Display_Time}
done
}
# Uncomment the next line for debugging output
#debug=y
#
# Minor housekeeping
#
# Default to changing the background on the first monitor unless passed an argument
MONITOR=${1:-0}
shift
# Make sure the system variable, DISPLAY, is defined
if [ -z "$DISPLAY" ]
then
export DISPLAY=":0.0"
fi
# Pass any remaining arguments to the loop function
wallpaper_loop "$@"
(I note that Xfce 4.8 has been released, but the Fedora people have only 4.6 in the repositories. Hopefully, this code will work on later Xfce releases, or the problem with backdrop lists will be - or has been - fixed.)
Thanks for sharing but Xfce 4.6 can cycle its own wallpapers:
Code:
Settings -> Desktop -> Background
Image list: selected
+ (to add images to the list)
Browsed to /home/c/d/.wallpapers
Ctrl+A to select all
Add
Style: Zoomed
This created /home/c/.config/xfce4/desktop/backdrop.list which has an initial comment introduced with a # and then a list of backdrop files, one per line with no special provision for embedded spaces.
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 386
Rep:
Well while we're sharing scripts; here's the one I wrote to shuffle wallpaper for XCFE v4.6.2.
Code:
#!/bin/bash
#: Title: shuffle-wallpaper-xfce4
#: Author: Neal T. Bailey <nealbailey@hotmail.com>
#: Date: 01/18/2011
#: Purpose: Randomly set a wallpaper (XFCE v4+)
#
#: Usage: ./shuffle-wallpaper-xfce4.sh [options]
#: Options: -d, --debug - print to stdout/log
#: -v, --version - print version info
#: -h, --help - print help synopsis
#: -p, --path [dir] - define image path
#:
# Notes: This script is best when run in a cron job to randomly set background images
# on a scheduled.
#
# Example:
# $ crontab -e
#
# Add this line into user crontab to change background every 30 minutes
# */30 * * * * /home/neal.bailey/shuffle-wallpaper-xfce4.sh
#
# Allows script to run as a cron job
export DISPLAY=:0.0
# Metadata
scriptname=${0##*/}
description="Apply random a background image"
usage="$scriptname [-d|-v|-h|-p]"
optionusage="-d:\tEnable debug logging\n -v:\tPrint version info\n -h:\tPrint help (this screen)\n -p:\tImage path [dir]\n"
date_of_creation=2011-01-18
version=1.0
author="Neal T. Bailey"
# Default Values
DEBUG=0
LOG="$0.debug"
LINE_PREFIX="[$(date +%m/%d/%Y' '%H:%M)]"
base="$HOME/Pictures" # wallpapers
# Functions
usage() #@ DESCRIPTION: print usage information
{ #@ USAGE: usage
printf "%s - %s\n" "$scriptname" "$description"
printf "Usage: %s\n" "$usage"
printf "%s $optionusage"
}
version() #@ DESCRIPTION: print version information
{ #@ USAGE: version
printf "%s (v%s)\n" "$scriptname" "$version"
printf "by %s, %d\n" "$author" "${date_of_creation%%-*}"
}
# Options the program will accept
optstring=p:dhv
while getopts $optstring opt
do
case $opt in
p) base=$OPTARG ;;
d) DEBUG=$(( $DEBUG + 1 )) ;;
h) usage; exit ;;
v) version; exit ;;
*) exit 1 ;;
esac
done
shift "$(( $OPTIND - 1 ))"
# Sanity checks
if [ ! -d "$base" ] || [ ! -e "$base" ]; then
echo "error: $base doesn't exist or isn't a directory!"
exit 1
fi
# Change the default IFS from \n\t\0 to ;
IFS=';'
if [ "$DEBUG" -gt 0 ]; then
echo "$LINE_PREFIX $0 began processing." > "$LOG"
fi
# Populate array with jpg/png files and append the new IFS (;) to each result
images=( $(find "$base" -printf %p\; -iname *jpg -or -iname *.png) )
# Get the upper bounds of the array
ubound=${#images[@]}
if [ "$DEBUG" -gt 0 ]; then
echo "$LINE_PREFIX Enumerating images in $base." | tee -a "$LOG"
echo "$LINE_PREFIX find ~/Pictures -printf %p\; -iname *jpg -or -iname *.png" | tee -a "$LOG"
echo "$LINE_PREFIX Search returned $ubound images." | tee -a "$LOG"
fi
if [ "$ubound" -eq 0 ]; then
status="There were no images found. Cannot continue operation."
echo "$status."
exit 1
fi
# Pick a random wallpaper
selected="${images[RANDOM % ubound]}"
if [ "$DEBUG" -gt 0 ]; then
echo "$LINE_PREFIX Selected random image $selected." | tee -a "$LOG"
fi
# Set the wallpaper
xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s "$selected" 2>>"$LOG"
if [ "$DEBUG" -gt 0 ]; then
echo "$LINE_PREFIX Instructing xfce to set new wallpaper." | tee -a "$LOG"
echo "$LINE_PREFIX xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s $selected" | tee -a "$LOG"
fi
echo "Operation completed successfully."
exit 0
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 386
Rep:
Quote:
Originally Posted by catkin
It works for me but, as you write, does not have an adjustable dwell time.
Yea, I prefer to set the rotation interval in the crontab personally. I have a lot of wallpapers that I really like one hour but don't like the next hour. Structuring the code in this manner (where the user sets up rotation in cron) lets me run the script over and over again when the automated process picks a background that doesn't match my mood at the time!
Hi thund3rstruck, that's a nice script, and I hadn't considered using crontab which is obvious now that you mention it. Note, however, the way the script I posted works: It sets the backdrop, sleeps for the specified interval, and does it again. But, as it runs, it checks for its file list in /tmp, and aborts if it isn't found. When it's started, it first deletes any /tmp lists, and then creates a new one.
Since it's started from a desktop icon, if I don't like a backdrop, all I need to do is click on the icon to start a new process which will immediately change the backdrop and cause the prior process to abort when it next wakes up. I don't know if that's any better than using crontab, but it seems to me that it's at least an alternate technique, and a sleeping job doesn't use to many resources.
Xfce4.16 has a working app to do this, but it's somewhat non intuitive
I don't know when the developers added a working background setting app to Xfce, but it has a user interface that took me several days to discover how to get it to work.
The "secret" is that the box labeled "Folder:" with a (working) pull-down toggle on the right side, is also a drop box. So, if you want to set a folder as a set of wallpaper images, all you need to do is copy the folder into that box.
(The pop-up prompt "Choose the folder to select wallpapers from.", besides being grammatically flawed, suggests that the pull-down process would do more then display top-level directory names.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.