Working with the Windows Registry

T

teh_sAbEr

Hi everybody. I'm trying to write a script that'll change desktop
wallpaper every time its run. Heres what I've gotten so far:

#random wallpaper changer!
import _winreg
from os import walk
from os.path import exists
from random import randint

#first grab a registry handle.
handle = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Control Panel
\Desktop',_winreg.KEY_SET_VALUE)

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."

The problem is, every time I run it, I get an "Access Denied" error
when it tries to execute
_winreg.SetValueEx(), even though i've opened the key with the
KEY_SET_VALUE mask like it said in the help docs. Could there be
another problem or a better way to do this?
 
S

s0suk3

Hi everybody. I'm trying to write a script that'll change desktop
wallpaper every time its run. Heres what I've gotten so far:

#random wallpaper changer!
import _winreg
from os import walk
from os.path import exists
from random import randint

#first grab a registry handle.
handle = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Control Panel
\Desktop',_winreg.KEY_SET_VALUE)

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."

The problem is, every time I run it, I get an "Access Denied" error
when it tries to execute
_winreg.SetValueEx(), even though i've opened the key with the
KEY_SET_VALUE mask like it said in the help docs. Could there be
another problem or a better way to do this?

Note the line

#first grab a registry handle.
handle = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, 'Control Panel
\Desktop', _winreg.KEY_SET_VALUE)

OpenKey() takes four arguments: (1) The Registry key handle or one of
the predefined constants, (2) the string containing the subkey to
open, (3) the 'res' (which I don't know what is :), and the 'sam',
which is the access mask (KEY_SET_VALUE, in this case). You are only
passing three arguments, so the access mask is going to the 'res'
argument instead of the 'sam' argument. Pass instead 0 as the res:


handle = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,
'Control Panel\Desktop',
0,
_winreg.KEY_SET_VALUE)

Regards,
Sebastian
 
D

drobinow

Hi everybody. I'm trying to write a script that'll change desktop
wallpaper every time its run. Heres what I've gotten so far:

#random wallpaper changer!
import _winreg
from os import walk
from os.path import exists
from random import randint

#first grab a registry handle.
handle = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Control Panel
\Desktop',_winreg.KEY_SET_VALUE)
You're missing the third parameter to OpenKey. Try:
handle = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,
'Control Panel\Desktop',_0, winreg.KEY_SET_VALUE)
 
T

teh_sAbEr

Great! It works properly now but I have one more question, would
anyone know how to get the changes to take effect immediately? Like
some sort of Python way to force the desktop to reload? AFAIK the only
way that'll happen is if I use the Display Properties dialog box. The
Registry value is changed properly, its just I don't think the changes
will take effect until I restart.
 
S

s0suk3

Great! It works properly now but I have one more question, would
anyone know how to get the changes to take effect immediately? Like
some sort of Python way to force the desktop to reload? AFAIK the only
way that'll happen is if I use the Display Properties dialog box. The
Registry value is changed properly, its just I don't think the changes
will take effect until I restart.

I haven't tried this, but I'd suggest to look at the Windows API.
There are several Python interface modules such as win32api.
 
T

Tim Golden

teh_sAbEr said:
Great! It works properly now but I have one more question, would
anyone know how to get the changes to take effect immediately? Like
some sort of Python way to force the desktop to reload? AFAIK the only
way that'll happen is if I use the Display Properties dialog box. The
Registry value is changed properly, its just I don't think the changes
will take effect until I restart.

Sorry; I missed this one earlier. One way to change the wallpaper
without delving into the registry is the SystemParametersInfo function.
Try the following:

<code>
import win32con
import win32gui

WALLPAPER_BMP = "c:/temp/t.bmp"
win32gui.SystemParametersInfo (
win32con.SPI_SETDESKWALLPAPER,
WALLPAPER_BMP,
win32con.SPIF_SENDCHANGE
)

</code>

The only thing I'm not sure about is whether it will survive
a logoff. If it doesn't I suppose you can always combine it
with the registry technique to make it permanent.

TJG
 
T

Tim Roberts

teh_sAbEr said:
Hi everybody. I'm trying to write a script that'll change desktop
wallpaper every time its run. Heres what I've gotten so far:

#random wallpaper changer!
import _winreg
from os import walk
from os.path import exists
from random import randint

#first grab a registry handle.
handle = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER,'Control Panel
\Desktop',_winreg.KEY_SET_VALUE)

def GenerateListOfWallpapers():
targetDir = 'C:\Documents and Settings\Enrico Jr\My Documents\Jr
\'s Wallpapers'

You are fortunate that your name is not "Tim" or "Ian" or "Nathan", because
this would not have worked as you have written it.

You either need to double the backslashes:
... 'C:\\Documents and Settings\\Enrico...'
or use forward slashes:
... 'C:/Documents and Settings/Enrico...'
or use the "r" modifier:
... r'C:\Documents and Settings\Enrico...'

However, as a general practice, it's probably better to get the special
directories from the environment:
targetDir = os.environ['USERPROFILE'] + '\\My Documents\\Jr\'s
Wallpapers'

Remember that it's not called "Documents and Settings" on Vista...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top