From: teh_sAbEr on
Hi,
I'm trying to write a script that'll change the wallpaper to a
randomly selected one each time its run. I've got it set up to point
to a folder and randomly select a wallpaper from there, and then
change the value of HKEY_CURRENT_USER\Control Panel\Desktop
\ConvertedWallpaper to the path to the newly selected wall. The
problem is it doesn't work and i'm told that the answer lies in the
Win32 API. I've downloaded the Python for Windows extensions but I
can't seem to find anything that'll work. Could someone give me an
idea as to where to look and what to look for?
This is the program in its current form, using Python 2.5

#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 = r'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."
From: Kellie Fitton on
On Jul 3, 3:00 pm, teh_sAbEr <teh.sa...(a)gmail.com> wrote:
> Hi,
> I'm trying to write a script that'll change the wallpaper to a
> randomly selected one each time its run. I've got it set up to point
> to a folder and randomly select a wallpaper from there, and then
> change the value of HKEY_CURRENT_USER\Control Panel\Desktop
> \ConvertedWallpaper to the path to the newly selected wall. The
> problem is it doesn't work and i'm told that the answer lies in the
> Win32 API. I've downloaded the Python for Windows extensions but I
> can't seem to find anything that'll work. Could someone give me an
> idea as to where to look and what to look for?
> This is the program in its current form, using Python 2.5
>
> #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 = r'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."



Hi,

You can use the following API to set the desktop's wallpaper:

SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
MyWallPaperFile,
SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);

http://msdn.microsoft.com/en-us/library/ms724947(VS.85).aspx

Kellie.

