Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there any possible way to change wallpaper (background) every five minutes on centos 6.3 with gnome 2.28.2 ? I found this Python script:

#!/usr/bin/env python

import gobject
gobject.threads_init()
import gtk
import os
import xml.dom.minidom
import gconf

class Wallpaper:
    def __init__(self):
        # Create GTK Window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_title("Dynamic Wallpaper")
        self.window.resize(350, 110)
        self.window.set_resizable(False)

        # Create window objects
        vbox = gtk.VBox()

        label = gtk.Label("Folder:")
        label.set_alignment(0.0, 0.5)
        self.folder_entry = gtk.Entry()
        button = gtk.Button("Select...")
        button.connect("clicked", self.button_clicked)
        hbox = gtk.HBox()
        hbox.pack_start(label, True)
        hbox.pack_start(self.folder_entry, False)
        hbox.pack_start(button, False)
        vbox.pack_start(hbox, False)

        label = gtk.Label("Picture duration:")
        label.set_alignment(0.0, 0.5)
        adj = gtk.Adjustment(5.0, 1.0, 60.0, 1.0, 1.0, 0.0)
        self.pdur_spinner = gtk.SpinButton(adj)
        self.pdur_combo = gtk.combo_box_new_text()
        self.pdur_combo.append_text("hr")
        self.pdur_combo.append_text("min")
        self.pdur_combo.append_text("sec")
        self.pdur_combo.set_active(1)
        hbox = gtk.HBox()
        hbox.pack_start(label, True)
        hbox.pack_start(self.pdur_spinner, False)
        hbox.pack_start(self.pdur_combo, False)
        vbox.pack_start(hbox, False)

        label = gtk.Label("Transition duration:")
        label.set_alignment(0.0, 0.5)
        adj = gtk.Adjustment(5.0, 0.0, 60.0, 1.0, 1.0, 0.0)
        self.tdur_spinner = gtk.SpinButton(adj)
        self.tdur_combo = gtk.combo_box_new_text()
        self.tdur_combo.append_text("hr")
        self.tdur_combo.append_text("min")
        self.tdur_combo.append_text("sec")
        self.tdur_combo.set_active(2)
        hbox = gtk.HBox()
        hbox.pack_start(label, True)
        hbox.pack_start(self.tdur_spinner, False)
        hbox.pack_start(self.tdur_combo, False)
        vbox.pack_start(hbox, False)

        self.cancel_button = gtk.Button(stock=gtk.STOCK_CLOSE)
        self.cancel_button.connect("clicked", self.destroy)
        self.ok_button = gtk.Button(stock=gtk.STOCK_OK)
        self.ok_button.set_sensitive(False)
        self.ok_button.connect("clicked", self.ok_button_clicked)
        hbox = gtk.HBox()
        hbox.pack_end(self.ok_button, False)
        hbox.pack_end(self.cancel_button, False)
        vbox.pack_end(hbox, False, False, 10)

        # Connect signal handlers
        self.window.connect("delete-event", self.delete_event)
        self.window.connect("destroy", self.destroy)

        # Show entire window
        self.window.add(vbox)
        self.window.show_all()

    def destroy(self, widget):
        gtk.main_quit()

    def button_clicked(self, button):
        dialog = gtk.FileChooserDialog("Open..", None, gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
                                       (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                        gtk.STOCK_OK, gtk.RESPONSE_OK))
        dialog.set_default_response(gtk.RESPONSE_OK)
        dialog.set_filename(os.path.expanduser("~/Pictures/*"))
        response = dialog.run()
        filename = dialog.get_filename()
        dialog.destroy()

        if response == gtk.RESPONSE_OK:
            self.folder_entry.set_text(filename)
            self.files = []
            accepted = ('.jpg', '.jpeg', '.gif', '.png')
            for file in os.listdir(filename):
                if os.path.splitext(file)[1] in accepted:
                    self.files.append(os.path.join(filename, file))

            if len(self.files):
                self.ok_button.set_sensitive(True)
            else:
                dialog = gtk.MessageDialog(parent = None, flags = gtk.DIALOG_DESTROY_WITH_PARENT,
                    type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK,
                    message_format = "Folder contains no images")
                dialog.set_title('Error')
                dialog.connect('response', lambda dialog, response: dialog.destroy())
                dialog.run()

    def ok_button_clicked(self, button):
        nfiles = len(self.files)

        if nfiles <= 0:
            return False

        folder = self.folder_entry.get_text()

        picture_duration = int(self.pdur_spinner.get_text())
        if self.pdur_combo.get_active_text() == "min":
            picture_duration = picture_duration * 60
        elif self.pdur_combo.get_active_text() == "hr":
            picture_duration = picture_duration * 60 * 60

        transition_duration = int(self.tdur_spinner.get_text())
        if self.tdur_combo.get_active_text() == "min":
            transition_duration = transition_duration * 60
        elif self.tdur_combo.get_active_text() == "hr":
            transition_duration = transition_duration * 60 * 60

        document = """<background><starttime><year>2009</year><month>08</month><day>04</day><hour>00</hour><minute>00</minute><second>00</second></starttime></background>"""
        dom = xml.dom.minidom.parseString(document)

        for i in range(0, nfiles):
            static = dom.createElement("static")
            duration = dom.createElement("duration")
            duration.appendChild(dom.createTextNode(str(picture_duration)))
            static.appendChild(duration)
            file = dom.createElement("file")
            file.appendChild(dom.createTextNode(self.files[i]))
            static.appendChild(file)
            dom.documentElement.appendChild(static)

            if transition_duration <= 0: continue

            transition = dom.createElement("transition")
            duration = dom.createElement("duration")
            duration.appendChild(dom.createTextNode(str(transition_duration)))
            transition.appendChild(duration)
            frm = dom.createElement("from")
            frm.appendChild(dom.createTextNode(self.files[i]))
            transition.appendChild(frm)
            to = dom.createElement("to")
            if i < nfiles-1:
                to.appendChild(dom.createTextNode(self.files[i+1]))
            else:
                to.appendChild(dom.createTextNode(self.files[0]))
            transition.appendChild(to)
            dom.documentElement.appendChild(transition)

        try:
            save_filename = os.path.join(folder, "background-1.xml")
            f = open(save_filename, 'w')
            f.write(dom.toxml("utf-8"))
            f.close()
        except IOError as (errno, strerror):
            try:
                save_filename = os.path.join(os.path.expanduser("~"), ".dynamic-wallpaper.xml")
                f = open(save_filename, 'w')
                f.write(dom.toxml("utf-8"))
                f.close()
            except Error as (errno, strerror):
                dialog = gtk.MessageDialog(parent = None, flags = gtk.DIALOG_DESTROY_WITH_PARENT,
                    type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK,
                    message_format = strerror)
                dialog.set_title('Error')
                dialog.connect('response', lambda dialog, response: dialog.destroy())
                dialog.run()
                return False
        except Error as (errno, strerror):
                dialog = gtk.MessageDialog(parent = None, flags = gtk.DIALOG_DESTROY_WITH_PARENT,
                    type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK,
                    message_format = strerror)
                dialog.set_title('Error')
                dialog.connect('response', lambda dialog, response: dialog.destroy())
                dialog.run()
                return False

        backgrounds_file = os.path.join(os.path.expanduser("~/.gnome2/"), "backgrounds.xml")
        doc = xml.dom.minidom.parse(backgrounds_file)

        wallpaper = dom.createElement("wallpaper")
        wallpaper.setAttribute("deleted", "false")
        name = dom.createElement("name")
        name.appendChild(dom.createTextNode("Dynamic Wallpaper"))
        filename = dom.createElement("filename")
        filename.appendChild(dom.createTextNode(save_filename))
        options = dom.createElement("options")
        options.appendChild(dom.createTextNode("stretched"))
        shade_type = dom.createElement("shade_type")
        shade_type.appendChild(dom.createTextNode("solid"))
        pcolor = dom.createElement("pcolor")
        pcolor.appendChild(dom.createTextNode("#2c2c00001e1e"))
        scolor = dom.createElement("scolor")
        scolor.appendChild(dom.createTextNode("#2c2c00001e1e"))
        wallpaper.appendChild(name)
        wallpaper.appendChild(filename)
        wallpaper.appendChild(options)
        wallpaper.appendChild(shade_type)
        wallpaper.appendChild(pcolor)
        wallpaper.appendChild(scolor)
        doc.documentElement.appendChild(wallpaper)

        try:
            f = open(backgrounds_file, 'w')
            f.write(doc.toxml("utf-8"))
            f.close()
        except Error as (errno, strerror):
                dialog = gtk.MessageDialog(parent = None, flags = gtk.DIALOG_DESTROY_WITH_PARENT,
                    type = gtk.MESSAGE_ERROR, buttons = gtk.BUTTONS_OK,
                    message_format = strerror)
                dialog.set_title('Error')
                dialog.connect('response', lambda dialog, response: dialog.destroy())
                dialog.run()
                return False

        conf_client = gconf.client_get_default()
        conf_client.add_dir("/desktop/gnome/background", gconf.CLIENT_PRELOAD_NONE)
        conf_client.set_string("/desktop/gnome/background/picture_filename", save_filename)
        conf_client.set_string("/desktop/gnome/background/picture_options", "strecthed")
        conf_client.set_string("/desktop/gnome/background/color_shading_type", "solid")
        conf_client.set_string("/desktop/gnome/background/primary_color", "#2c2c00001e1e")
        conf_client.set_string("/desktop/gnome/background/secondary_color", "#2c2c00001e1e")

        gtk.main_quit()

    def delete_event(self, widget, event):
        return False

