Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi all! We have been experiencing an issue on site where threads have been missing the latest postings. The platform host Vanilla are working on this issue. A workaround that has been used by some is to navigate back from 1 to 10+ pages to re-sync the thread and this will then show the latest posts. Thanks, Mike.
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Help me with my random desktop script

  • 27-08-2006 2:06pm
    #1
    Closed Accounts Posts: 4,842 ✭✭✭


    Howdy,
    I've been trying to come up with a script to refresh the desktop with a random desktop-background.

    This is what I have so far:
    [php]#!/bin/bash

    DIRECTORY=/home/steve/Pictures/RandomDesktops/ # the directory with your files in it
    # This directory can only contain image files!
    DIRECTORYCOUNT=`ls $DIRECTORY | wc -l` # counts the files in your chosen directory
    VARIABLE=$[($RANDOM % $DIRECTORYCOUNT) + 1] # find a random element

    if [ $VARIABLE = 0 ] ; then # can't find the 0th file, returns 2
    VARIABLE=2
    fi

    # Now load each of the files in the directory into an array
    i="1"
    for fileName in `ls $DIRECTORY`
    do
    array[$i]="$fileName"
    i=`expr $i + 1`
    done

    # echo $DIRECTORY${array[$VARIABLE]} # Make sure the correct path is echoed

    gconftool-2 -t str -s /desktop/gnome/background/picture_filename $DIRECTORY${array[$VARIABLE]}
    gconftool-2 -t str -s /desktop/gnome/background/picture_options “stretched”[/php]

    So it counts all the files in the specified directory (only contains image files the right size for a desktop) and chooses a random number between 1 and the total number of files, assigns each filename to an element in an array and then uses that random number to pick out a random element in that array.

    It then, supposedly, assigns this random element to the desktop....

    When I run it, the desktop just goes back to the default colour.

    If I choose to change the desktop afterwards then the change desktop dialogue has the chosen random desktop selected and once it's opened it refreshes the desktop with that desktop.

    Anyone know if there's a command I have to run to refresh the desktop? I've tried refreshing the gnome-panel and nautilus but obviously this has no effect on the desktop


