Automatically changing wallpaper relatively to daytime in Ubuntu

15 02 2008

Fedora 8 introduced a nice feature which is automatically changing wallpaper relatively to daytime. Here’s how to have this cool feature in Ubuntu (and any other linux distribution) !
Select four wallpapers, one to be displayed at sunrise, the second during the day, third at sunset and the last at night.
Then, in a terminal, create a bash script as follows :

gedit .change.sh

You’ll get the newly created .change.sh file opened in a text editor.
Copy the following text in the file (Change the PATH-TO-**-PICTURE with the appropriate path) :

#!/bin/bash
HOUR=$(date +%H)
case "$HOUR" in
04|05|06|07)
gconftool -t string -s /desktop/gnome/background/picture_filename PATH-TO-SUNRISE-PICTURE
;;
08|09|10|11|12|13|14|15)
gconftool -t string -s /desktop/gnome/background/picture_filename PATH-TO-DAY-PICTURE
;;
16|17|18 )
gconftool -t string -s /desktop/gnome/background/picture_filename PATH-TO-SUNSET-PICTURE
;;
*)
gconftool -t string -s /desktop/gnome/background/picture_filename PATH-TO-NIGHT-PICTURE
;;
esac

As you can tell, this script sets the appropriate picture depending on the system’s current hour.
Now we’ll create a cron job, which is a linux way to automatically run tasks.

gedit .change.cron

This will open the text editor to edit our cron job.
Copy the following in the text editor (Replace YOUR-HOME-FOLDER by your home folder’s absolute path):

* 4,8,16,19 * * * YOUR-HOME-FOLDER/.change.sh

As you see, the cron task will lauch our previously created script at 4am, 8am, 4pm and 7pm to set the adequate wallpaper.
(Of course, you can change the hours at your convenience).
Now, let’s add these tasks to our gnome session so that they are automatically launched every time we login.
Open the System>Preferences>Sessions menu.
* Click the Add Button
Name: Changing Wallpaper Cron
Command: crontab PATH-TO-YOUR-HOME-FOLDER/.change.cron
Click Ok
Now the job of automatically changing wallpaper is set as an automated job every time we login.
We also have to add the script at session startup so that the correct wallpaper is initialized when we login. (This means, that if you login at e.g. 5:30pm, you’ll have the right wallpaper for that time). To do that,
* Click again the Add Button
Name: Initializing Wallpaper
Command: PATH-TO-YOUR-HOME-FOLDER/.change.sh
Click Ok
Here you are ! Your desktop wallpaper is living and reflecting the daytime! :P
Send your commentaries if you have any difficulty.


Actions

Information

25 responses

28 03 2008
Keith

Thanks very much. This didn’t work for me. I’m totally new to Linux and am wondering if PATH-TO-YOUR-HOME-FOLDER means entering the directory for /home/myname or the directory where the pictures are? I used /home/myname. Also, the pics are .jpg so I am wondering if that’s acceptable. Thanks very much for any advice.

29 03 2008
joeamined

Hi Keith,
In the example, .change.sh and .change.cron are located in my home folder (example /home/joeamined/.change.sh and /home/joeamined/.change.cron), but you can create them anywhere you like and just point to them. Maybe it didn’t work because you didn’t create them in /home/yourname.
For instance, you can create /home/yourname/.change.sh and the content of /home/yourname/.change.cron will be * 4,8,16,19 * * * /home/yourname/.change.sh.
Then add /home/yourname/.change.cron to the session startup programs.
Just make sure the paths are correct and the files actually exist there.
Also, jpg files can be used as wallpapers with no problem.
Hope this will solve the issue and don’t hesitate to ask again. It will be my pleasure to answer :)

29 03 2008
Keith

Thanks very much for your help. Without knowing, I actually did create .sh and .cron in my /home/keith folder. So, I determined that wasn’t my problem. I think I may have misunderstood the directions for the .change.sh file. When you write the following:

gconftool -t string -s /desktop/gnome/background/picture_filename PATH-TO-SUNRISE-PICTURE

I cut and pasted, then changed it as such

gconftool -t string -s /desktop/gnome/background/picture_filename /home/keith/Pictures/Wallpaper/sunrise.jpg

as this is where my pic is. Now I’m wondering if I should’ve done something differently here. Many thanks for your help. I really liked this effect in Fedora, but have found Ubuntu to be a bit more stable, so I was excited to see your directions on how to do this. Thanks again.

29 03 2008
joeamined

Hi Keith, I think what you did :
gconftool -t string -s /desktop/gnome/background/picture_filename /home/keith/Pictures/Wallpaper/sunrise.jpg is correct.
Maybe the two session startup programs are pointing to wrong filenames (crontab PATH-TO-YOUR-HOME-FOLDER/.change.cron and PATH-TO-YOUR-HOME-FOLDER/.change.sh) ?
You can test the command gconftool -t string -s /desktop/gnome/background/picture_filename /home/keith/Pictures/Wallpaper/sunrise.jpg in a terminal to see if it works.
About Fedora and Ubuntu, I think exactly the same as you do (I prefer apt package management too).
Hope you’ll get it work :)

