Recently I had a meeting with one of our customers. I was setting up my laptop, plugging it into the beamer. Immediately the customer raised a brow. The beamer was showing a gorgeous HD image of the deepwater horizon oil rig blowing up. The following smalltalk was interesting.

I decided to show some nicer images the next time I'd meet them and this evening I got around to writing a little Perl snippet to download the Astronomy Picture of the Day from apod.nasa.gov <http://apod.nasa.gov>_ and set it as the Windows wallpaper. It uses LWP::get to fetch the main page. It fiddles with the text and downloads the linked image to $dlbase, finally using Win32::API to call SystemParametersInfo to set the background.

Be warned: during testing not every valid .jpeg actually would display as a desktop wallpaper. Another thing is that you really want to specify an absolute path, when calling SystemParametersInfo, or it wont display correctly after logging out and back in again, hence the $dlbasew variable.

    #!/usr/bin/perl  
    use strict;  
    use warnings;  
      
    use Win32;  
    use Win32::API;  
    use LWP::Simple;  
    require Carp;  
      
    # for both constants see winuser.h  
      
    use constant SPI_SETDESKWALLPAPER  => 20;  
    use constant SPIF_UPDATEANDSENDINI => 3; # SPIF_UPDATEINIFILE || SPIF_SENDWININICHANGE  
      
    my $apodbase = 'http://apod.nasa.gov/';  
    my $dlbase = 'c:/Users/mmeyer/Downloads';  
    my $dlbasew = 'c:\\Users\\mmeyer\\Downloads\\';  
      
    chdir $dlbase or die "Couldnt chdir to $dlbase: $!";  
      
    my $content = get($apodbase) or die "Couldn't download image: $!";  
      
    Carp::croak 'Content doesn\'t match' unless $content =~ m/<IMG SRC="(.*)"/g;  
      
    my $urlend = $1;  
      
    Carp::croak 'No url found' unless $urlend =~ m|([^/]+)$|;  
    my $targ = $1;  
      
    my $status = getstore($apodbase . $urlend, $targ);  
    Carp::croak "Couldn't store image: $status" unless is_success($status);  
      
    my $spf = Win32::API->new('user32','SystemParametersInfo', 'IIPI', 'I')  
      or Carp::croak "Could not import function.\n";  
      
    $spf->Call(SPI_SETDESKWALLPAPER, 0, $dlbasew . $targ, SPIF_UPDATEANDSENDINI)  
      or Carp::croak "Could not set wallpaper:\n" . Win32::GetLastError();  
      
    exit;  

See my github scratch repo.

APOD to Desktop Wallpaper

Follow me on Mastodon!