Skip to content

Instantly share code, notes, and snippets.

@Generhr
Last active January 18, 2023 10:26
Show Gist options
  • Save Generhr/3ccd99f0d099fed4319bd4cd78847935 to your computer and use it in GitHub Desktop.
Save Generhr/3ccd99f0d099fed4319bd4cd78847935 to your computer and use it in GitHub Desktop.
;* PostMessage(msg, (wParam), (lParam), (winTitle), (excludeTitle), (detectHiddenWindows))
PostMessage(msg, wParam := 0, lParam := 0, winTitle := "", excludeTitle := "", detectHiddenWindows := "") {
if (!(detectHiddenWindows == "") && !(detectHiddenWindows == (detect := A_DetectHiddenWindows))) {
DetectHiddenWindows, % detectHiddenWindows ;~ No error handling.
}
PostMessage, msg, wParam, lParam, , % winTitle, , % excludeTitle
if (detect) {
DetectHiddenWindows, % detect
}
return (ErrorLevel) ;* ErrorLevel is set to 1 if there was a problem such as the target window or control not existing. Otherwise, it is set to 0.
}
Class Spotify {
Static Handle := 0
Pause() {
PostMessage(0x319, , 0xD0000, this.GetWindow(1), , "On") ;? 0x319 = WM_APPCOMMAND
}
PlayPause() {
PostMessage(0x319, , 0xE0000, this.GetWindow(1), , "On")
}
Play() {
Local
detect := A_DetectHiddenWindows
DetectHiddenWindows, On
window := this.GetWindow(1)
PostMessage, 0x319, , 0xD0000, , % window
PostMessage, 0x319, , 0xE0000, , % window
DetectHiddenWindows, % detect
}
Prev() {
PostMessage(0x319, , 0xC0000, this.GetWindow(1), , "On")
}
Next() {
PostMessage(0x319, , 0xB0000, this.GetWindow(1), , "On")
}
GetWindow(prefix := true) {
Local
detect := A_DetectHiddenWindows
DetectHiddenWindows, On
if (WinExist("ahk_exe Spotify.exe")) {
if (this.Handle && DllCall("IsWindow", "Ptr", this.Handle, "UInt")) {
window := (prefix) ? ("ahk_ID" . this.Handle) : (this.Handle)
}
else {
for i, hWnd in WinGet("List", "ahk_exe Spotify.exe") {
if (WinGet("Class", "ahk_ID" . hWnd) == "Chrome_WidgetWin_0" && WinGet("Title", "ahk_ID" . hWnd) ~= "^(Spotify.*|.* - .*)$") {
this.Handle := hWnd
, window := (prefix) ? ("ahk_ID" . hWnd) : (hWnd)
break
}
}
}
}
DetectHiddenWindows, % detect ;* Avoid leaving `DetectHiddenWindows` on for the calling thread.
return (window)
}
}
@Arslan-TR
Copy link

which version is the AHK?

@Generhr
Copy link
Author

Generhr commented Jan 18, 2023

which version is the AHK?

ErrorLevel was removed in v2, this is old v1 code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment