Updated 2015-11-30 19:37:56 by dbohdan


The script below was written in October 2002, nowadays I would use TWAPI to change the desktop background:


This script sets the daily User Friendly strip as my desktop background. I post it here because it may be useful to someone. It uses http to download the image, Img to convert the gif to bmp and ffidl to call Windows API.

The difficult part was locating the information about the Windows API. Please tell me if there is any obvious way to improve this.

Keywords: Setting Windows Desktop background, SystemParametersInfo , SPIF_SENDCHANGE , SPIF_UPDATEINIFILE , SPIF_SENDWININICHANGE
 package require http
 package require Img
 load ffidl05.dll
 set target_page "http://www.userfriendly.org" ; # the page to molest
 set gif_image_path "c:\\gif.gif"              ; # download the image here..
 set bmp_image_path "c:\\bmp.bmp"       ; # ... and feed Windows with this

 #Sugar is sweet ...
 proc between { start "and" end "in" text} {
    set string_start  [string first $start $text]
    set string_end    [string first $end $text $string_start]
    set start_removed [string replace [string range $text $string_start $string_end] 0 [expr [string length $start ]-1] ]
    return [string replace $start_removed end end ]

 #I'm behind a proxy here, I must configure it...
 http::config -proxyhost -proxyport 8080
 #... now I can get the initial page
 set html [http::data [http::geturl $target_page]]
 #Image size parameters change, get the whole <IMG> first:
 set url [between "<IMG ALT=\"Latest Strip\"" and "</A>" in $html]
 #I only want the image url...
 set url [between "SRC=\"" and "\">" in $url]
 #...and write the image to a file:
 set gif_file [open $gif_image_path w+]
 http::geturl $url -blocksize 1024 -channel $gif_file
 close $gif_file
 #I have to convert to BMP to be able to use the Windows API call
 set image [image create photo -file $gif_image_path]
 $image write $bmp_image_path -format bmp
 #this fiddles with user32.dll and refreshes the background immediately
 ffidl::callout dll_SystemParametersInfo {int int pointer-utf8 int} int       [ffidl::symbol user32.dll  SystemParametersInfoA]
 dll_SystemParametersInfo 20 0 $bmp_image_path 3

JM 27 Jul 2011 - As mentioned above, using twapi looks like this...
(to have the hostname written to your wallpaper, for example)
  package require twapi
  package require Img

  canvas .c
  pack .c
  .c create text 100 100 -text "hostname: [info hostname]"
  [image create photo -data .c] write kal1.gif

  set gif_image_path "kal1.gif"
  set bmp_image_path "kal1.bmp"

  #I have to convert to BMP to be able to use the Windows API call
  set image [image create photo -file $gif_image_path]
  $image write $bmp_image_path -format bmp

  twapi::set_desktop_wallpaper kal1.bmp