Ubuntu Forums ubuntu.com - launchpad.net - ubuntu help  

Go Back   Ubuntu Forums > The Ubuntu Forum Community > Forum Archive > Absolute Beginner Talk
Register Reset Password Forum Help Forum Council Search Today's Posts Mark Forums Read

Hello, Unregistered You are browsing a READ only archive of the main support categories pre 4/21/2008. You will not be able to post or reply any threads in this section.

Absolute Beginner Talk
The perfect starting place to find out more about computers, Linux and Ubuntu.

 
Thread Tools Display Modes
Old December 30th, 2006   #1
davidY
Just Give Me the Beans!
 
Join Date: Jul 2006
Beans: 61
Wallpaper changing shell script

Hi, I have searched this forums for a way to make wall papers change randomly. Now this is something so simple I was sure I could find an easy program. Unfortunately all the things seem to be pretty buggy or did not run or had problems. Thus, I was forced to learn about shell scripting and then how to use cron (hey, i should learn these things anyway). Anyway, here is code from somewhere that I found:

Code:
#!/bin/sh
#Go to our pics
cd /media/documents/Desktops
#Get all the picture files names
IMGS=`find . -iname '*.jpg' -o -iname '*.png' -o -iname '*.svg'`
#Find out how many pictures we got
N=`echo $IMGS | wc -w`
#Take a random number between 1 and N
((N=RANDOM%N))

#Cut the file to get the n'th picture
BGNAME=`echo $IMGS | cut -d '/' -f $N | cut -d ' ' -f 1`

#Call upon gconftool to write into the "registry"
gconftool-2 -t str --set /desktop/gnome/background/picture_filename "/media/documents/Desktops/$BGNAME"
Now this code would work perfectly, except for the fact that the list of names in BGNAME has to be one single word, which makes it unable to handle either directories, or file names with spaces. I don't mind having no directories, but I would prefer to have file names with spaces. Any solutions?
davidY is offline   Reply With Quote
Old December 30th, 2006   #2
davidY
Just Give Me the Beans!
 
Join Date: Jul 2006
Beans: 61
Re: Wallpaper changing shell script

On second thoughts, (although I will not prefer this alternative), is it possible to rename all files with spaces to files with underscores or other characters?
davidY is offline   Reply With Quote
Old December 30th, 2006   #3
wildkarde
A Carafe of Ubuntu
 
Join Date: Jul 2006
My beans are hidden!
Kubuntu 7.10 Gutsy Gibbon
Re: Wallpaper changing shell script

you could try this nifty shell script
Code:
#! /bin/bash
# blank-rename.sh
#
# Substitutes underscores for blanks in all the filenames in a directory.

ONE=1                     # For getting singular/plural right (see below).
number=0                  # Keeps track of how many files actually renamed.
FOUND=0                   # Successful return value.

for filename in *         #Traverse all files in directory.
do
     echo "$filename" | grep -q " "         #  Check whether filename
     if [ $? -eq $FOUND ]                   #+ contains space(s).
     then
       fname=$filename                      # Yes, this filename needs work.
       n=`echo $fname | sed -e "s/ /_/g"`   # Substitute underscore for blank.
       mv "$fname" "$n"                     # Do the actual renaming.
       let "number += 1"
     fi
done   

if [ "$number" -eq "$ONE" ]                 # For correct grammar.
then
 echo "$number file renamed."
else 
 echo "$number files renamed."
fi 

exit 0
wildkarde is offline   Reply With Quote
Old December 30th, 2006   #4
davidY
Just Give Me the Beans!
 
Join Date: Jul 2006
Beans: 61
Re: Wallpaper changing shell script

... the funny thing is that about the same lines of code were used to maintian correct grammar. thanks for that suggestion, but before i go doing some mass 1000+ files file renaming, I would like to find a way to keep the names.
heaps of pictures like these
davidY is offline   Reply With Quote
Old January 1st, 2007   #5
davidY
Just Give Me the Beans!
 
Join Date: Jul 2006
Beans: 61
Re: Wallpaper changing shell script

This is the final solution. It works for any thing as long as all pictures are in the directory, regardless of name formatting, subdirectories or hard links(not sure about that one though)

Code:
#!/bin/sh

# Set your folder with the pics
picsfolder="/media/documents/Desktops/"

# Go to your folder with the pics
cd $picsfolder

# Create an array of the files
files=(./*/*.jpg)

# Get the size of the array
N=${#files[@]}

# Select a random number between this range 
((N=RANDOM%N))

# Get the name of this file
randomfile=${files[$N]}

# start of gconftool command and set the desktop
gconftool-2 -t str --set /desktop/gnome/background/picture_filename "$picsfolder$randomfile"
davidY is offline   Reply With Quote
Old January 26th, 2007   #6
Kat of Zion
A Carafe of Ubuntu
 
Kat of Zion's Avatar
 
Join Date: Jan 2007
Location: SoCal
Beans: 92
Kubuntu 8.04 Hardy Heron
Send a message via ICQ to Kat of Zion Send a message via AIM to Kat of Zion Send a message via MSN to Kat of Zion Send a message via Yahoo to Kat of Zion Send a message via Skype™ to Kat of Zion
Re: Wallpaper changing shell script

So if I copy this script, what kind of file do I save it as?

Quote:
Originally Posted by davidY View Post
This is the final solution. It works for any thing as long as all pictures are in the directory, regardless of name formatting, subdirectories or hard links(not sure about that one though)

Code:
#!/bin/sh

# Set your folder with the pics
picsfolder="/media/documents/Desktops/"

# Go to your folder with the pics
cd $picsfolder

# Create an array of the files
files=(./*/*.jpg)

# Get the size of the array
N=${#files[@]}

# Select a random number between this range 
((N=RANDOM%N))

# Get the name of this file
randomfile=${files[$N]}

# start of gconftool command and set the desktop
gconftool-2 -t str --set /desktop/gnome/background/picture_filename "$picsfolder$randomfile"
__________________
Love my Stego - Happiest Geek Gal in fandom!
Kat of Zion is offline   Reply With Quote

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 04:00 AM.


vBulletin ©2000 - 2009, Jelsoft Enterprises Ltd. Ubuntu Logo, Ubuntu and Canonical © Canonical Ltd. Tango Icons © Tango Desktop Project. lingonberry