SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2011-01-27 22:23:55

SuNjACk
#! Die Hard
Registered: 2010-05-23
Posts: 538

Random Wallpapers Script

I made a simple script for random wallpapers

#!/bin/bash

Dir="/media/Data/Images/wallpapers"

if [ ! -d "$Dir" ]; then 
    echo "Not Exist $Dir"
    exit 1
fi

SetBG () {
TotalFiles=$( ls -1 "$Dir" | wc -l )
RandomNumber=$(( $RANDOM % $TotalFiles ))
test ! $RandomNumber = 0 || RandomNumber=1

RandomFile="$( ls -1 $Dir | head -n $RandomNumber | tail -n 1)"

#echo "Selected File: $RandomFile"
feh --bg-fill "${Dir%/}/${RandomFile}"
}

while true; do
    SetBG
    sleep 20m
done

Last edited by SuNjACk (2011-02-19 21:39:03)


"I'd rather run Linux on a 6.5KHz machine through an ARM emulator than run Vista"

Offline

Help fund CrunchBang, donate to the project!

#2 2011-02-19 17:50:54

fouck
New Member
Registered: 2011-02-19
Posts: 1

Re: Random Wallpapers Script

Hello,
i'm a beginner. Can you tell me the name of file extension ?
thanks for this one,

Offline

#3 2011-02-19 18:10:28

anonymous
The Mystery Member
From: Arch Linux Forums
Registered: 2008-11-29
Posts: 8,928

Re: Random Wallpapers Script

You can use sh as the file extension but it will also work fine without it.


Note: ** Please read before posting **

BTW if you wish to contact me, send me an e-mail instead of a PM.

Offline

#4 2011-02-19 21:25:56

luc
#! Die Hard
From: Munich, Germany
Registered: 2010-03-21
Posts: 561

Re: Random Wallpapers Script

hello fouck,
you only have to make the file you save the text to "executable". you can do it in the filemanager if you select the information/properties window of the file or you can use the terminal. In the terminal you have to type

cd the/folder/where/the/script/is
chmod +x script_name

obviously replace the names accordingly in the above. "cd" means "change directory" and will just do that (like clicking in a filemanager) and "chmod" means "change mode" and "+x" means "make this file executable" (or "add the right to execute this")
have fun.

@sunjack

SuNjACk wrote:

