I am very new to Perl and this is the first program I've written that I consider to be useful.

The program selects a random image to be used as a background for IceWM by editing the relevant line in ~/.icewm/preferences. It then checks to see if IceWM is running. If it is, it restarts icewmbg. If it is not, it starts icewm-session.

I've edited my .xinitrc file to call this program on startx and I have also added "F2 = icewmRandombg" to my ~/.icewm/keys file so pressing F2 gives me a new background.

I'm fairly certain the second chdir command isn't necessary, but I'm not absolutely certain, and it's not hurting anything, so I left it. I put it there so icewm-session would be called from my home directory.

#!/usr/bin/perl -w # This program edits ~/.icewm/preferences to set a random wallpaper. # It then checks to see if IceWM is running. If it is, it restarts # icewmbg. If it isn't it starts icewm-session. use strict; my $image_directory = "/home/jivy/media/images/wallpaper"; my @image_files = glob "$image_directory/*"; my $wallpaper = $image_files[rand @image_files]; chdir "/home/jivy/.icewm" or die "Cannot change to .icewm: ($!)"; my $pref = "preferences"; my $new_pref = "preferences.tmp"; my $bak = "preferences.bak"; open PREF, "< $pref"; open NEW_PREF, ">> $new_pref"; while (<PREF>) { s#$image_directory/.*#$wallpaper"#; print NEW_PREF $_; } close PREF; close NEW_PREF; rename ($pref,$bak); rename($new_pref,$pref); chdir "/home/jivy" or die "Cannot change to jivy: ($!)"; if (`ps -e | grep icewm-session`) { exec "icewmbg -r"; } else { exec "icewm-session"; }


In reply to Random Desktop Background for IceWM by Texadan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.