Comments

  • Closed Accounts Posts: 2,148 ✭✭✭angelofdeath


    kde has an option for this in the control center :p
    for gnome theres some options here

    gl


  • Closed Accounts Posts: 4,842 ✭✭✭steveland?


    Heh, cheers but I'm sticking with Gnome, don't like KDE at all.

    Also I'd really like to use a script I've made myself that I can run whenever I want. It's a little bit more gratifying to have something you've written yourself.

    I reckon my code should work if I can find some way of refreshing the desktop when it's rung.


  • Closed Accounts Posts: 2,148 ✭✭✭angelofdeath


    http://ubuntuguide.org/wiki/Dapper#How_to_refresh_GNOME_desktop
    How to refresh GNOME desktop
    Read #General Notes
    killall nautilus

    hardly ideal though if you're file browsing and the wallpaper changes:rolleyes:
    sorry probably not being of much use:o


  • Closed Accounts Posts: 4,842 ✭✭✭steveland?


    http://ubuntuguide.org/wiki/Dapper#How_to_refresh_GNOME_desktop


    hardly ideal though if you're file browsing and the wallpaper changes:rolleyes:
    sorry probably not being of much use:o
    Nah doesn't work, as you can see from the end of my first post I tried it, that just refreshes that panel, not the desktop.


  • Registered Users, Registered Users 2 Posts: 865 ✭✭✭generalmiaow


    Hi, I tried your script and had the same problem. but when I used

    [PHP]gconftool-2 -t str --set /desktop/gnome/background/picture_filename $DIRECTORY${array[$VARIABLE]}

    gconftool-2 -t str --set /desktop/gnome/background/picture_options "stretched"[/PHP]

    instead of your last two lines, it worked immediately
    I have NO IDEA why. --set is the same as -s ain't it?

    in case I have changed something else without realising it, here is my whole script

    [PHP]#!/bin/bash

    DIRECTORY=~/pics/ # the directory with your files in it
    # This directory can only contain image files!
    DIRECTORYCOUNT=`ls $DIRECTORY | wc -l` # counts the files in your chosen directory
    VARIABLE=$[($RANDOM % $DIRECTORYCOUNT) + 1] # find a random element

    if [ $VARIABLE = 0 ] ; then # can't find the 0th file, returns 2
    VARIABLE=2
    fi

    # Now load each of the files in the directory into an array
    i="1"
    for fileName in `ls $DIRECTORY`
    do
    array[$i]="$fileName"
    i=`expr $i + 1`
    done

    echo $DIRECTORY${array[$VARIABLE]} # Make sure the correct path is echoed

    gconftool-2 -t str --set /desktop/gnome/background/picture_filename $DIRECTORY${array[$VARIABLE]}
    gconftool-2 -t str --set /desktop/gnome/background/picture_options "stretched"
    [/PHP]

    tell me if it works. Maybe there's a bug in gconftool.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 865 ✭✭✭generalmiaow


    to add to the oddness, sometimes using the -s way works on the command line (save yourself some trouble and make your terminal transparent maybe) and sometimes it doesn't. so far the --set way has always worked.


  • Closed Accounts Posts: 4,842 ✭✭✭steveland?


    Hi, I tried your script and had the same problem. but when I used

    [PHP]gconftool-2 -t str --set /desktop/gnome/background/picture_filename $DIRECTORY${array[$VARIABLE]}

    gconftool-2 -t str --set /desktop/gnome/background/picture_options "stretched"[/PHP]

    instead of your last two lines, it worked immediately
    I have NO IDEA why. --set is the same as -s ain't it?

    in case I have changed something else without realising it, here is my whole script

    [PHP]#!/bin/bash

    DIRECTORY=~/pics/ # the directory with your files in it
    # This directory can only contain image files!
    DIRECTORYCOUNT=`ls $DIRECTORY | wc -l` # counts the files in your chosen directory
    VARIABLE=$[($RANDOM % $DIRECTORYCOUNT) + 1] # find a random element

    if [ $VARIABLE = 0 ] ; then # can't find the 0th file, returns 2
    VARIABLE=2
    fi

    # Now load each of the files in the directory into an array
    i="1"
    for fileName in `ls $DIRECTORY`
    do
    array[$i]="$fileName"
    i=`expr $i + 1`
    done

    echo $DIRECTORY${array[$VARIABLE]} # Make sure the correct path is echoed

    gconftool-2 -t str --set /desktop/gnome/background/picture_filename $DIRECTORY${array[$VARIABLE]}
    gconftool-2 -t str --set /desktop/gnome/background/picture_options "stretched"
    [/PHP]

    tell me if it works. Maybe there's a bug in gconftool.
    Yes indeedy, works perfectly with --set instead of -s.

    I'd tried --set before hand and it hadn't worked which is why I tried -s (should be the same).

    Either you must've rectified some silly little syntax error that I'd left in or my laptop's just gone crazy :)

    Cheers mate.

    Now, how do I schedule this to run every 30 minutes or so?

    I never got the hang of cron...


  • Closed Accounts Posts: 4,842 ✭✭✭steveland?


    to add to the oddness, sometimes using the -s way works on the command line (save yourself some trouble and make your terminal transparent maybe) and sometimes it doesn't. so far the --set way has always worked.
    Yeh that was another funny thing... it worked perfectly when I chose a path to a desktop file and ran it on the command line but not in the script.

    Working perfectly now though :D


  • Registered Users, Registered Users 2 Posts: 865 ✭✭✭generalmiaow


    cron should be easy enough

    do

    [PHP]crontab -e[/PHP]

    and then type

    30 * * * * ~/random.sh //execute random.sh on 30th minute of every hour, every day, every month, every day of week

    0 * * * * ~/random.sh //on the 0th minute as well, I always forget this one

    and then Ctrl+O, enter, and then ctrl + x

    should work. I'm actually taking up using your script :)


  • Closed Accounts Posts: 4,842 ✭✭✭steveland?


    Cheers, given that a go, I've set it to change in 5 minute increments (*/5 * * * ~/random) so I'll let you know in 5 minutes :)

    Thanks for the help


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 865 ✭✭✭generalmiaow


    steveland? wrote:
    Cheers, given that a go, I've set it to change in 5 minute increments (*/5 * * * ~/random) so I'll let you know in 5 minutes :)

    Thanks for the help

    no problem :) one other thing that may or may not have contributed is that a string containing a filename with a (literal) space might echo correctly but won't work as part of a command. like gconftool will see ~/holy crap.jpg as ~/holy . and it would need ~/holy\ crap.jpg or something to work. I'm sure there's some amazing way of putting in those slashes that somebody knows. or putting quotes around it might work.


  • Closed Accounts Posts: 4,842 ✭✭✭steveland?


    Ah yeh, I got used to not putting spaces into filenames fairly quickly when I started using Linux :)


Advertisement