Skip to content

Instantly share code, notes, and snippets.

@zlrc
Last active April 22, 2024 02:06
Show Gist options
  • Save zlrc/2e452daac77ff87d321198e0d8414cbf to your computer and use it in GitHub Desktop.
Save zlrc/2e452daac77ff87d321198e0d8414cbf to your computer and use it in GitHub Desktop.
An AutoHotkey v2 script for Windows 10 that advances a wallpaper slideshow to the next background. This automates right-clicking the desktop and selecting "Next desktop background" from the context menu.
; This script advances the wallpaper slideshow to the next image. Requires AutoHotkey v2.
; Author: Mike "ZiRC" K. (https://github.com/zlrc)
; License: CC0 (https://creativecommons.org/publicdomain/zero/1.0/)
;
; Relevant AutoHotkey Documentation:
; - ComObject: https://www.autohotkey.com/docs/v2/lib/ComObject.htm
; - ComValue: https://www.autohotkey.com/docs/v2/lib/ComValue.htm
; - ComCall: https://www.autohotkey.com/docs/v2/lib/ComCall.htm
; - DllCall: https://www.autohotkey.com/docs/v2/lib/DllCall.htm
Sleep 2500
; 1 in `numImages` chance the rest of this script doesn't get executed.
numImages := 6
if Random(1, numImages) == 1
ExitApp
; The code below is an updated version of a snippet written for AHKv1, documented here: https://www.autohotkey.com/boards/viewtopic.php?style=7&t=34436#p242537
try if ((pDesktopWallpaper := ComObject("{C2CF3110-460E-4fc1-B9D0-8A1C0C9CC4BD}", "{B92B56A9-8B55-4E14-9A89-0199BBB6F93B}"))) {
; HRESULT AdvanceSlideshow(monitorID, direction) -
; https://learn.microsoft.com/en-us/windows/win32/api/shobjidl_core/nf-shobjidl_core-idesktopwallpaper-advanceslideshow
; `monitorID` is a pointer to the target monitor's ID. Setting to `NULL` targets the monitor that is scheduled to change next.
; (below, passing `0` as the pointer value is the same as passing `NULL` to the function.)
; `direction` indicates which direction the slideshow advances (0 is forward, 1 is backward).
ComCall(16, pDesktopWallpaper, "Ptr", 0, "UInt", 0) ; IDesktopWallpaper::AdvanceSlideshow(NULL, 0)
; `16` above refers to index 16 of the virtual method table for IDesktopWallpaper (i.e. the 17th method declared for this interface).
; - A reference for IDesktopWallpaper's virtual table can be found here: https://github.com/retep998/winapi-rs/blob/5b1829956ef645f3c2f8236ba18bb198ca4c2468/src/um/shobjidl_core.rs#L292
; - Indices start at 0, so the "17th" method is at index 16.
; - IDesktopWallpaper inherits IUnknown, which declares 3 methods that occupy the first 3 indices (0, 1, and 2).
; - This means that, when counting the indices for IDesktopWallpaper methods, start at index 3 (which refers to `SetWallpaper`).
} catch {
MsgBox "Error: " . A_LastError
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment