Jump to content

AutoIt script to change desktop


Recommended Posts

Hey guys. I LOVE THE FORUM!

I couldn't find anywhere else to put this, so I decided to post it here.

This AutoIt script will grab a random .bmp from a given directory and set it as the desktop. Change $favDir to your own directory from which to grab. Irfanview can be used to convert .jpgs to .bmps. Windows actually only can load .bmps to the desktop and usually does the conversion on-the-fly when you set a .jpg to the desktop. Hope it's appreciated :) Personally, I have a startup item linking to the script so it changes when Windows starts.

PLEASE NOTE: I have used stolen code in this script. I don't know where it came from. Some of it is my work, some of it is not.

Dim $favDir = 'C:\Downloads\Wallpapers\favorites\'

Dim $cCounter = 0
Dim $FindFile = FileFindFirstFile($favDir & '*.bmp')
While 1
FileFindNextFile($FindFile)
If @error Then ExitLoop
$cCounter = $cCounter + 1
WEnd
FileClose($FindFile)

$FindFile = FileFindFirstFile($favDir & '*.bmp')
Dim $RandomNum = Random(1, $cCounter, 1)

Dim $curFile
For $i = 1 To $RandomNum
$curFile = FileFindNextFile($FindFile)
If $RandomNum = $i Then
ChangeDesktopWallpaper($favDir & $curFile)
ExitLoop
EndIf
Next

FileClose($FindFile)

Exit

Func ChangeDesktopWallpaper($bmp)
;===============================================================================
; Usage: _ChangeDesktopWallPaper(@WindowsDir & '\' & 'zapotec.bmp')
; Parameter(s): $bmp - Full Path to BitMap File (*.bmp)
; Requirement(s): None.
; Return Value(s): On Success - Returns 0
; On Failure - -1
;===============================================================================

If Not FileExists($bmp) Then Return -1
;The $SPI* values could be defined elsewhere via #include - if you conflict,
; remove these, or add if Not IsDeclared "SPI_SETDESKWALLPAPER" Logic
Local $SPI_SETDESKWALLPAPER = 20
Local $SPIF_UPDATEINIFILE = 1
Local $SPIF_SENDCHANGE = 2
Local $REG_DESKTOP= "HKEY_CURRENT_USER\Control Panel\Desktop"

;Don't tile - just center
RegWrite($REG_DESKTOP, "TileWallPaper", "REG_SZ", 0)
RegWrite($REG_DESKTOP, "WallpaperStyle", "REG_SZ", 0)
RegWrite($REG_DESKTOP, "Wallpaper", "REG_SZ", $bmp)

DllCall("user32.dll", "int", "SystemParametersInfo", _
"int", $SPI_SETDESKWALLPAPER, _
"int", 0, _
"str", $bmp, _
"int", BitOR($SPIF_UPDATEINIFILE, $SPIF_SENDCHANGE))

Return 0
EndFunc;==>_ChangeDestopWallpaper

Change_Desktop.au3

Edited by aboutblank
Link to comment
Share on other sites


  • 4 weeks later...
  • 3 months later...
  • 1 year later...

Sorry to bring up such an old thread, but I need help getting this piece of code to work.

My OS is XP PS3 with the latest AutoIt compiler and SciTE

I run the code, and the wallpaper doesn't change, so I right click on my desktop and go to Properties. I noticed that the preview image on the properties window now has changed to one of the specified wallpaper, I click some other wallpaper and reselect the previously selected wallpaper (which is one of the wallaper that the autoit script selected) and I hit Ok, the wallpaper changed to one of the correct wallpaper.

So I think the wallpaper just needs to get refreshed.. but I don't know how I would do that.. Any Help? I just want a script/program/command to be able to select a random wallpaper from a directory and set it as the wallpaper when I want to.

Link to comment
Share on other sites

Sorry to bring up such an old thread, but I need help getting this piece of code to work.

My OS is XP PS3 with the latest AutoIt compiler and SciTE

I run the code, and the wallpaper doesn't change, so I right click on my desktop and go to Properties. I noticed that the preview image on the properties window now has changed to one of the specified wallpaper, I click some other wallpaper and reselect the previously selected wallpaper (which is one of the wallaper that the autoit script selected) and I hit Ok, the wallpaper changed to one of the correct wallpaper.

So I think the wallpaper just needs to get refreshed.. but I don't know how I would do that.. Any Help? I just want a script/program/command to be able to select a random wallpaper from a directory and set it as the wallpaper when I want to.

Kill the explorer.exe, and rerun it. Maybe this will fix it?

Link to comment
Share on other sites

  • 4 months later...
hey... this is freakin awesome. but how do you get it to be stretched, not centered. (i'm new here.)

Change

RegWrite($REG_DESKTOP, "WallpaperStyle", "REG_SZ", 0)

to

RegWrite($REG_DESKTOP, "WallpaperStyle", "REG_SZ", 2)

Edited by anandchakru2
Link to comment
Share on other sites

  • 7 months later...

ChangeDesktopWallpaper.au3

I have attempt to modify the script to make it work with all images type (.png, .bmp, jpg etc). Since it need to be converted to bmp, I use IrfanView main executable to do this task. Hence for the script to work, it requires you to put i_view32.exe in either the same folder as the script or C:\Windows. (I prefer C:\Windows)

Image location is C:\WINDOWS\Web\Wallpaper

I have included the modified script (as in the above) for you to edit as well as the compiled script (as in below) so that non-autoit user can also use it.

Tested in XP only. Enjoy :)

ChangeDesktopWallpaper.exe

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...