2 04 2008
Gatesgamer33

I an having some trouble with this, the cron job is working perfectly, but the problem lies in ~/.change.sh

When I try to run it, I get this error.

/bin/bash: HOUR=$(date +%H)case “$HOUR” in: No such file or directory

Could you help me fix this?

2 04 2008
Gatesgamer33

BTW, the contains of .change.sh is as followed:

#!/bin/bashHOUR=$(date +%H)case “$HOUR” in
04|05|06|07)
gconftool -t string -s /desktop/gnome/background/picture_filename /home/jenkins/Pictures/.Wallpapers/Sunrise.jpg
;;
08|09|10|11|12|13|14|15)
gconftool -t string -s /desktop/gnome/background/picture_filename /home/jenkins/Pictures/.Wallpapers/Day.jpg
;;
16|17|18)
gconftool -t string -s /desktop/gnome/background/picture_filename /home/jenkins/Pictures/.Wallpapers/Sunset.jpg
;;
*)
gconftool -t string -s /desktop/gnome/background/picture_filename /home/jenkins/Pictures/.Wallpapers/Night.jpg
;;
esac

3 04 2008
joeamined

Hi Gatesgamer33
Maybe it is because you wrote:
#!/bin/bashHOUR=$(date +%H)case “$HOUR” in
instead of:
#!/bin/bash
HOUR=$(date +%H)
case “$HOUR” in
….
I know it is my fault because I wrote it the first way in the blog post, so I’m correcting it now. Thanks.

10 04 2008
Kai Dietrich

Hey, thank you for this idea, I ported your code to KDE and put together a tgz with the fedora wallpapers and the script. If anyone is interested:

http://www.cleeus.de/cms/content/blogsection/1/57/

2 05 2008
Davor

Hey Joe!
Very nice of you to solve this in such a simple manner! Thanks.
I have a question though: what needs to be modified in order for the script to select random files from let say a folder called “night” (see where I’m getting – how to make it select a picture from a collection of pictures)? And another one: would it be possible to take into consideration the weather conditions, given by the gnome weather applet, so to create a resemblance of real life on the screen?
Thanks again and keep up the good work!
Davor

3 05 2008
joeamined

Thank you Davor for your kind comment !
Your two ideas are really nice, and I’m sure there’s a way to implement them.
For the pictures, I’m thinking of a workaround, for example naming the files on the directory as picture1, picture2… and using a variable i to pick a random picturei from the folder… but I’m sure there’s a cleaner and better way to do it.
Concerning the weather applet, I think you’ll get help on how to use its data if you post a question to the guys who made it or to some gnome related forums..
Good luck. Hope you’ll find a way to implement your nice ideas !

5 05 2008
Davor

Hello again!
Nice idea for the workaround…will try it as soon as possible and report back!
In the meanwhile I have to report a minor problem with your scripts (two actually), which oddly didn’t manifest untill the first reboot…left me wondering…anyway, there they go:
In the .change.sh users should omit the quotation marks (“) befor and after the $HOUR in line 3 (so case $HOUR in , not case “$HOUR” in) – this worked for me (using gutsy); next in the .change.cron file the sh command for running scripts is missing… at least in my experience sh scripts won’t run unless you tell bash it’s a sh script. So instead of * 4,8,16,19 * * * YOUR-HOME-FOLDER/.change.sh it should be * 4,8,16,19 * * * sh YOUR-HOME-FOLDER/.change.sh.
That worked like a charm for me.
Thanks again for the idea and again keep up the good work! :)
P.S: the and are just in case HTML coding works with this posts, if they don’t please remove them in order to prevent misunderstandings.I know you know that but a reminder can’t hurt right? :)

5 05 2008
joeamined

Thanks a lot Davor for your contribution and your very nice and encouraging comments

13 05 2008
Hawk

Easier code:

#!/bin/bash
HOUR=$(date +%H)
PICPATH=’/home/hawk/Pictures/wallpaper’

gconftool -t string -s /desktop/gnome/background/picture_filename $PICPATH/$HOUR.jpg

Then have all the pictures in your wallpaper directory named 00.jpg to 23.jpg

6 06 2008
Davor

Thanks Hawk!

perfectly sound code. I just wish there was a way to randomize the selection of pictures… :) Working on another project at the moment…will catch up when there’ll be more time.

20 08 2008
Sascha

Hey

You should be able to use something like this to select a random picture from directory $DIR:

