I'm trying to make a script that changes the desktop wallpaper (Windows XP) every time its run. To do this I wrote a script that edits a Registry value containing the path to the currently selected wallpaper, and it seems to be working, but the change doesn't take effect immediately, like it would through the Display Properties dialog. I have to open up the dialog myself and just hit 'Apply' (the wallpaper is already there in the list, selected). Is there a way to make the change take effect automatically, without having to restart the PC or anything?
#random wallpaper changer!
import _winreg
from os import walk
from os.path import exists
from random import randint
#first grab a registry handle.
#_winreg.KEY_SET_VALUE
handle = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,r'Control Panel\Desktop',0,_winreg.KEY_WRITE)
print "Registry Handle Created with KEY_SET_VALUE mask"
def GenerateListOfWallpapers():
targetDir = 'C:\Documents and Settings\Enrico Jr\My Documents\Jr\'s Wallpapers'
fileNames = []
filePaths = []
if exists(targetDir):
#proceed to make the list of files
for x,y,z in walk(targetDir):
for name in z:
fileNames.append(name)
for item in fileNames:
filePaths.append(targetDir + '\\' + item)
return filePaths
def RandomlySelectWallpaper(filePaths):
index = randint(0,len(filePaths)-1)
RandomlySelectedWallpaper = filePaths[index]
return RandomlySelectedWallpaper #it should be a string...
#now to edit the wallpaper registry key
newWallpaper = RandomlySelectWallpaper(GenerateListOfWallpapers())
print "Registry Handle Created."
print "Random wallpaper selected."
_winreg.SetValueEx(handle,'ConvertedWallpaper',0,_winreg.REG_SZ,newWallpaper)
print "New wallpaper value set."