PDA

View Full Version : simple desktop-changing script



flyingsliverfin
September 21st, 2009, 02:38 AM
i know some very basic shell scripting, but how do i create a script that will change my desktop background every time i log in? i have about 5 different jpegs that i realy like so i want to have a diferent one each log in

dont just tell me right off, i want to figure something out myself. the only 2 things i want are: first, what is the command that will change the background. if there is none, what is the config file that has the background image location? the second thing is, how do i create simple for or while loop? i know some python and basic and java, but i dont know how to do it in scripting.

thx

kaibob
September 21st, 2009, 06:11 AM
...dont just tell me right off, i want to figure something out myself. the only 2 things i want are: first, what is the command that will change the background. if there is none, what is the config file that has the background image location? the second thing is, how do i create simple for or while loop? i know some python and basic and java, but i dont know how to do it in scripting.
thx
With Ubuntu, the desktop background is saved in the GConf database and can be viewed or changed with the GConf editor at the following key:

/desktop/gnome/background/picture_filename

You can change the value of this key with the command line as follows:


gconftool-2 --type str --set /desktop/gnome/background/picture_filename /path/to/file.jpg

If you simply want to rotate five backgrounds at boot, I'm not sure you would need a for or while loop, but you can find a good discussion of these at:

http://mywiki.wooledge.org/BashGuide#BashGuide.2BAC8-TheBasics.2BAC8-TestsAndConditionals.Conditional_Loops_.28while.2C _until_and_for.29

I have read reports of the GConf database being damaged when changed at the command line, so be sure to have a reliable backup.

falconindy
September 21st, 2009, 12:33 PM
how do i create simple for or while loop? i know some python and basic and java, but i dont know how to do it in scripting.
This should look familiar then...

while [[ condition ]]; do
# stuff happens here
done

flyingsliverfin
September 23rd, 2009, 10:20 PM
maybe i should have asked for the basics first: how do i make a file execute at log-in?

kaibob
September 24th, 2009, 01:03 AM
The easiest way is to use the Startup Programs tool:

System > Preferences > Startup Applications > Add

You can also do this by way of the crontab file with the @reboot option:

https://help.ubuntu.com/community/CronHowto

http://www.debianhelp.co.uk/crontab.htm

flyingsliverfin
September 26th, 2009, 08:01 PM
heh, im finding this bit hard with a language that i hardly know. is there any way to exceccute the command for changing the desktop background in python?

phrostbyte
September 27th, 2009, 03:26 AM
os.system("<command goes here>")

flyingsliverfin
September 29th, 2009, 02:12 AM
lol i just found an easier way to do it i think. it might take a bit to test the idea though. ill let u know when i get it

flyingsliverfin
September 29th, 2009, 03:53 AM
:lolflag: that was really easy. i was mkaing it way to hard for myself: heres my script (though its written in python)




#! /usr/bin/python

import os
import random
shuffle = [0,1,2,3]

random.shuffle(shuffle)

if shuffle[0] == 0:
os.system("gconftool-2 --type str --set /desktop/gnome/background/picture_filename /home/joshua/Desktop/ALL/downloaded_wallpaper/a.jpg")
if shuffle[0] == 1:
os.system("gconftool-2 --type str --set /desktop/gnome/background/picture_filename /home/joshua/Desktop/ALL/downloaded_wallpaper/k.jpg")
if shuffle[0] == 2:
os.system("gconftool-2 --type str --set /desktop/gnome/background/picture_filename /home/joshua/Desktop/ALL/downloaded_wallpaper/j.jpg")
if shuffle[0] == 3:
os.system("gconftool-2 --type str --set /desktop/gnome/background/picture_filename /home/joshua/Desktop/ALL/downloaded_wallpaper/d.jpg")



the only problem now is that it always asks me the 'run in terminal' 'display' etc pop-up every time... any way not to let it ask that on that specific script?

flyingsliverfin
September 29th, 2009, 03:57 AM
actually that only works if i click on it manually.... i cant figure out whats wrong when i put it under startup app's

renkinjutsu
September 29th, 2009, 04:13 AM
did you put the script into one of your PATH directories? if not, you'll need to specify the full path to the executable in your startup settings..

/path/to/python/script

flyingsliverfin
September 29th, 2009, 04:24 AM
i think i did...i put under 'command' /home/joshua/Desktop/ALL/scripts/desktop-changer.py

thatas the whole path

diesch
September 29th, 2009, 04:43 AM
The shell script version is a bit shorter:



#!/bin/sh
gconftool-2 --type str --set /desktop/gnome/background/picture_filename "$(ls /home/joshua/Desktop/ALL/downloaded_wallpaper/*.jpg|sort -R| head -n1)"
:-)

flyingsliverfin
September 29th, 2009, 11:30 PM
!!

is there a way to make it stop asking me to run in terminal or whatever?

diesch
September 30th, 2009, 12:34 AM
Nautilus always asks before executing a script. But you can create a starter for it on the desktop.

engla
September 30th, 2009, 12:55 AM
:lolflag: that was really easy. i was mkaing it way to hard for myself: heres my script (though its written in python)




