Change wallpaper randomly

Get help with using AutoHotkey and its commands and hotkeys
kexmonser
Posts: 1
Joined: 03 Jan 2019, 18:39

Change wallpaper randomly

03 Jan 2019, 18:57

Hello.

I've cannibalized some parts of the script found in https://autohotkey.com/board/topic/4391 ... er-on-cue/. The reason that I've split it up is that it wasn't working as inteded like it is in the thread, and I'm trying to implement my own stuff. When the hotkey is pressed, the tray message displays the correct file name. However the wallpaper won't update until i log out and back in or restart.

It looks like this:

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.


ArrayCount=0
Loop, C:\Users\Kexmonster\Desktop\E\Pictures\*.jpg ; wherever you store your wallpapers
{
ArrayCount+=1
Array%ArrayCount% := A_LoopFileName
}

Menu, TRAY, Add, Refresh Wallpapers, RefreshWallpapers ; Manual refresh
return ; Ends the auto-execute section

RefreshWallpapers: ; does the same job as the auto-execute
ArrayCount=0
Loop, C:\Users\Kexmonster\Desktop\E\Pictures\*.jpg ; same place
{
ArrayCount+=1
Array%ArrayCount% := A_LoopFileName
}
TrayTip,, Wallpaper list refreshed!,,1
SPI_SETDESKWALLPAPER := 20
SPIF_SENDWININICHANGE := 2 
return

!5::
Random, filenum, 1, %ArrayCount%
filecalled := Array%filenum%
TrayTip,, Setting "%filecalled%" as your wallpaper,,1
FileCopy, C:\Users\Kexmonster\Desktop\E\Pictures\%filecalled%, %A_MyDocuments%\currentwallpaper.jpg, 1
DllCall("SystemParametersInfo", UInt, 0x14, UInt, 0, Str, A_MyDocuments . "\currentwallpaper.jpg", UInt, 2)
RegWrite, REG_SZ, HKEY_CURRENT_USER, Control Panel\Desktop, WallPaper, C:\Users\Kexmonster\Desktop\E\Pictures\%filecalled%
Sleep, 500 ; can't change the wallpaper too quickly in succession.
return
However, just setting the wallpaper like in the script below will instantly switch the wallpapers.

Code: Select all

!4::
SetWallpaper("C:\Users\Kexmonster\Desktop\E\Pictures\WP1.jpg")
Return


SetWallpaper(BMPpath)
{
  SPI_SETDESKWALLPAPER := 20
  SPIF_SENDWININICHANGE := 2  
Return DllCall("SystemParametersInfo", UINT, SPI_SETDESKWALLPAPER, UINT, uiParam, STR, BMPpath, UINT, SPIF_SENDWININICHANGE)
}
I'm unable to grasp exactly how the topmost script gets a random filename and what variable or whatever it's stored in. Any help is greatly appreciated!
User avatar
Delta Pythagorean
Posts: 481
Joined: 13 Feb 2017, 13:44
GitHub: DelPyth
Location: Somewhere in the US

Re: Change wallpaper randomly

04 Jan 2019, 14:44

Code: Select all

#NoEnv
#SingleInstance, Force
#KeyHistory, 0
#MaxThreadsPerHotkey, 1
#Persistent
ListLines, Off
SendMode, Input
SetBatchLines, -1
SetWinDelay, -1
SetMouseDelay, -1
SetKeyDelay, -1, -1
SetTitleMatchMode, 2
DetectHiddenWindows, On
SetWorkingDir, % A_ScriptDir

Wallpapers := [] ; Forces the list to be an array.

WallpaperLoc := A_Desktop "\E\Pictures\" ; The location of the wallpapers

Loop, % WallpaperLoc . "*.jpg" ; wherever you store your wallpapers
{
	Wallpapers[A_Index] := A_LoopFileName
}

Menu, Tray, NoStandard ; Forces the Tray menu to be blank before adding anything.
Menu, Tray, Add, Refresh Wallpapers, RefreshWallpapers ; Manual refresh for wallpapers
Menu, Tray, Add
Menu, Tray, Add, Exit Script, Exit ; Allow exiting the script with a simple command.
Return ; Ends the auto-execute section

RefreshWallpapers: ; does the same job as the auto-execute
	Wallpapers := []

	Loop, % WallpaperLoc . "*.jpg" ; wherever you store your wallpapers
	{
		Wallpapers[A_Index] := A_LoopFileName
	}
	TrayTip, Wallpaper, Wallpaper list refreshed!,,1
	Return

Exit:
	ExitApp, 0 ; Exit the script with a code of 0

!5:: ; Sets the wallpaper to a random image in the 'Pictures' folder.
	TrayTip, Wallpaper, % Format("Setting ""{1}"" as your wallpaper", File := Wallpapers[Random(1, Wallpapers.MaxIndex())]),, 1
	FileCopy, % % WallpaperLoc . File, % A_MyDocuments . "\currentwallpaper.jpg", 1
	SetWallpaper(A_MyDocuments . "\currentwallpaper.jpg")
	Return

!4:: ; Sets the wallpaper to 'WM1.jpg'
	SetWallpaper(WallpaperLoc . "WP1.jpg")
	Return

; Sets the wallpaper to a selected file of your choosing.
SetWallpaper(WallpaperFile) {
	DllCall("SystemParametersInfo", "Uint", 20, "Uint", 0, "Str", WallpaperFile, "Uint", 2)
}

; Just a minor function to help with minimizing code. Nothing to worry about for your sake.
Random(Min, Max) {
	Random, Out, % Min, % Max
	Return, Out
}
Now, I'm not an expert at changing wallpapers with AHK. But this was quite simple compared to other scripts.
Hope this helps you.
And good luck in the future.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], DavidBevi, Henrik_Oe, hoppfrosch and 208 guests