473,385 Members | 1,732 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Working with the Windows Registry

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?
Jun 27 '08 #1
5 2492
On Jun 25, 9:48 pm, teh_sAbEr <teh.sa...@gmail.comwrote:
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
Jun 27 '08 #2
teh_sAbEr wrote:
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?
Common error. You have to open the key so that it can be written as follows:

reg = _winreg.HKEY_CURRENT_USER
key = r'Control Panel\Desktop'
handle = _winreg.OpenKey(reg, key, 0, _winreg.KEY_WRITE)
Note: be careful with backslashes (\) in non-raw strings they will be
interpreted as escaped sequences. You were lucky because \D doesn't
represent anything escaped. You should either use r's\gg\gg' or use double
backslashes 's\\gg\\gg'.

-Larry
Jun 27 '08 #3
On Jun 25, 10:48*pm, teh_sAbEr <teh.sa...@gmail.comwrote:
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)
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?
Jun 27 '08 #4
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.
Jun 27 '08 #5
teh_sAbEr <te*******@gmail.comwrote:
>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...
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 28 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Paul Rubin | last post by:
As what must be penance for something or other, I'm needing to release a Python app for use under Windows XP. Please be gentle with me since I'm a Un*x weenie and the only thing I've had much...
0
by: glenn | last post by:
I am trying to write to the registry and the objects that are suppose to be handling this function are not working. Here is what is happening. I am issuing the following commands: RegistryKey...
4
by: Richard | last post by:
All, I have coded an Outlook automation Addin in C# and .NET. I created the project using the Extensibility wizard. The Addin installs and runs Ok on my machine. However I am unable to...
5
by: Dhilip Kumar | last post by:
Hi All, I'm writing a Windows Service app using C#. I need to read some configuration settings before the service starts up. These settings will be used by the service in its operation. ...
3
by: vighnesh | last post by:
Hi All I have to develop an application, which has to scan the windows registry and fix the bugs if any. Please let me know whether it is possible to develop that application in VB.NET/C#.NET?...
3
by: Benny Raymond | last post by:
I get the following error message when trying to use the Excel Interop on my wife's machine however I don't get it on my own - we have the same version of Office installed - what could be the...
4
by: JohnB111 | last post by:
Hi Environment: Windows XPPRO SP2 IIS 5.1 AVG Anti Virus (Not script Blocking) The following code used to work perfectly on my local development machine and still does on my web server.
1
by: chitra g | last post by:
Hi, I tried all the options below but did not work. Your suggestions please.
0
AmberJain
by: AmberJain | last post by:
Windows Autorun FAQs: Description NOTE- If you are unfamiliar with the concept of autoruns, then read "Windows Autorun FAQs: Overview". Que-1: How can I safely remove or edit the autorun...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.