if __name__ == "__main__":
    wallpaper = Wallpaper()
    gtk.main()

The script is for Ubuntu and when I ran it on my system and it generate a xml file but it doesn't work.

Can anybody help me?

Best regards, George!

Postscript: And sorry for my awful English...

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I'd look to use an application to do this. Here are 2 ways that I've used on Fedora and CentOS in the past.

Wallpapoz

Is an applet that runs in your GNOME taskbar.

  • You can download the RPM from here for various Fedora/CentOS versions.
  • The main site for it is here.
  • The main github tree is here.

ss of wallpapoz

Shell script

Here's a bash script that will cycle through a list of images.

#!/bin/bash
#Default values
pictdir="$HOME/Pictures"
time=1h

#Wallpaper names
fnme=('wp1.jpg' 'wp2.jpg' 'wp3.jpg')

fin=${#fnme[@]}
let bck=$fin-1

#Exit if the script is already running, just in case
#Got this part of script somewhere in this forum
if pidof -x $(basename $0) > /dev/null; then
  for p in $(pidof -x $(basename $0)); do
    if [ $p -ne $$ ]; then
      echo "Script $0 is already running: exiting"
      exit
    fi
  done
fi

#This is where the desktop wallpaper changes
for (( i=0;i<$fin;i++ )); do
    sleep $time #wait for $time second(s)
    gconftool-2 --type string --set /desktop/gnome/background/picture_filename $pictdir/${fnme[${i}]}
    if [ $i -eq $bck ]; then
         let i=-1
    fi
done

It makes use of gconftool-2. I haven't tried this on CentOS 6.x yet so it might require some tweaking, but I have used it on Fedora and CentOS 5.x. The script is courtesy of this linuxquestions thread.

Wallch

Available here. Haven't tried it but it's purported to work on GNOME 2 & 3. There's a nice review of Wallch here.

ss wallch

share|improve this answer
    
Thank you, mate :) –  T0m3kk Jan 16 '13 at 15:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.