#! /usr/bin/python

import os
import random
shuffle = [0,1,2,3]

random.shuffle(shuffle)

if shuffle[0] == 0:
os.system("gconftool-2 --type str --set /desktop/gnome/background/picture_filename /home/joshua/Desktop/ALL/downloaded_wallpaper/a.jpg")
if shuffle[0] == 1:
os.system("gconftool-2 --type str --set /desktop/gnome/background/picture_filename /home/joshua/Desktop/ALL/downloaded_wallpaper/k.jpg")
if shuffle[0] == 2:
os.system("gconftool-2 --type str --set /desktop/gnome/background/picture_filename /home/joshua/Desktop/ALL/downloaded_wallpaper/j.jpg")
if shuffle[0] == 3:
os.system("gconftool-2 --type str --set /desktop/gnome/background/picture_filename /home/joshua/Desktop/ALL/downloaded_wallpaper/d.jpg")



the only problem now is that it always asks me the 'run in terminal' 'display' etc pop-up every time... any way not to let it ask that on that specific script?

Just as an excercise, let's type this up a bit more Pythonic:



#!/usr/bin/python

import os
import random

backgrounds = os.listdir("/home/joshua/Desktop/ALL/downloaded_wallpaper/")
new_bkg = random.choice(backgrounds)

os.system("gconftool-2 --type str --set /desktop/gnome/background/picture_filename %s" % new_bkg)

flyingsliverfin
September 30th, 2009, 01:51 AM
listdir does what? (i sppose i can figure it out just by looking at the word but i dont want to mix myself up for next time)

Apart from that im a beginner at python :( i wish i werent.

i like that word 'pythonic'

sorry if this all sounds a bit childish (im only 13)

fiddler616
September 30th, 2009, 02:21 AM
!!

is there a way to make it stop asking me to run in terminal or whatever?

The problem of making Python programs executable crops about every three months here. I think for your purposes you need to Google something along the lines of "chmod +x python", IIRC.

diesch
September 30th, 2009, 03:08 AM
Just as an excercise, let's type this up a bit more Pythonic:



#!/usr/bin/python

import os
import random

backgrounds = os.listdir("/home/joshua/Desktop/ALL/downloaded_wallpaper/")
new_bkg = random.choice(backgrounds)

os.system("gconftool-2 --type str --set /desktop/gnome/background/picture_filename %s" % new_bkg)



new_bkg is just the file name, but you need the full path.

Without calling gconftool-2:



#!/usr/bin/env python

import os, os.path, random, gconf

bkg_dir = "/home/joshua/Desktop/ALL/downloaded_wallpaper/"
new_bkg = os.path.join(bkg_dir, random.choice(os.listdir(bkg_dir)))

gclient = gconf.client_get_default()
gval=gconf.Value(gconf.VALUE_STRING)
gval.set_string(new_bkg)
gclient.set('/desktop/gnome/background/picture_filename', gval)
Time for advertising... ;-)

http://www.florian-diesch.de/software/easygconf/ makes accessing GConf easier:



#!/usr/bin/env python
import os, os.path, random, easygconf

bkg_dir = "/home/joshua/Desktop/ALL/downloaded_wallpaper/"
new_bkg = os.path.join(bkg_dir, random.choice(os.listdir(bkg_dir)))

gcd = easygconf.GConfDict('/desktop/gnome/background/')
gcd['picture_filename'] = new_bkg

flyingsliverfin
September 30th, 2009, 03:51 AM
oh, u made that? cool :) did u write it? If u did, in what language?

sprechen sie den auch Deutsch?

engla
September 30th, 2009, 09:36 AM
You got me there! Yeah I forgot the full path. Machen wir den Thread weiter komplett auf Deutsch? :-)

flyingsliverfin
September 30th, 2009, 10:13 PM
Darf man das?

for you guys that cant read german: is it allowed to do a start a whole thread in english and then have it change compeltely to a different language?


maybe its a stupid question, but ive been to forums where your only allowed to speak english

fiddler616
September 30th, 2009, 10:26 PM
German speakers: I recommend providing translations of what you said before somebody pulls out Google Translate and mangles it. This has happened before (between me and nvteighen, for one).

diesch
October 1st, 2009, 06:17 AM
oh, u made that? cool :) did u write it? If u did, in what language?


easygconf? Yes, I wrore it. Isn't anything fancy, just a few lines of Python to implement a dict-like interface for the GConf Python bindings.





sprechen sie den auch Deutsch?

Ja. Das habe ich schon als kleines Kind gelernt ;-)

diesch
October 1st, 2009, 06:45 AM
Darf man das?

Legal? Illegal? Scheißegal! ;-)



for you guys that cant read german: is it allowed to do a start a whole thread in english and then have it change compeltely to a different language?


The Forum Rules say


Please try to write your posts in English. We have many users from many different countries that visit here.
But I guess we will not get banned immediately if we use some German ;-)

Using English for the important parts would be polite, though.

flyingsliverfin
October 2nd, 2009, 08:23 PM
lol.