Working with the Windows Registry

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • teh_sAbEr

    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_C URRENT_USER,'Co ntrol Panel
    \Desktop',_winr eg.KEY_SET_VALU E)

    def GenerateListOfW allpapers():
    targetDir = 'C:\Documents and Settings\Enrico Jr\My Documents\Jr
    \'s Wallpapers'
    fileNames = []
    filePaths = []
    if exists(targetDi r):
    #proceed to make the list of files
    for x,y,z in walk(targetDir) :
    for name in z:
    fileNames.appen d(name)
    for item in fileNames:
    filePaths.appen d(targetDir + '\\' + item)
    return filePaths

    def RandomlySelectW allpaper(filePa ths):
    index = randint(0,len(f ilePaths)-1)
    RandomlySelecte dWallpaper = filePaths[index]
    return RandomlySelecte dWallpaper #it should be a string...

    #now to edit the wallpaper registry key
    newWallpaper = RandomlySelectW allpaper(Genera teListOfWallpap ers())
    print "Registry Handle Created."
    print "Random wallpaper selected."
    _winreg.SetValu eEx(handle,'Con vertedWallpaper ',
    0,_winreg.REG_S Z,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.SetValu eEx(), 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?
  • s0suk3@gmail.com

    #2
    Re: Working with the Windows Registry

    On Jun 25, 9:48 pm, teh_sAbEr <teh.sa...@gmai l.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_C URRENT_USER,'Co ntrol Panel
    \Desktop',_winr eg.KEY_SET_VALU E)
    >
    def GenerateListOfW allpapers():
    targetDir = 'C:\Documents and Settings\Enrico Jr\My Documents\Jr
    \'s Wallpapers'
    fileNames = []
    filePaths = []
    if exists(targetDi r):
    #proceed to make the list of files
    for x,y,z in walk(targetDir) :
    for name in z:
    fileNames.appen d(name)
    for item in fileNames:
    filePaths.appen d(targetDir + '\\' + item)
    return filePaths
    >
    def RandomlySelectW allpaper(filePa ths):
    index = randint(0,len(f ilePaths)-1)
    RandomlySelecte dWallpaper = filePaths[index]
    return RandomlySelecte dWallpaper #it should be a string...
    >
    #now to edit the wallpaper registry key
    newWallpaper = RandomlySelectW allpaper(Genera teListOfWallpap ers())
    print "Registry Handle Created."
    print "Random wallpaper selected."
    _winreg.SetValu eEx(handle,'Con vertedWallpaper ',
    0,_winreg.REG_S Z,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.SetValu eEx(), 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_C URRENT_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_C URRENT_USER,
    'Control Panel\Desktop',
    0,
    _winreg.KEY_SET _VALUE)

    Regards,
    Sebastian

    Comment

    • Larry Bates

      #3
      Re: Working with the Windows Registry

      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_C URRENT_USER,'Co ntrol Panel
      \Desktop',_winr eg.KEY_SET_VALU E)
      >
      def GenerateListOfW allpapers():
      targetDir = 'C:\Documents and Settings\Enrico Jr\My Documents\Jr
      \'s Wallpapers'
      fileNames = []
      filePaths = []
      if exists(targetDi r):
      #proceed to make the list of files
      for x,y,z in walk(targetDir) :
      for name in z:
      fileNames.appen d(name)
      for item in fileNames:
      filePaths.appen d(targetDir + '\\' + item)
      return filePaths
      >
      def RandomlySelectW allpaper(filePa ths):
      index = randint(0,len(f ilePaths)-1)
      RandomlySelecte dWallpaper = filePaths[index]
      return RandomlySelecte dWallpaper #it should be a string...
      >
      #now to edit the wallpaper registry key
      newWallpaper = RandomlySelectW allpaper(Genera teListOfWallpap ers())
      print "Registry Handle Created."
      print "Random wallpaper selected."
      _winreg.SetValu eEx(handle,'Con vertedWallpaper ',
      0,_winreg.REG_S Z,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.SetValu eEx(), 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_CU RRENT_USER
      key = r'Control Panel\Desktop'
      handle = _winreg.OpenKey (reg, key, 0, _winreg.KEY_WRI TE)


      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

      Comment

      • drobinow@gmail.com

        #4
        Re: Working with the Windows Registry

        On Jun 25, 10:48 pm, teh_sAbEr <teh.sa...@gmai l.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_C URRENT_USER,'Co ntrol Panel
        \Desktop',_winr eg.KEY_SET_VALU E)
        >
        You're missing the third parameter to OpenKey. Try:
        handle = _winreg.OpenKey (_winreg.HKEY_C URRENT_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.SetValu eEx(), 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?

        Comment

        • teh_sAbEr

          #5
          Re: Working with the Windows Registry

          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.

          Comment

          • Tim Roberts

            #6
            Re: Working with the Windows Registry

            teh_sAbEr <teh.saber@gmai l.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_C URRENT_USER,'Co ntrol Panel
            >\Desktop',_win reg.KEY_SET_VAL UE)
            >
            >def GenerateListOfW allpapers():
            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\\Enric o...'
            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, timr@probo.com
            Providenza & Boekelheide, Inc.

            Comment

            Working...