my .zshrc (I don't use bash) ...

(from here)
so it seems this does not hold for scripts smile
you could put some quotes around the dir and file names just in case you ever drop a file with a space in there.

Offline

#5 2011-02-19 21:40:01

SuNjACk
#! Die Hard
Registered: 2010-05-23
Posts: 538

Re: Random Wallpapers Script

luc wrote:

hello fouck,
@sunjack

SuNjACk wrote:

my .zshrc (I don't use bash) ...

(from here)
so it seems this does not hold for scripts smile
you could put some quotes around the dir and file names just in case you ever drop a file with a space in there.

thanks for the suggestions, I updated the script


"I'd rather run Linux on a 6.5KHz machine through an ARM emulator than run Vista"

Offline

#6 2011-06-09 04:46:56

ervichu
New Member
Registered: 2011-06-04
Posts: 4

Re: Random Wallpapers Script

there was an error when running ur script please help:
./script: line 18:feh :command not found

Offline

#7 2011-06-09 05:22:14

ali
#! Die Hard
Registered: 2010-05-31
Posts: 1,034

Re: Random Wallpapers Script

ervichu, have you thought about installing the missing package?
if not (and you want to) then

sudo aptitude install feh

Offline

#8 2011-06-09 05:26:01

ali
#! Die Hard
Registered: 2010-05-31
Posts: 1,034

Re: Random Wallpapers Script

or you could do it without feh using corenominals favorite wallpaper manager (?)

just replace

feh --bg-fill "${Dir%/}/${RandomFile}"

with

nitrogen --set-auto "$Dir"/"$RandomFile"

Last edited by ali (2011-06-09 05:31:21)

Offline

#9 2011-06-13 18:25:52

sunfizz98
Carbonated Orange Juice
From: su terminal
Registered: 2011-05-12
Posts: 1,876
Website

Re: Random Wallpapers Script

Does this script check folders recursively, like folders within the designated folder?  Also, do I need to run this script every time I boot the computer?

Last edited by sunfizz98 (2011-06-13 18:54:54)

Offline

#10 2011-06-13 19:19:32

ali
#! Die Hard
Registered: 2010-05-31
Posts: 1,034

Re: Random Wallpapers Script

sunfizz98 wrote:

Does this script check folders recursively, like folders within the designated folder?  Also, do I need to run this script every time I boot the computer?

the original doesn't seam to check recursively, i modified it a little so it does what you asked for (i hope he doesn't mind)

#!/bin/bash

Dir="/home/ali/.backgrounds/"

if [ ! -d "$Dir" ]; then 
    echo "Not Exist $Dir"
    exit 1
fi

SetBG () {
while IFS= read -rd '' file; do list+=("$file"); done < <(find "$Dir" ! -type d -print0)
TotalFiles=${#list[@]}
RandomNumber=$(( $RANDOM % $TotalFiles ))
test ! $RandomNumber = 0 || RandomNumber=1

RandomFile="${list[RandomNumber]}"

#echo "Selected File: $RandomFile"
feh --bg-fill "${RandomFile}"

}

while true; do
    SetBG
    sleep 20m
done

0038.png

and yes, you will obviously have to start this process in the background every time you boot, best way to do it is to insert it in the autostart.sh (if you're on openbox, not sure where the autostart file is in xfce)

Offline

#11 2011-06-14 03:51:08

ervichu
New Member
Registered: 2011-06-04
Posts: 4

Re: Random Wallpapers Script

ali wrote:

ervichu, have you thought about installing the missing package?
if not (and you want to) then

sudo aptitude install feh

i was so dump to not think about installing feh:thanks a ton for replying

Offline

#12 2011-06-14 18:24:28

sunfizz98
Carbonated Orange Juice
From: su terminal
Registered: 2011-05-12
Posts: 1,876
Website

Re: Random Wallpapers Script

I replaced this

feh --bg-fill "${RandomFile}"

with

nitrogen --set-fill "$RandomFile"

I hope that's the setting for "zoomed fill" within nitrogen.  Also, I couldn't find the autostart.sh file within openbox.  Is there a way to insert it into conky to run at startup?

Offline

#13 2011-06-14 19:22:44

ali
#! Die Hard
Registered: 2010-05-31
Posts: 1,034

Re: Random Wallpapers Script

you should check nitrogen --help in a terminal
the zoomed fill (At least in my nitrogen version) is --set-zoom-fill
as for the autostarting the autostart.sh script is under your /home/*****/.config/openbox/autostart.sh
you CAN place that in your conky  using ${exec path/to/script} but i would recommend you use autostart.sh instead since conky is not really meant for that kind of stuff

Offline

#14 2011-06-14 21:02:58

sunfizz98
Carbonated Orange Juice
From: su terminal
Registered: 2011-05-12
Posts: 1,876
Website

Re: Random Wallpapers Script

so I did some reading on the autostart.sh script, but I'm still unsure if I wrote the command correctly to execute the script.

exec /home/sunfizz98/bin/randomize_wallpaper.sh &

I was also wondering if there was a way to make this random wallpaper script to work for multiple users, rather than individualize them for each user.  I have all my wallpapers stored within dropbox. I would like to use a more flexible script within my dropbox folder and only have to edit the autostart.sh script to invoke them.

Offline

#15 2011-06-15 02:37:51

ali
#! Die Hard
Registered: 2010-05-31
Posts: 1,034

Re: Random Wallpapers Script

so what's wrong with this one?
and you don't need the exec in front of the path

Last edited by ali (2011-06-15 15:53:50)

Offline

#16 2011-08-27 21:38:49

sunfizz98
Carbonated Orange Juice
From: su terminal
Registered: 2011-05-12
Posts: 1,876
Website

Re: Random Wallpapers Script

@ali
Your script works like a charm in openbox, but it seems to crash my conky and give me a black background in my xfce environment.  I thought it was just nitrogen so I tried out feh, but both give the same result.

Last edited by sunfizz98 (2011-08-27 22:22:41)

Offline

#17 2011-08-28 13:42:17

ali
#! Die Hard
Registered: 2010-05-31
Posts: 1,034

Re: Random Wallpapers Script

what do you mean it crashed your conky?

Offline

#18 2011-08-28 21:32:13

sunfizz98
Carbonated Orange Juice
From: su terminal
Registered: 2011-05-12
Posts: 1,876
Website

Re: Random Wallpapers Script

After the random_wallpaper script is executed, my desktop became a black wallpaper and the conky display disappeared.  Also, I would have to run conky again if I want it to be visible.

Offline

#19 2011-08-29 03:50:23

ali
#! Die Hard
Registered: 2010-05-31
Posts: 1,034

Re: Random Wallpapers Script

if i remember correctly xfce does not use feh nor nitrogen to set up the wallpaper
try searching for a command line to set the xfce4 wallpaper and simply replace the nitrogen/feh line in the script
i haven't searched very much for it but try this

xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s <image-file>

and conky and the script are unrelated (unless you put the script in conky)

Offline

#20 2011-12-22 20:42:46

squid
New Member
From: Salt Lake City, UT
Registered: 2011-12-22
Posts: 2

Re: Random Wallpapers Script

When I try to run this script, this happens:

dan@crunchbang:~$ sh .scripts/wallpaper.sh 
.scripts/wallpaper.sh: 24: arithmetic expression: expecting primary: "  % 17 "

Yes, my avatar is an octopus.

Offline

#21 2012-01-08 12:58:43

SabreWolfy
#! Die Hard
Registered: 2009-03-09
Posts: 1,276

Re: Random Wallpapers Script

@SuNjACk: Thanks for the script smile Modified it for nitrogen and removed the loop. I will try putting it into a cron job to run every X hours/whatever.


Support #!Waldorf • Debian sid • Xubuntu • siduction • Peppermint • OpenBox • Xfce • LXDE •

Offline

#22 2012-01-08 13:28:16

SabreWolfy
#! Die Hard
Registered: 2009-03-09
Posts: 1,276

Re: Random Wallpapers Script

Sigh. cron can be a real pain sometimes. I've modified the script:

#!/bin/bash

/bin/date >> /tmp/test1    # TO CHECK THE CRON JOB IS RUNNING

Dir="/home/XXX/images/wallpapers"

if [ ! -d "$Dir" ]; then
    echo "$Dir does not exist"
    exit 1
fi

SetBG () {
TotalFiles=$( ls -1 "$Dir" | grep jpg | wc -l )
RandomNumber=$(( $RANDOM % $TotalFiles ))
test ! $RandomNumber = 0 || RandomNumber=1

RandomFile="$( ls -1 $Dir | head -n $RandomNumber | tail -n 1)"

echo "$Dir"/"$RandomFile" >> /tmp/test1   # TO CHECK

/usr/bin/nitrogen --set-scaled "$Dir"/"$RandomFile"
}

SetBG

This is saved as ~/images/wallpapers/random.sh and is chmod to 744. If I run the script on it's own, the wallpaper changes.

I setup a new cron job:

*    *    *    *    *    /home/XXX/images/wallpapers/random.sh

And then tail the /tmp/test1 file. Every minute, the date and the full path and filename are appended to the file, so the script is running every minute. I've even inserted the path for nitrogen, but still the wallpaper will not change every minute.

Setting the wallpaper manually to "1.jpg" and then hard-coding the script (commenting out the existing nitrogen line and adding a new one) to set it to "2.jpg" also does not work. Adding the "--save" parameter to nitrogen also does not work. Clearly there is a trick to using nitrogen in cron...?

Last edited by SabreWolfy (2012-01-08 13:35:23)


Support #!Waldorf • Debian sid • Xubuntu • siduction • Peppermint • OpenBox • Xfce • LXDE •

Offline

#23 2012-01-08 13:34:07

2ManyDogs
dv#!
From: elsewhere
Registered: 2011-11-22
Posts: 1,346

Re: Random Wallpapers Script

I went through this particular pain a few weeks ago (including writing the time to a test file to make sure the script was running smile). Apparently, cron can't access the display without explicit instructions.

Here's the crontab that is working for me:

*/10 * * * * DISPLAY=:0.0 /home/bsh/.scripts/random-wallpaper

Let me know if it works.


Be eggsalad to each other.

Offline

#24 2012-01-08 13:36:52

SabreWolfy
#! Die Hard
Registered: 2009-03-09
Posts: 1,276

Re: Random Wallpapers Script

^  Thanks! Just now found your thread on this ...

Edit: w00t! It works smile Thanks smile

Last edited by SabreWolfy (2012-01-08 13:41:10)


Support #!Waldorf • Debian sid • Xubuntu • siduction • Peppermint • OpenBox • Xfce • LXDE •

Offline

Help fund CrunchBang, donate to the project!

#25 2012-01-08 14:26:54

2ManyDogs
dv#!
From: elsewhere
Registered: 2011-11-22
Posts: 1,346

Re: Random Wallpapers Script

You're very welcome. That was a painful learning experience for me, especially after it stopped working for no apparent reason and then started working again for reasons I still don't understand. I really started to hate cron. hmm


Be eggsalad to each other.

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo