Page 1 of 3

Gnome wallpaper changer

Posted: 20 Jul 2010, 19:30
by viking60
In KDE you can put all your Wallpapers in a folder and change them every hour minute or day or whatever. There is no similar function in gnome.
Here is a python script that rvause made for it with improvements from b1o:
It will change your wallpaper every 5th minute (300 sec.).

Code: Select all

import os, random, time
 
WALLPAPER_DIR = '/your/wallpaper/folder'
 
walls = os.listdir(WALLPAPER_DIR)
wallpaper = os.path.join(WALLPAPER_DIR, walls[random.randint(0, len(walls)-1)])
#i = 0
while (3):
 walls = os.listdir(WALLPAPER_DIR)
 wallpaper = os.path.join(WALLPAPER_DIR, walls[random.randint(0, len(walls)-1)])
 if os.path.exists(wallpaper):
   os.system('gconftool-2 -s -t string /desktop/gnome/background/picture_filename "%s"' % wallpaper)
   time.sleep(300)


Save it as wallpaper.py (or whatever as long as you have the extension py) and call it in a terminal with

Code: Select all

python wallpaper.py

Your background will now change every 5 minutes.
To change the interval you just alter the last line "time.sleep(300)" to whatever you like 300 sec = 5 minutes. Altering it to 600 would then change your wallpaper every 10 minutes.

Put in among your autostarts and it will work everytime you boot.
Nice work there rvause and b1o.
I have a challenge for you tho..
Since this function already is in KDE we do not want this script to work there - only in Gnome. The first one to make a condition that takes care of this, gets a pannekake from rolf :D

Re: Gnome wallpaper changer

Posted: 20 Jul 2010, 21:03
by rvause
stick

Code: Select all

#!/usr/bin/env python

on the first line then you don't need to run it with python

you can check for gnome like:

Code: Select all

if os.environ.get('DESKTOP_SESSION') == 'gnome':
    ...

Re: Gnome wallpaper changer

Posted: 20 Jul 2010, 21:52
by dedanna1029
Nice! :B

Re: Gnome wallpaper changer

Posted: 21 Jul 2010, 00:11
by viking60
+1 Pannekaker coming up
My box would not take the == 'gnome' though. But it is a Gnome function if you want to log into KDE you can just deactivate the function in KDE control panel (If you have activated it). No problem.

Re: Gnome wallpaper changer

Posted: 21 Jul 2010, 01:23
by b1o
Here's one in java that does the same:
http://hotfile.com/dl/56450584/8c2de72/ ... ar.gz.html

pack it out in for example a folder called desktopchanger in your home folder.

edit the conf(stored in conf) file so that it knows of the folder your pictures are stored in, the default interval is 30 but you could change that in the config too.
might be the best solution if you don't want to meddle with scripts ;)

Then you can run it by typing

Code: Select all

java -jar /path/to/program/wp.jar

or
right click the jar file, open with other program, click the button that says you can use your own command.
Write java -jar and press ok and close the windows.

right click wp.jar again and click run with java

Source:
Main Class:

Code: Select all

import java.io.File;
import java.util.Random;

/**
 * Program for linux to change your background image from a folder.
 * @author b1o
 */
public class Main {
    public static void main(String args[]) throws Exception {
        ConfReader cr = new ConfReader();
        while (true) {
            File[] files = new File(cr.getPath()).listFiles();
            Runtime.getRuntime().exec("gconftool-2 -s -t string /desktop/gnome/background/picture_filename "+files[new Random().nextInt(files.length)].getAbsolutePath());
            Thread.currentThread().sleep(cr.getInterval()*1000);
        }
    }
}


The main class is realy all you need if you do some slight changes, but i though a config file would be nice since it's not a scripting language which you can run interpreted.
Here's the code for reading a config file with 2 parameters:

Code: Select all

import java.io.File;
import java.util.Scanner;

/**
 * Reads the config file
 * @author b1o
 */
public class ConfReader {

    private File conf;
    private Scanner sc;
    private String output;
    private File jarFile;

    public ConfReader()  {
       
        try{
        jarFile = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI());
        }catch(Exception e){}
        String[] temp = jarFile.getAbsolutePath().split("/");
        String path = "";
        for(int i = 0; i < temp.length-1; i++)
        {
            path = path + temp[i] + "/";
        }
        conf = new File(path+"/conf/conf.cfg");
        output = "";
    }

    public String getPath() throws Exception{
        sc = new Scanner(conf);
        while (sc.hasNext()) {
            output = sc.next().toLowerCase();
            if (output.equals("wallpaper_folder")) {
                sc.next();
                output = sc.next();
                break;
            }
        }
        sc.close();
        return output;
    }

    public int getInterval() throws Exception{
        sc = new Scanner(conf);
        while (sc.hasNext()) {
            output = sc.next().toLowerCase();
            if (output.equals("interval")) {
                sc.next();
                output = sc.next();
                break;
            }
        }
        sc.close();
        return Integer.parseInt(output);
    }
}


And at last the config file:

Code: Select all

Wallpaper_Folder = /folder/containing/wallpapers
interval = 30

Re: Gnome wallpaper changer

Posted: 21 Jul 2010, 01:34
by viking60
Yes! I have tested it and it works perfectly. :s When I change the time in the conf. file it alters it on the fly - no restarting. Nice :!:
It does not work in KDE (That is a good thing - we don't want it to work there).
Here is a video and demonstration of how to change the wallpaper intervals on the fly My conky did not fix the speed :lol: :

Re: Gnome wallpaper changer

Posted: 28 Jul 2010, 12:32
by viking60
I have tested this for a week now and I am happy as a peach.Image Thanks again guys :B

Re: Gnome wallpaper changer

Posted: 13 Oct 2010, 11:26
by b1o
Desktop changer now added as a google code project

http://code.google.com/p/gautobg-changer/

This will make it easier for developers to contribute if there is something they think should be changed.

Re: Gnome wallpaper changer

Posted: 13 Oct 2010, 18:35
by viking60
Nice! I did not know about the Google project thing :think:

Re: Gnome wallpaper changer

Posted: 13 Oct 2010, 19:20
by dedanna1029
Very nice! I may even implement that, although I do prefer the wallpaper I'm using right now.

Re: Gnome wallpaper changer

Posted: 24 Oct 2010, 10:29
by viking60
Hey b1o where are the downloads?

Re: Gnome wallpaper changer

Posted: 25 Oct 2010, 21:31
by rvause
Here is a link to my solution. It will take an argument (path of a wallpaper) so if you add a launcher for it on your panel you can drag and drop wallpapers on the launcher to change. Only clicking the launcher would pick a random wallpaper. You can provide a path to wallpapers folder in wallpaper.py

If you want to use cron job to change automatically, you can make generate_Xdbus to run on startup. This will generate a file in $HOME/.Xdbus. Then your cron line should look something like this

Code: Select all

0,15,30,45 * * * * . /home/me/.Xdbus; /home/me/wallpaper.py >| wallpaper.log 2>&1


I will work on an indicator applet but for now this is very convenient for me.

http://dl.dropbox.com/u/2968911/wallpaper.tar.gz