files=($DIR/*.jpg) # create an array of the files.
N=${#files[@]} # Number of members in the array
((N=RANDOM%N))
randomfile=${files[$N]}

gconftool -s “/desktop/gnome/background/picture_filename” –type=string $randomfile

26 08 2008
Lee

Hey, so I’ve tried this and it seemingly works but I only know this because when I shutdown I see the picture that is supposed to be up…almost subliminal in a way. I think the code is mocking me right about now…

Seriously though, before I saw the code I changed the wallpaper. Any suggestions? Thanks…

-Lee

30 08 2008
Ray

Hey, I was trying to get this going on Ubuntu and I’m not sure where the problem is…

I have:

#!/bin/bash
$HOUR=$(date +%H)
case “$HOUR” in
04|05)
gconftool -t string -s /desktop/gnome/background/picture_filename /home/ray/Pictures/allday/01.png
;;
06|07)
gconftool -t string -s /desktop/gnome/background/picture_filename /home/ray/Pictures/allday/02.png
;;
08|09)
gconftool -t string -s /desktop/gnome/background/picture_filename /home/ray/Pictures/allday/03.png
;;
10|11)
gconftool -t string -s /desktop/gnome/background/picture_filename /home/ray/Pictures/allday/04.png
;;
12|13)
gconftool -t string -s /desktop/gnome/background/picture_filename /home/ray/Pictures/allday/05.png
;;
14|15)
gconftool -t string -s /desktop/gnome/background/picture_filename /home/ray/Pictures/allday/06.png
;;
16|17|18)
gconftool -t string -s /desktop/gnome/background/picture_filename /home/ray/Pictures/allday/07.png
;;
*)
gconftool -t string -s /desktop/gnome/background/picture_filename /home/ray/Pictures/allday/08.png
;;
esac

and then:

* 4,6,8,10,12,14,16,19 * * * sh /home/ray/.change.sh

Am I missing something simple here? When I run the gconftool it changes the background so I know the path to the picture is right, but it never seems to do anything when I start up. Any suggestions would be a great help. Thanks!

30 08 2008
Ray

To moderator: Ok, I hope you can delete my previous post because I figured it out!

I just worked on this and finally got it working! What you need to make sure you do is not only change “/home/NAME/.change.sh” to sh /home/ray/.change.sh” but also make that change in this part:

From above:
* Click again the Add Button
Name: Initializing Wallpaper
Command: sh PATH-TO-YOUR-HOME-FOLDER/.change.sh
Click Ok

Make sure to add the sh to the “command” line too.

That worked for me then!

10 09 2008
gliphwriter

ive triedall the ideas on the page so far and the only thing that happens is at login i get the brown default wallpaper?

12 09 2008
gliphwriter

gconftool -t string -s /desktop/gnome/background/picture_filename /home/gliphwriter/pictures/360/x360wall.jpg

In the terminal still gives me default wallpaper

26 12 2008
Damian

Thanks! I wrote a little Python script to extend this which cycles between any images in a given directory (without needing to be hard coded). It’s available . Cheers!

26 12 2008
Damian

Sorry, evidently the link tag doesn’t work as described. The url is:
http://www.atagar.com/transfer/tmp/cycleWallpaper.py

7 02 2009
Miguel

Hi, I’m trying to get this working. I believe I followed all the steps you mentioned at the begining, and also tryed the erasing the “” at the beginning and the adding sh infront of where you mentioned.

Here is my code for .change.sh

#!/bin/bash
HOUR=$(date +%H)
case “$HOUR” in
04|05|06|07)
gconftool -t string -s /desktop/gnome/background/picture_filename
/home/miguel/Themes/nature_0083.jpg
;;
08|09|10|11|12)
gconftool -t string -s /desktop/gnome/background/picture_filename
/home/miguel/Themes/nature_0039.jpg
;;
13|14|15)
gconftool -t string -s /desktop/gnome/background/picture_filename
/home/miguel/Themes/nature_0055.jpg
;;
16|17|18 )
gconftool -t string -s /desktop/gnome/background/picture_filename
/home/miguel/Themes/nature_0043.jpg
;;
*)
gconftool -t string -s /desktop/gnome/background/picture_filename
/home/miguel/Themes/nature_0044.jpg
;;
esac

here is .change.cron

* 4,8,16,19 * * * sh /home/miguel/.change.sh

here is my Changing wallpaper cron path filename in the System/preferences/sessions..

/home/miguel/.change.cron

and the Initialize wallpaper

sh /home/miguel/.change.sh


What should I change?
I installed Ubuntu a week ago and I love it.

thanks!!

8 02 2009
Miguel

I fixed it, somehow the scrips could not acces my me folder, I had to move my pics to tmp folder…

2 10 2009
Eduardo

Excelent!!!!
Thank you so much for the script and the cron!
I use the method described by Hawk, just because it was easier.
Cheers!

Leave a comment