Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell script changing desktop wallpaper

Could you write the easiest possible shell script that will change the desktop wallpaper (in Ubuntu) in regular intervals (e.g. 1 minute).

Wallpapers will be saved in particular directory (e.g. $HOME/wallpapers). I need only basic functionality.

1) select random wallpaper from $HOME/wallpapers
2) set it as wallpaper on desktop
3) set cron to run the script every minute (not part of the question).

like image 406
xralf Avatar asked Apr 05 '11 11:04

xralf


People also ask

How do I set my wallpaper to automatically change in Linux?

Start shotwell image viewer application using the Activities menu. While all the the images are selected click on File and select Set as Desktop Slideshow . Set the time between each automatic wallpaper change. Optionally, also set wallpaper slideshow to be shown on your lock screen.

How do I stop my Ubuntu from changing my wallpaper?

Save this answer. Try right-clicking on the desktop and choosing change desktop background and choose one that you like that does not appear in a stack. If it looks like a stack of pictures, then it's a slideshow and it'll keep changing.


4 Answers

#!/bin/bash
wallpaperdir='$HOME/wallpaper'

files=($wallpaperdir/*)
randompic=`printf "%s\n" "${files[RANDOM % ${#files[@]}]}"`

gconftool-2 -t str --set /desktop/gnome/background/picture_filename "$randompic"

Save this script and edit your with the command "crontab -e" (it launches an editor where you put this line at the end of the file):

*/1     *     *     *     *         /bin/bash /path/to/script.sh

edit: I assumed you're using gnome. If not you need to edit the last line, because my example uses the Gnome Conftool. ;)

To change the background in XFCE, you should change the line with gconftool-2 to:

echo -e “# xfce backdrop list\n$randompic”>$HOME/.config/xfce4/desktop/backdrops.list    
killall -USR1 xfdesktop
like image 161
tamasgal Avatar answered Nov 25 '22 13:11

tamasgal


For gnome3 you need to use gsettings instead of gconftool.

But if you're going to execute the script throught cron it will not work.

I've tried a lot of .sh scripts but no one works for me.

At the end, i fixed it using this python script that loads a random wallpaper from a folder:

#!/usr/bin/env python
#coding: utf8 

import os,random
setup = "/path_to_folder/" + random.choice(os.listdir("/path_to_folder/"))
os.system("DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri  'file://%s'" %(setup))

Hope it helps for someone with my same problem!

like image 44
tomeu_quely Avatar answered Nov 25 '22 13:11

tomeu_quely


I know this answer is kind of late but since it could help some people, I'm posting it.

From septi's code plus some modifications, here is my solution :

#!/bin/bash
wallpaperdir="$HOME/wallpaper"

files=($wallpaperdir/*)
randompic=`printf "%s\n" "${files[RANDOM % ${#files[@]}]}"`

echo -e "# xfce backdrop list\n$randompic">$HOME/.config/xfce4/desktop/backdrop.list
xfdesktop --reload

The single quotes must be replaced by double quotes in order for the computer to interpret the $HOME part correctly. Also, the file you want to edit is backdrop.list, not backdrops.list. And finally, I find that using killall is kind of excessive in this case, since you can simply reload xfdesktop.

I've tested it on my computer (Linux Mint Debian Edition) and it seems to work perfectly.

Hope it helps. =)

EDIT : I forgot to mention that you have to add DISPLAY=:0.0 before your command, in crontab. That gives

*/1 * * * * DISPLAY=:0.0 wallpaper.sh
like image 33
Migwel Avatar answered Nov 25 '22 12:11

Migwel


This is just my approach on this matter. I don't claim that it's the ideal one.

WALLS_PATH=/path/to/images
cd $WALLS_PATH

while [ 1 ]; do
    for NEW_WALL in "$WALLS_PATH"/*; do
        gsettings set org.gnome.desktop.background picture-uri "file://${NEW_WALL}"
        sleep 1800
    done
done
like image 41
mackatozis Avatar answered Nov 25 '22 13:11

mackatozis