Wallpaper Changer for Gnome3

I thought I would just post this for anyone that is interested in a lightweight wallpaper changer. I did not write it–the thanks go to the author in the script. This is for the Gnome3 DE–try it in others at your own risk…Enjoy! NOTE: you can try the first script, dalto recommended modifications to allow tolerance in the script–that is the second script below. I would recomend his modification–I am currently using it.

Script Start:


#!/bin/bash
#
# WallpaperChanger.sh
# Copyright 2014 Michele Bonazza michele@michelebonazza.com
#
# A simple script to automatically change your wallpaper in Gnome.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
WALLPAPERS_FOLDER=/home/yourhome/yourwallpaperfolder
REFRESH_INTERVAL=$((5 * 60)) # change every 5 minutes
MODE="stretched" # one between none, centered, wallpaper, scaled, stretched, zoom, spanned
 
# Changes the desktop background, and moves it to the "shown" folder so that it's
# not shown again before all wallpapers in the folder have been used.
# arg1 the file name of the file to be set as new background; must be in the
#      current folder
function change_wallpaper() {
  mv $1 shown
  gsettings set org.gnome.desktop.background picture-uri file://$WALLPAPERS_FOLDER/shown/$1
  gsettings set org.gnome.desktop.background picture-options $MODE
}
 
# Echoes the next wallpaper to be set, picked at random among images in the
# configured folder
function get_next_wallpaper() {
  find . -maxdepth 1 -type f -name "*.png" -o -name "*.jpg" -o -name "*.gif" -o -name "*.jpeg"| shuf -n 1
}
 
mkdir -p $WALLPAPERS_FOLDER/shown
cd $WALLPAPERS_FOLDER
 
while true; do
  NEXT_WP=$(get_next_wallpaper)
   
  # have we used all wallpapers?
  if [[ "$NEXT_WP" == "" ]]; then
    # yes, chdir to shown, and move them all back to the parent folder
    cd shown
    # move them to parent folder
    find . -maxdepth 1 -type f -name "*.png" -o -name "*.jpg" -o -name "*.gif" -o -name "*.jpeg" | xargs mv -t ..
    cd ..
 
    # check again
    NEXT_WP=$(get_next_wallpaper)
 
    if [[ "$NEXT_WP" == "" ]]; then
      echo "no wallpapers found in $WALLPAPERS_FOLDER, will check again in $REFRESH_INTERVAL seconds..."
      sleep $REFRESH_INTERVAL
      continue
    fi
  fi
   
  echo "changing background to $NEXT_WP"
  change_wallpaper $NEXT_WP
  sleep $REFRESH_INTERVAL
done

End Script:

As you can see, it uses your selected folder & creates a “shown” folder inside of it. Then it uses a sudo-random select of the remaining wallpapers in the folder.
These 3 functions define its operation:

WALLPAPERS_FOLDER=/home/yourhome/yourwallpaperfolder
REFRESH_INTERVAL=$((5 * 60)) # change every 5 minutes
MODE=“stretched” # one between none, centered, wallpaper, scaled, stretched, zoom, spanned

I’ve used this from when it was offered & am very happy with the operation. Just put the script in your /home, chmod it & create a startup for it & you are good to go.

So, as noted below—make sure there is NO whitespace in the filenames. I have used underscores if there needs to be seperation between words in the wallpaper name. Best practice is to condense the name with no “blanks” between words.

3 Likes

Thanks for sharing. :partying_face:

You might consider making sure there is sufficient use of quotes in your script. I didn’t test it but I think it will probably fall apart if it hits any whitespace as it is now.

Something like this whenever a variable is used:

gsettings set org.gnome.desktop.background picture-uri "file://${WALLPAPERS_FOLDER}/shown/${1}"
1 Like

Hmmm… I pulled the script directly from my /home…that was the way it was written when I got it. I’m looking at it & yes, there is quite a bit of whitespace. Will condense it & test for operation.

Err…I mean, if there is whitespace in the filenames of the wallpapers.

AH…Yes, I have run into that before. I just edit my wallpapers to use underscores if there needs to be a space between. I will edit the main post to include that information.

1 Like

Why not just fix the script? It would take about 3 minutes…

1 Like

Well…I was just offering the script “as-is” —I write conkys— I would ask for information to modify this the correct way. I’m not quite sure what the correct way is.

You can do like in the example I posted above in the second post.

Or, if you aren’t familiar with bash scripting, I can do it for you. It will only take a couple of minutes.

If you are willing, that would be perfect…I work with LUA & write conky…do simple bash–nothing in depth. That is on the rather long list I still have to do. :slight_smile:

I am not running gnome, would you mind testing this?

#!/bin/bash
#
# WallpaperChanger.sh
# Copyright 2014 Michele Bonazza michele@michelebonazza.com
#
# A simple script to automatically change your wallpaper in Gnome.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
WALLPAPERS_FOLDER=/home/yourhome/yourwallpaperfolder
REFRESH_INTERVAL=$((5 * 60)) # change every 5 minutes
MODE="stretched" # one between none, centered, wallpaper, scaled, stretched, zoom, spanned
 
# Changes the desktop background, and moves it to the "shown" folder so that it's
# not shown again before all wallpapers in the folder have been used.
# arg1 the file name of the file to be set as new background; must be in the
#      current folder
function change_wallpaper() {
  mv "${1}" shown
  gsettings set org.gnome.desktop.background picture-uri "file://${WALLPAPERS_FOLDER}/shown/${1}"
  gsettings set org.gnome.desktop.background picture-options "${MODE}"
}
 
# Echoes the next wallpaper to be set, picked at random among images in the
# configured folder
function get_next_wallpaper() {
  find . -maxdepth 1 -type f -name "*.png" -o -name "*.jpg" -o -name "*.gif" -o -name "*.jpeg"| shuf -n 1
}
 
mkdir -p "${WALLPAPERS_FOLDER}/shown"
cd "${WALLPAPERS_FOLDER}"
 
while true; do
  NEXT_WP=$(get_next_wallpaper)
   
  # have we used all wallpapers?
  if [[ "$NEXT_WP" == "" ]]; then
    # yes, chdir to shown, and move them all back to the parent folder
    cd shown
    # move them to parent folder
    find . -maxdepth 1 -type f -name "*.png" -o -name "*.jpg" -o -name "*.gif" -o -name "*.jpeg" | xargs mv -t ..
    cd ..
 
    # check again
    NEXT_WP=$(get_next_wallpaper)
 
    if [[ "$NEXT_WP" == "" ]]; then
      echo "no wallpapers found in ${WALLPAPERS_FOLDER}, will check again in ${REFRESH_INTERVAL} seconds..."
      sleep ${REFRESH_INTERVAL}
      continue
    fi
  fi
   
  echo "changing background to ${NEXT_WP}"
  change_wallpaper "${NEXT_WP}"
  sleep ${REFRESH_INTERVAL}
done
3 Likes

So, the braces are adding “tolerance” to the result?

The braces and the double quotes.

The braces aren’t always needed but it should never hurt so I always use them.

OK—Interesting…I’ll make the mod & reboot.

I have substituted the modified script & the first changes worked normally. I don’t have an image with whitespace at present…will create one for test…as there is a sudo-random function, will need to check the logs to verify. Looks good so far, Thank You. I have posted this as a solution in the first post.

2 Likes

Nice script.
I am using Variety as wallpaper changer with Gnome. It has some more options like more than one source for the wallpaper, like wallpapers from external sources etc.

Yes—I tried it a while back. Really wanted something simple, so I went back to this script. I do have a large wallpaper collection (1,000 +), so this serves my needs. Must admit that Variety increased my collection a fair amount :slight_smile: , just included some stuff I didn’t want to see on my desktop…

1 Like