From: Dean Earley on
Kellie Fitton wrote:
> On Jul 3, 3:00 pm, teh_sAbEr <teh.sa...(a)gmail.com> wrote:
>> Hi,
>> I'm trying to write a script that'll change the wallpaper to a
>> randomly selected one each time its run. I've got it set up to point
>> to a folder and randomly select a wallpaper from there, and then
>> change the value of HKEY_CURRENT_USER\Control Panel\Desktop
>> \ConvertedWallpaper to the path to the newly selected wall. The
>> problem is it doesn't work and i'm told that the answer lies in the
>> Win32 API.
>
> You can use the following API to set the desktop's wallpaper:
>
> SystemParametersInfo(SPI_SETDESKWALLPAPER,
> 0,
> MyWallPaperFile,
> SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
>
> http://msdn.microsoft.com/en-us/library/ms724947(VS.85).aspx

Further to this:
http://blogs.msdn.com/oldnewthing/archive/2005/03/09/390706.aspx

--
Dean Earley (dean.earley(a)icode.co.uk)
i-Catcher Development Team

iCode Systems
From: teh_sAbEr on
Ok, I've managed to get the script working, but after a few tries
explorer seems to have crashed (the taskbar disappears and reappears),
the wallpaper no longer shows and no amount of refreshing seems to
make it work again. I think I might've broken something.

#random wallpaper changer!
import win32gui #use this to change the wallpaper.
import os
import os.path
import random
import Image

def GenerateListOfWallpapers():
targetDir = 'C:\\Documents and Settings\\Enrico Jr\\My Documents\
\Jr\'s Wallpapers'
fileNames = []
filePaths = []
if os.path.exists(targetDir):
#proceed to make the list of files
print "Target directory found. Walking..."
for x,y,z in os.walk(targetDir):
for name in z:
fileNames.append(name)
for item in fileNames:
filePaths.append(targetDir + '\\' + item)
print "Walk complete! Moving on..."
print type(filePaths)
return filePaths #this should be a list.
else:
print "Error! Target directory not found. Check settings and
try again."
exit()

def RandomlySelectWallpaper(filePaths):
index = random.randint(0,len(filePaths)-1)
print "Selecting wallpaper randomly."
RandomlySelectedWallpaper = filePaths[index]
print "Selection made. Completing function cycle..."
print RandomlySelectedWallpaper
return RandomlySelectedWallpaper #it should be a string...

def ChangeDesktopWallpaper(RandomlySelectedWallpaper):
SPI_SETDESKWALLPAPER = 20
SPI_GETDESKWALLPAPER = 115
root,extension = os.path.splitext(RandomlySelectedWallpaper)
newFileName = root + '.bmp'
print "New format determined....Attempting to save..."
try:
Image.open(RandomlySelectedWallpaper).save(newFileName)
print "File saved!"
except IOError:
print "Error while converting, please check filepath and
permissions."
print "Program will terminate now."
exit()
print "Now setting desktop wallpaper."
win32gui.SystemParametersInfo(SPI_SETDESKWALLPAPER,newFileName,
1+2)
print "Wallpaper set! Enjoy."

def Main():
#woot.
print "Program starting!"
print "GENERATING LIST OF WALLS."
listOfWallpaperPaths = GenerateListOfWallpapers()
print type(listOfWallpaperPaths)
print "RANDOMLY SELECTING WALL."
RandomlySelectedWall =
RandomlySelectWallpaper(listOfWallpaperPaths)
print "CHANGING SETTINGS TO APPLY NEWLY SELECTED WALLPAPER."
ChangeDesktopWallpaper(RandomlySelectedWall)
print "PROGRAM COMPLETE. TERMINATE."

Main()
From: Jerry Coffin on
In article <89b71a91-b93e-4c9a-be6b-6467d0012e71
@w34g2000prm.googlegroups.com>, teh.saber(a)gmail.com says...
> Ok, I've managed to get the script working, but after a few tries
> explorer seems to have crashed (the taskbar disappears and reappears),
> the wallpaper no longer shows and no amount of refreshing seems to
> make it work again. I think I might've broken something.

I don't use Python enough to notice, but quickly checking with a version
in C++, things seem to work all right.

#pragma warning(disable:4786)
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <time.h>
#include "dir_iterator.h"

int main(int argc, char **argv) {
std::vector<std::string> filenames;

try {
dir_iterator d("C:\\Pics\\Christmas 03\\*.bmp");

std::copy(d, dir_iterator(), std::back_inserter(filenames));

srand(time(NULL));

std::string path = filenames[rand() % filenames.size()];
std::cout << path << "\n";
SystemParametersInfo(SPI_SETDESKWALLPAPER,
0,
(void*)path.c_str(),
SPIF_SENDCHANGE);
}
catch (exception const &i) {
std::cerr << i.what() << "\n";
return EXIT_FAILURE;
}
return 0;
}

Oh, looking at it, I suppose you need a copy of my dir_iterator.h to
make that work:

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <string>
#include <iterator>
#include <exception>

#ifndef DIR_ITERATOR_H_INC
#define DIR_ITERATOR_H_INC

class dir_iterator

#if (!defined(_MSC_VER)) || (_MSC_VER > 1200)
: public std::iterator<std::input_iterator_tag,
std::string,
int,
std::string *,
std::string &>
#endif
{
HANDLE it;
std::string mask;
std::string path;
WIN32_FIND_DATA data;
bool done;
DWORD require;
DWORD prohibit;
public:
std::string operator*() {
return path + data.cFileName;
}

dir_iterator(std::string const &s,
DWORD must = 0,
DWORD cant = FILE_ATTRIBUTE_DIRECTORY)
: mask(s),
require(must),
prohibit(cant & ~must),
done(false)
{
int pos;
if (std::string::npos != (pos=mask.find_last_of("\\/")))
path = std::string(mask, 0, pos+1);

it = FindFirstFile(mask.c_str(), &data);
if (it == INVALID_HANDLE_VALUE)
throw std::invalid_argument("Directory Inaccessible");

while (!(((data.dwFileAttributes & require) == require) &&
((data.dwFileAttributes & prohibit) == 0)))
{
if (done = (FindNextFile(it, &data)==0))
break;
}
}

dir_iterator() : done(true) {}

dir_iterator &operator++() {
do {
if (done = (FindNextFile(it, &data)==0))
break;
} while (!(((data.dwFileAttributes & require) == require) &&
(data.dwFileAttributes & prohibit) == 0));

return *this;
}

bool operator!=(dir_iterator const &other) {
return done != other.done;
}
bool operator==(dir_iterator const &other) {
return done == other.done;
}

~dir_iterator() {
FindClose(it);
}
};

#endif

--
Later,
Jerry.

The universe is a figment of its own imagination.