You are not logged in.

#1 2010-08-14 01:45:46

wisp558
Member
Registered: 2010-06-10
Posts: 17

A nice wallpaper randomizer scipt.

I know most of you here would make this on your own if you wanted it, but for those who can't, or never bothered, here's a handy and easily editable script.

#!/bin/bash

DIR=/home/wisp/Wallpapers
FLOOR=1
RANGE=`ls -1 "$DIR"/*.jpg "$DIR"/*.png | wc | awk '// {print $1}'`

number=0

while [ 1 -eq 1 ]; do
    
    number=$RANDOM
    while [ "$number" -le $FLOOR ]; do
          number=$RANDOM
    done
    let "number %= $RANGE"  # Scales $number down within $RANGE.
    COUNTER=1
    for X in "$DIR"/*.jpg "$DIR"/*.png
    do
        if [ $number -eq $COUNTER ]; then
            feh --bg-scale "$X"
        fi
    COUNTER=$(($COUNTER+1))
    done
    COUNTER=1
    sleep 2m
done

There you have it!

Offline

#2 2010-08-14 02:08:14

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: A nice wallpaper randomizer scipt.

I don't mean to steal your thunder, but it pains me to see people try and parse the output of ls. It's far simpler to generate a random file.

wp_dir=/path/to/wallpapers
WP=($wp_dir/*)

echo ${WP[(( RANDOM % ${#WP[@]} ))]}

If you need to traverse multiple directories to discover wallpapers, then just glob on multiple directories, i.e.

WP=(/path/to/foo/* /path/to/bar/*)

But please, let the shell do the work.

Last edited by falconindy (2010-08-14 02:11:07)

Offline

#3 2010-08-14 03:11:14

hellomynameisphil
Member
From: /home/phil/Vancouver
Registered: 2009-10-02
Posts: 257
Website

Re: A nice wallpaper randomizer scipt.

I like something like:

hsetroot -fill "$(find /path/to/wallpapers/ -type f | shuf -n 1)"

It presumes that all the files in your wallpaper dir are image files, but this one-liner works well for me.

Offline

#4 2010-08-14 03:28:35

cesura
Package Maintainer (PM)
From: Tallinn, Estonia
Registered: 2010-01-23
Posts: 1,867

Re: A nice wallpaper randomizer scipt.

You should also make the sleep time into a variable wink

Offline

#5 2010-08-14 04:24:21

wisp558
Member
Registered: 2010-06-10
Posts: 17

Re: A nice wallpaper randomizer scipt.

Ha. This is why I put this up here: I'm a bit of a novice bash scripter, and it's nice to get these sorts of critiques. Yes, I could put the sleep time into a variable, but it's not too painful to change the sleeptime manually in a script largely written for myself. As for shuf... I had no idea that existed. Dammit! That would have been useful a little bit ago. Still, thanks for letting me know.

As for your example falcon, I don't quite understand how yours works. I guess I'm not grokking how () {} and [] work in bash.

Offline

#6 2010-08-14 05:47:32

jck
Member
From: USA
Registered: 2009-05-08
Posts: 98

Re: A nice wallpaper randomizer scipt.

Can you think of any way to make the transitions smooth? like in windows 7

Offline

#7 2010-08-14 17:35:32

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: A nice wallpaper randomizer scipt.

wisp558 wrote:

Ha. This is why I put this up here: I'm a bit of a novice bash scripter, and it's nice to get these sorts of critiques. Yes, I could put the sleep time into a variable, but it's not too painful to change the sleeptime manually in a script largely written for myself. As for shuf... I had no idea that existed. Dammit! That would have been useful a little bit ago. Still, thanks for letting me know.

As for your example falcon, I don't quite understand how yours works. I guess I'm not grokking how () {} and [] work in bash.

Sorry, it's a bit of a mish mash of nesting in that final line. The first two lines are all the setup you need (and you may as well just use the second). This populates an array full of filenames from which to draw from. The echo line is where all the magic happens. I'll break it down...

wp_count=${#WP[@]}  # size of the array -- aka the number of wallpapers
index=$(( RANDOM % wp_count))  # a random number within the bounds of the array size
echo ${WP[index]}  # the wallpaper at the random index generated above

This can all be combined into the frankenstein posted above. Furthermore, it can be passed to hsetroot or feh (or whatever other utility is being used to paint the wallpaper itself) inside the while loop. Because it calls the environment variable RANDOM each time, it'll generate a new pseudo-random wallpaper on each iteration of the loop.

@hellomynameisphil: yes, you can take that shortcut, but the results of find are not meant to be piped and parsed in this way. Shell globbing is an assuredly safe way of handling any possible filename.

Offline

Board footer

Powered by FluxBB