The Curious Dev

Various programming sidetracks, devops detours and other shiny objects

Nov 26, 2012 - 3 minute read - Groovy Utility

Desktop Wallpaper Grabber

A feature I really like about Window 7 (and 8) is the ability to make the desktop cycle through all the images in a given directory structure, perfect for those 1000’s of photos we’ve taken on our travels.

Over the last couple years I’ve been using a little script I wrote to extract the current image and copy it into a shared directory in my dropbox. This directory is shared with my wife and we both have a shortcut on our desktops to click when we see an image we like to share those “Nîmes was awesome wasn’t it!” kind of moments.

Arena Nîmes

Groovy is my tool of choice and after my recent move to Windows 8, I’ve had to rejig the script as Microsoft have changed the registry value used.

Windows 7

Windows 7 stores the path to the current desktop image here HKCU\Software\Microsoft\Internet Explorer\Desktop\General\WallpaperSource

Pont du Gard

Extracting the path to the image is straight forward:

def extractWindows7Value() {
	def sourceData = 'REG QUERY "HKCU\\Software\\Microsoft\\Internet Explorer\\Desktop\\General" /v WallpaperSource'.execute().text
	//WallpaperSource    REG_SZ    D:\_media\photos\2011\2011-03-31\IMG_1983.JPG
	def imageFilename
	sourceData.eachLine() { String l ->
		if (l.toLowerCase().contains("jpg")) {
			//registry key seems to split on 4 spaces        
			l.split('    ').each() { String row ->
				if (row.size() > 0 && !row.contains('WallpaperSource') && !row.contains('REG_SZ')) {
					println row
					imageFilename = row.trim()
				}
			}
		}
	}
	
	return imageFilename
}

Windows 8

Windows 8 stores the path to the current desktop image here in hex-encoded format: HKEY_CURRENT_USER\Control Panel\Desktop\TranscodedImageCache

Knowing this, we can then extract and convert it using the org.codehaus.groovy.runtime.EncodingGroovyMethods utility class:

def extractWindows8Value() {
	def sourceLine = ""
	def sourceData = 'REG QUERY "HKEY_CURRENT_USER\\Control Panel\\Desktop" /v TranscodedImageCache /t REG_BINARY'.execute().text
	// TranscodeImageCach REG_BINARY 7AC30100EC364600800D...00000000000000000
	sourceData.eachLine { line ->
		if (line.indexOf('TranscodedImageCache') > 0) {
			sourceLine = line.split('    ')[3] //this gets only the hex values we want
		}
	}

	//trim off initial junk characters
	def val2 = sourceLine.trim().substring(48)

	//step through each quad and grab the first hex pair
	String output = ""
	while (val2.length() > 0) {
		def hexDuo = val2.substring(0, 2)
		//println val2
		val2 = val2.substring(4)
		
		if (hexDuo != "00") {
			print hexDuo
			output += hexDuo
		}
	}

	//decode our hex values into a string
	byte[] bytesArray = EncodingGroovyMethods.decodeHex(output)	
	String imageFilename = new String(bytesArray)
	println "\n" + imageFilename
	
	return imageFilename
}

Copy File with AntBuilder

Armed with the path to the current image, we can then simply copy the image to the provided dropbox path using the Ant builder copy:

new AntBuilder().copy ( 
		file: imageFilename, 
		tofile: dropBoxDir+imageFilename.substring(imageFilename.lastIndexOf('\\')+1,imageFilename.size()) 
)

Summary

There’s also just a little windows batch script to kickoff the script, with two arguments: the dropbox location and the version of windows (7 or 8). I plan to add some other systems in the future, probably starting with Ubuntu.

One aspect of this process is sometimes you’ll click on the shortcut when the image is about to change so you’ll end up with a different image than exepcted, but I’m willing to deal with that for the simplicity provided by this script.

Code: DesktopWallpaperGrabber