Saturday, February 12, 2011

Openbox random desktop wallpaper script

There are a lot of ways to change the desktop background of Openbox, but for this tutorial we are going to focus our thoughts on feh.

To set the background in Openbox, simply open a terminal and type the following command:

Code:
# feh --bg-scale /path/to/file.jpg

If you want to only use a single wallpaper for your desktop, the put this line in your autostart file:

Code:
# nano -w ~/.config/openbox/autostart.sh
# feh --bg-scale /path/to/file.jpg &

Remember the & at the end, very important!

Random wallpaper script
Let's take this one step further and take a look at a script that will randomly change the desktop wallpaper each time we log into Openbox.

Here is the script first:

Code:
#!/bin/sh 
WALLPAPERS="/path/to/folder/containing/wallpapers"
ALIST=( `ls -w1 /path/to/folder/containing/wallpapers` )
RANGE=${#ALIST[@]}
let "number = $RANDOM"
let LASTNUM="`cat $WALLPAPERS/.last` + $number"
let "number = $LASTNUM % $RANGE"
echo $number > $WALLPAPERS/.last

feh --bg-scale $WALLPAPERS/${ALIST[$number]}

Name it something easy and save it, on my PC all my scripts (conky, wallpaper, etc.) is saved in the same folder (~/.scripts). Let's name it randomwall.sh.

Make it executable:

Code:
# chmod +x .scripts/randomwall.sh

Test the script by running it from a terminal:

Code:
# cd .scripts
# ./randomwall.sh

If all went well you should see the desktop background changing. On my machine the script complained about a file (.last) not being present in the wallpaper directory, so just create that file:

Code:
# cd /path/to/wallpaper/folder
# nano .last
ctrl+o to save the file
ctrl+x to exit

No need to write anything in it, just create it.
If everything is working as expected, simply add the script to your autostart file:

Code:
# nano -w .config/openbox/autostart.sh 

~/.scripts/randomwall.sh &

If you have problems with the script not being found, use the full path to the file:

Code:
/home/user/.scripts/randomwall.sh &

Log out and log back in, you should see a new wallpaper now!


Other alternatives
There are some scripts out there that allows you to change the desktop wallpaper with a menu click inside the Openbox right-click menu, if you want to try them out you can have a look here.

No comments: