Gnome wallpaper changer

Every day solutions to every day challenges. + Brilliant stuff

Moderators: b1o, jkerr82508

Forum rules
Please feel free to post your tip it does not have to be advanced. Also ask questions directly related to the tip here. But do not start new threads with questions or ask for help here. That is what the help section is for. forum rules: http://bjoernvold.com/forum/viewtopic.php?f=8&t=568
User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Gnome wallpaper changer

Postby viking60 » 20 Jul 2010, 19:30

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
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

rvause
Viking
Posts: 42
Joined: 16 Jun 2010, 20:17
Contact:

Re: Gnome wallpaper changer

Postby rvause » 20 Jul 2010, 21:03

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':
    ...

User avatar
dedanna1029
Sound-Berserk
Posts: 8784
Joined: 14 Mar 2010, 20:29
Contact:

Re: Gnome wallpaper changer

Postby dedanna1029 » 20 Jul 2010, 21:52

Nice! :B
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Gnome wallpaper changer

Postby viking60 » 21 Jul 2010, 00:11

+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.
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
b1o
IT and IS Guru-Berserk
Posts: 198
Joined: 15 Mar 2010, 03:16

Re: Gnome wallpaper changer

Postby b1o » 21 Jul 2010, 01:23

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
CPU: i7 950 3.1 ghz |RAM: 12 GB DDR3 |Graphics: Nvidia Geforce gtx 280 |motherboard: Rampage II Extreme |OS: Arch + windows7

Just remember, there is no such thing as a stupid question, until you ask it

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Gnome wallpaper changer

Postby viking60 » 21 Jul 2010, 01:34

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: :
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Gnome wallpaper changer

Postby viking60 » 28 Jul 2010, 12:32

I have tested this for a week now and I am happy as a peach.Image Thanks again guys :B
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
b1o
IT and IS Guru-Berserk
Posts: 198
Joined: 15 Mar 2010, 03:16

Re: Gnome wallpaper changer

Postby b1o » 13 Oct 2010, 11:26

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.
CPU: i7 950 3.1 ghz |RAM: 12 GB DDR3 |Graphics: Nvidia Geforce gtx 280 |motherboard: Rampage II Extreme |OS: Arch + windows7

Just remember, there is no such thing as a stupid question, until you ask it

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Gnome wallpaper changer

Postby viking60 » 13 Oct 2010, 18:35

Nice! I did not know about the Google project thing :think:
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

User avatar
dedanna1029
Sound-Berserk
Posts: 8784
Joined: 14 Mar 2010, 20:29
Contact:

Re: Gnome wallpaper changer

Postby dedanna1029 » 13 Oct 2010, 19:20

Very nice! I may even implement that, although I do prefer the wallpaper I'm using right now.
I'd rather be a free person who fears terrorists, than be a "safe" person who fears the government.
No gods, no masters.
"A druid is by nature anarchistic, that is, submits to no one."
http://uk.druidcollege.org/faqs.html

User avatar
viking60
Über-Berserk
Posts: 9351
Joined: 14 Mar 2010, 16:34

Re: Gnome wallpaper changer

Postby viking60 » 24 Oct 2010, 10:29

Hey b1o where are the downloads?
Manjaro 64bit on the main box -Intel(R) Core(TM) i7 CPU 920 @ 2.67GHz and nVidia Corporation GT200b [GeForce GTX 275] (rev a1. + Centos on the server - Arch on the laptop.
"There are no stupid questions - Only stupid answers!"

rvause
Viking
Posts: 42
Joined: 16 Jun 2010, 20:17
Contact:

Re: Gnome wallpaper changer

Postby rvause » 25 Oct 2010, 21:31

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


Return to “Tips & Tricks”