LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 04-29-2011, 10:43 PM   #1
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Smile [Xfce4.6] A script to rotate the backdrop image


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.)
 
Old 04-30-2011, 10:12 AM   #2
RockDoctor
Senior Member
 
Registered: Nov 2003
Location: Minnesota, US
Distribution: Fedora, Ubuntu, Manjaro
Posts: 1,791

Rep: Reputation: 427Reputation: 427Reputation: 427Reputation: 427Reputation: 427
The Fedora 15 repos have XFCE 4.8. F15 is still beta, but the F15 LXDE spin has been quite problem-free for a while.
 
Old 04-30-2011, 11:43 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.
 
Old 04-30-2011, 12:04 PM   #4
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Original Poster
Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
catkin, I tried that before I wrote the script and found that it
  1. didn't actually work and
  2. didn't alow me to specify a "dwell" time, even if it had worked.
You experience may be different. (Perhaps it just the decade-old hardware I'm using . . . )
 
Old 04-30-2011, 05:07 PM   #5
thund3rstruck
Member
 
Registered: Nov 2005
Location: East Coast, USA
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 386

Rep: Reputation: 43
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
 
Old 04-30-2011, 11:18 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
It works for me but, as you write, does not have an adjustable dwell time.
 
Old 05-01-2011, 09:06 AM   #7
thund3rstruck
Member
 
Registered: Nov 2005
Location: East Coast, USA
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 386

Rep: Reputation: 43
Quote:
Originally Posted by catkin View Post
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!
 
Old 05-01-2011, 11:23 AM   #8
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Original Poster
Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.
 
Old 05-01-2011, 12:00 PM   #9
thund3rstruck
Member
 
Registered: Nov 2005
Location: East Coast, USA
Distribution: Fedora 18, Slackware64 13.37, Windows 7/8
Posts: 386

Rep: Reputation: 43
Absolutely... alternate sides to the same coin! I just thought I'd contribute in a little show and tell (I'll show you mine if you show me yours!)
 
Old 04-30-2023, 02:48 PM   #10
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Original Poster
Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Lightbulb 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.
 
  


Reply

Tags
xfce4.6



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rotate Image (Script) without changing "modified" date tg0000 Linux - General 2 11-20-2010 02:32 AM
[SOLVED] Rotate image for desktop applications connie84911 Linux - Desktop 4 09-02-2010 11:39 AM
imagemagick and rotate image question nephish Linux - Software 2 08-26-2006 10:54 PM
how to rotate an image (clockwise for example)? xpucto Linux - Newbie 21 03-08-2006 12:22 PM
GIMP - Rotate image arbitrarily Micro420 Linux - Software 2 11-03-2003 02:52 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 01:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration