O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  

Buy the book!
Yahoo! Hacks
By Paul Bausch
October 2005
More Info

Randomize Your Windows Desktop Background
Use the Yahoo! Image Search web service to spin a virtual roulette wheel and let fate choose your desktop background image
The Code
[Discuss (0) | Link to this hack]

The Code

This hack contacts Yahoo! via its Web Services API, performs an image search, chooses one of the responses at random, downloads the image, and sets it as your desktop background. Because Windows expects desktop backgrounds to be the bitmap image type and most web images are JPEGs, the script will also have to convert any image from a JPEG to a bitmap.

This conversion process is out of the scope of the Windows scripting environment, and this hack uses a third-party ActiveX component that converts JPEG files to bitmaps. An ActiveX component is a piece of software that is meant to be used by other programs, and it usually performs a specific task. Alex K. Angelopoulos wrote this particular component, called Convert to Bitmap, and published it on his web site. You can download a copy at http://dev.remotenetworktechnology.com/wsh/comwsh.htm. Once you've downloaded the file and unzipped the contents, you'll need to install the component so it's available to scripts. From a command line, browse to the directory where you unzipped the package and type the following command:

Regsvr32 GfxConverter.ocx

With the component installed, you'll be set to run the script. Save the following code to a file called Y!background.vbs:

	' Y!Background.vbs
	' Finds a random image via the Yahoo! Image API and
	' sets it as the desktop background image.
	' This script depends on GfxConverter.ocx that you
	' download here:
	' http://dev.remotenetworktechnology.com/wsh/comwsh.htm
	' Click/Save "Convert to Bitmap", unzip, and install
	' from the command line, like this:
	' Regsvr32 GfxConverter.ocx
	' Edit the following line to change the "theme" of
	' your background. Double-click this file to run it.
	strQuery = "landscape"

	' the Yahoo! App ID -- Please don't change this
	Const APP_ID = "ybackground"

	' Using the query, find a random image via Yahoo!
	'Construct a Yahoo! Search Query
	strReqURL = "http://api.search.yahoo.com/" & _
                "ImageSearchService/V1/imageSearch?" & _
				"appid=" & APP_ID & _
				"&query=" & strQuery & _
				"&results=50" & _

	'Start the XML Parser
	Set MSXML = CreateObject("MSXML.DOMDocument")

	'Set the XML Parser options
	MSXML.Async = False

	'Make the Request
	strResponse = MSXML.Load(strReqURL)
	If MSXML.parseError.errorCode <> 0 Then
       WScript.Echo("Error! " + MSXML.parseError.reason)
    End If

    'Make sure the request loaded 
	If (strResponse) Then

        'Find the total available
		Set ResultSet = MSXML.SelectSingleNode("//ResultSet")
		intTotalReturned = ResultSet.getAttribute("totalResultsReturned")

		'Pick a random number
		RandomNumber = Int(intTotalReturned * Rnd + 1)

        'Load the results
		Set Results = MSXML.SelectNodes("//Result")

		'Loop through the results
		For x = 0 to Results.length - 1
		If x = RandomNumber Then

                strImageURL = Results(x).SelectSingleNode("Url").text
        End If

		'Unload the results
		Set Results = Nothing
		Set ResultSet = Nothing

    End If

    'If no image URL is found, send a message
    If strImageURL = "" Then 
        WScript.Echo "No image found! Try again." 
    End If

	' Save the image locally to the root c:\ folder 
	Set fs = CreateObject("Scripting.FileSystemObject") 
	Set xmlhttp = CreateObject("Msxml2.SERVERXMLHTTP") 
	xmlhttp.Open "GET", strImageURL, false 

	fs.CreateTextFile "c:\" & strImageFile & ".jpg"

	'Create a Stream instance 
	Dim objStream 
	Set objStream = CreateObject("ADODB.Stream")

	'Open the stream 
	objStream.Type = 1 'adTypeBinary 
	objStream.Write xmlhttp.responseBody 
	objStream.SaveToFile "c:\" & strQuery & ".jpg", 2 'adSaveCreateOverwrite 
	Set objStream = Nothing 
	Set xmlhttp = Nothing
	Set fs = Nothing

	' Convert the jpeg to a bitmap 
	Set Converter = CreateObject("Gfx.Converter") 
	Converter.ToBitmap "c:\" & strQuery & ".jpg", "c:\" & strQuery & ".bmp" 
	Set Converter = Nothing

	' Set the newly created bitmap to background
	' and refresh
	strRegRoot = "HKEY_CURRENT_USER\Software\Microsoft\" & _
                 "Internet Explorer\Desktop\General\"
    strRegRoot2 = "HKEY_CURRENT_USER\Control Panel\Desktop\"
	Set Shell = WScript.CreateObject("Wscript.Shell")
	Shell.RegWrite strRegRoot & "BackUpWallpaper", "c:\" & strQuery & ".bmp"
	Shell.RegWrite strRegRoot & "Wallpaper", "c:\" & strQuery & ".bmp"
	Shell.RegWrite strRegRoot2 & "Wallpaper", "c:\" & strQuery & ".bmp"
	Shell.Run "%windir%\System32\RUNDLL32.EXE " & _
              "user32.dll,UpdatePerUserSystemParameters", 1, False
    Set Shell = Nothing

Note that the first line of the script sets the theme of your desktop background. If you'd like something different from landscape images, simply edit this line to whatever you'd like to see.

Also keep in mind that you can tweak the request URL variable strReqURL to get different results. The URL is currently set to exclude adult images: adult_ok is set to 0. If you change this value to 1, you might find adult images in your results. Of course, changing the strReqURL value to something more adult-oriented will have an effect on the content of the search as well. Even with the filter set to keep adult images out, understand that Yahoo!'s filter isn't perfect and some adult images might find their way into the mix.

O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.