The wrong approach to things

teh_sAbEr teh.saber at gmail.com
Sat Aug 9 22:20:02 EDT 2008


Ok, so this isn't necessarily a programming issue, but anyways. I've
managed to write that random wallpaper changer i've been wanting to
make, but i've run into a little problem. According to the MS
Knowledge Base, SystemParametersInfo() can't take a .jpg file as an
argument when changing the wallpaper (it doesn't work, i've tried it),
only .bmps so i'm stuck converting one of my many wallpapers from .jpg
to .bmp, passing that to SystemParametersInfo(), and deleting the
previous .bmp so that i don't have any duplicates. The problem with
this is that other members of my family use this computer too, and
they sometimes set the desktop wallpaper to some other, usually funny
picture they've got stored on the HD, and when I run this little
script it'll delete whatever the current wallpaper is, regardless of
whether its one of mine or not. Any suggestions on how to work around
this problem?

#random wallpaper changer!
import win32gui #use this to change the wallpaper.
import os
import os.path
import random
import Image
SPI_SETDESKWALLPAPER = 20  #It took me WAY too long to find them.
SPI_GETDESKWALLPAPER = 115 #I should keep these handy.



def RandomlySelectWallpaper(filePaths):
    CurrentWallpaper =
win32gui.SystemParametersInfo(SPI_GETDESKWALLPAPER)
    while True:
        index = random.randint(0,len(filePaths)-1)
        RandomlySelectedWallpaper = filePaths[index]
        if RandomlySelectedWallpaper <> CurrentWallpaper:
            break
    print RandomlySelectedWallpaper
    return RandomlySelectedWallpaper #it should be a string...

def ChangeDesktopWallpaper(RandomlySelectedWallpaper):
    #so the RIGHT way to do this would be to use
    #win32gui.SystemParametersInfo() to change the wallpaper.
    #1) we convert the image to .bmp.
    #Delete the old wallpaper, actual delete occurs after new
wallpaper has been set.
    pathToCurrentWall =
win32gui.SystemParametersInfo(SPI_GETDESKWALLPAPER)
    root,extension = os.path.splitext(RandomlySelectedWallpaper)
    newFileName = root + '.bmp'
    print "Wallpaper to delete: ", pathToCurrentWall
    try:
        #every so often something goes wrong at this stage in the
script
        #and I can't figure out what. Something raises an IOError.
        Image.open(RandomlySelectedWallpaper).save(newFileName)
        print "File saved!"
    except IOError:
        print "Error while converting, please check filepath and
permissions."
        print "Program will restart in an attempt to generate a
correct wallpaper."
        Main()
    win32gui.SystemParametersInfo(SPI_SETDESKWALLPAPER,newFileName,
1+2)
    print "Removing: ", pathToCurrentWall
    os.remove(pathToCurrentWall)

def Main():
    #woot.
    listOfWallpaperPaths = GenerateListOfWallpapers()
    RandomlySelectedWall =
RandomlySelectWallpaper(listOfWallpaperPaths)
    ChangeDesktopWallpaper(RandomlySelectedWall)

Main()



More information about the Python-list mailing list