Friday, August 10, 2007

Change Desktop Pattern

Here's another example of how you can make something really nifty in under thirty minutes. A year ago, I wrote a WSH script to change my desktop pattern on startup. I found the right registry keys to change, but unfortunately, the changes wouldn't take effect until the desktop was refreshed. Then, I found an example of how to refresh the desktop here (I guess someone else had thought of this before me), and so I was ready. Note: I haven't tested this in Vista.
To set it up,
1) Create a folder, and a subfolder named "bgtemp".

2) Enter the following and save it as "go.bat", within the folder:
echo "Change of Wallpaper - Ben Fisher"
@echo off
del bgtemp\*.bmp
cscript /NoLogo changed.wsf
3) Enter the following and save as "changed.wsf". Change the code so that strPath is set to any directory with JPG pictures, and strDestination is set to the "bgtemp" directory.
<job id="ChangeBG">
<script language="JScript">
fs = new ActiveXObject("Scripting.FileSystemObject");
var fso, e, file;
var strName; 
var aNames = [];
var strPath = "C:\\Program Files\\Ben's Fractal Screensaver\\Media";
var strDestination = "C:\\Program Files\\Ben's Fractal Screensaver\\Script\\bgtemp\\";
fso = new ActiveXObject("Scripting.FileSystemObject");
e = new Enumerator(fso.GetFolder(strPath).files);
for (e.moveFirst(); ! e.atEnd(); e.moveNext()) 
{
  file = e.item();
  strName = file.name.toString();
  if (strName.indexOf(".jpg") > 1) 
    aNames.push(strName);
}
var nWhich = Math.floor(Math.random() * aNames.length);
var fullpath = strPath + '\\' + aNames[nWhich];
strDestination += aNames[nWhich] + '.bmp';

//Convert to bitmap
var Converter = WScript.CreateObject("Gfx.Converter");
Converter.ToBitmap( fullpath, strDestination );

// Change registry
var objShell = WScript.CreateObject("Wscript.Shell");
objShell.RegWrite("HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\Desktop\\General\\BackUpWallpaper", strDestination);
objShell.RegWrite("HKEY_CURRENT_USER\\Control Panel\\Desktop\\Wallpaper",  strDestination);
objShell.RegWrite("HKEY_CURRENT_USER\\Control Panel\\Desktop\\Wallpaper",  strDestination);

// Refresh, credit to http://www.oreilly.com/pub/h/5091
objShell.Run("%windir%\\System32\\RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters", 1, false);
</script>
</job>
4) You'll also need the "convert to bitmap" ActiveX control from http://dev.remotenetworktechnology.com/wsh/comwsh.htm. Unzip and register the dll with the command: Regsvr32 GfxConverter.ocx. It is kind of interesting to learn that desktop patterns must be .bmp files - if you choose another file type in the Desktop control panel it is silently converted into bmp.

5) Put a shortcut to "go.bat" in your startup folder. c:\Documents and Settings\(you)\Start Menu\Programs\Startup.
And that's it!

No comments: