Skip to content

Instantly share code, notes, and snippets.

@anonymous1184
Last active January 3, 2025 18:24
Show Gist options
  • Save anonymous1184/64fe98f2d25c8faaeea11ebae7bf1014 to your computer and use it in GitHub Desktop.
Save anonymous1184/64fe98f2d25c8faaeea11ebae7bf1014 to your computer and use it in GitHub Desktop.
Spotify Keys (APP)
#Requires AutoHotkey v2.0
/*
If placing below any hotkeys that had a
previous thread change use the default:
*/
#MaxThreadsPerHotkey 1
/*
Change keys for something meaningful
This example is in alphabetical order
*/
1::Spotify.Next()
2::Spotify.Previous()
3::Spotify.Repeat()
4::Spotify.SeekBackward()
5::Spotify.SeekForward()
6::Spotify.Shuffle()
7::Spotify.TogglePlay()
8::Spotify.VolumeDown()
9::Spotify.VolumeUp()
; Show Spotify
0::Spotify.Restore()
; Dependency
#Include %A_LineFile%\..\Spotify.ahk
#Requires AutoHotkey v1.1
/*
If placing below any hotkeys that had a
previous thread change use the default:
*/
#MaxThreadsPerHotkey 1
/*
Change keys for something meaningful
This example is in alphabetical order
*/
1::Spotify.Next()
2::Spotify.Previous()
3::Spotify.Repeat()
4::Spotify.SeekBackward()
5::Spotify.SeekForward()
6::Spotify.Shuffle()
7::Spotify.TogglePlay()
8::Spotify.VolumeDown()
9::Spotify.VolumeUp()
; Show Spotify
0::Spotify.Restore()
; Dependency
#Include %A_LineFile%\..\Spotify1.ahk
#Requires AutoHotkey v2.0
; Version: 2023.06.18.1
; Usages and information: https://redd.it/orzend
class Spotify extends Spotify.Controls {
static _hWnd := 0, _OnExit := ""
static Restore() {
this._Win()
WinShow()
WinActivate()
Spotify._hWnd := 0
if (IsObject(Spotify._OnExit)) {
OnExit(Spotify._OnExit, 0)
Spotify._OnExit := ""
}
}
static _Win(Stash := false) {
static title := "-|Spotify ahk_exe i)Spotify\.exe"
DetectHiddenWindows(true)
SetTitleMatchMode("RegEx")
hWnd := WinExist(title)
if (hWnd = 0) {
MsgBox("Spotify is not running...", "Error", 0x40010)
Exit()
}
if (Spotify._hWnd = hWnd) {
return
}
Spotify._hWnd := hWnd
visible := DllCall("IsWindowVisible", "Ptr", hWnd, "Int")
RunWait("Spotify:")
WinWait(title)
if (Stash && !visible) {
WinWaitActive()
WinHide()
if (!IsObject(Spotify._OnExit)) {
Spotify._OnExit := ObjBindMethod(Spotify, "Restore")
OnExit(Spotify._OnExit, 1)
}
}
}
class Controls {
static _shortcuts := Map("Next", "^{Right}", "Previous", "^{Left}", "Repeat", "^r", "SeekBackward", "+{Left}", "SeekForward", "+{Right}", "Shuffle", "^s", "TogglePlay", "{Space}", "VolumeDown", "^{Down}", "VolumeUp", "^{Up}")
static __Call(Action, *) {
static WM_APPCOMMAND := 793
if (!this._shortcuts.Has(Action)) {
throw Error("Invalid action." Action, -1)
}
shortcut := this._shortcuts[Action]
hActive := WinExist("A")
this._Win(true)
ControlFocus("Chrome Legacy Window")
ControlSend(shortcut, "Chrome Legacy Window")
if (Action ~= "i)^(Next|Previous|TogglePlay)$") {
hWnd := DllCall("FindWindow", "Str", "NativeHWNDHost", "Ptr", 0)
try PostMessage(WM_APPCOMMAND, 0x000C, 0xA0000, , hWnd)
}
WinActivate(hActive)
}
}
}
#Requires AutoHotkey v1.1
; Version: 2023.06.18.1
; Usages and information: https://redd.it/orzend
class Spotify extends Spotify.Controls {
static _hWnd := 0, _OnExit := ""
Restore() {
this._Win()
WinShow
WinActivate
Spotify._hWnd := 0
if (IsObject(Spotify._OnExit)) {
OnExit(Spotify._OnExit, 0)
Spotify._OnExit := ""
}
}
_Win(Stash := false) {
static title := "-|Spotify ahk_exe i)Spotify\.exe"
DetectHiddenWindows On
SetTitleMatchMode RegEx
hWnd := WinExist(title)
if (hWnd = 0) {
MsgBox 0x40010, Error, Spotify is not running...
Exit
}
if (Spotify._hWnd = hWnd) {
return
}
Spotify._hWnd := hWnd
visible := DllCall("IsWindowVisible", "Ptr", hWnd, "Int")
RunWait Spotify:
WinWait % title
if (Stash && !visible) {
WinWaitActive
WinHide
if (!IsObject(Spotify._OnExit)) {
Spotify._OnExit := ObjBindMethod(Spotify, "Restore")
OnExit(Spotify._OnExit, 1)
}
}
}
class Controls {
static _shortcuts := { Next: "^{Right}", Previous: "^{Left}", Repeat: "^r", SeekBackward: "+{Left}", SeekForward: "+{Right}", Shuffle: "^s", TogglePlay: "{Space}", VolumeDown: "^{Down}", VolumeUp: "^{Up}" }
__Call(Action, _*) {
static WM_APPCOMMAND := 793
if (!this._shortcuts.HasKey(Action)) {
throw Exception("Invalid action." Action, -1)
}
shortcut := this._shortcuts[Action]
hActive := WinExist("A")
this._Win(true)
ControlFocus Chrome Legacy Window
ControlSend Chrome Legacy Window, % shortcut
if (Action ~= "i)^(Next|Previous|TogglePlay)$") {
hWnd := DllCall("FindWindow", "Str", "NativeHWNDHost", "Ptr", 0)
try PostMessage WM_APPCOMMAND, 0x000C, 0xA0000, , % "ahk_id" hWnd
}
WinActivate % "ahk_id" hActive
}
}
}
--- Spotify.ahk
+++ Spotify1.ahk
@@ -1 +1 @@
-#Requires AutoHotkey v2.0
+#Requires AutoHotkey v1.1
@@ -10 +10 @@
- static Restore() {
+ Restore() {
@@ -12,2 +12,2 @@
- WinShow()
- WinActivate()
+ WinShow
+ WinActivate
@@ -21 +21 @@
- static _Win(Stash := false) {
+ _Win(Stash := false) {
@@ -23,2 +23,2 @@
- DetectHiddenWindows(true)
- SetTitleMatchMode("RegEx")
+ DetectHiddenWindows On
+ SetTitleMatchMode RegEx
@@ -27,2 +27,2 @@
- MsgBox("Spotify is not running...", "Error", 0x40010)
- Exit()
+ MsgBox 0x40010, Error, Spotify is not running...
+ Exit
@@ -35,2 +35,2 @@
- RunWait("Spotify:")
- WinWait(title)
+ RunWait Spotify:
+ WinWait % title
@@ -38,2 +38,2 @@
- WinWaitActive()
- WinHide()
+ WinWaitActive
+ WinHide
@@ -49 +49 @@
- static _shortcuts := Map("Next", "^{Right}", "Previous", "^{Left}", "Repeat", "^r", "SeekBackward", "+{Left}", "SeekForward", "+{Right}", "Shuffle", "^s", "TogglePlay", "{Space}", "VolumeDown", "^{Down}", "VolumeUp", "^{Up}")
+ static _shortcuts := { Next: "^{Right}", Previous: "^{Left}", Repeat: "^r", SeekBackward: "+{Left}", SeekForward: "+{Right}", Shuffle: "^s", TogglePlay: "{Space}", VolumeDown: "^{Down}", VolumeUp: "^{Up}" }
@@ -51 +51 @@
- static __Call(Action, *) {
+ __Call(Action, _*) {
@@ -53,2 +53,2 @@
- if (!this._shortcuts.Has(Action)) {
- throw Error("Invalid action." Action, -1)
+ if (!this._shortcuts.HasKey(Action)) {
+ throw Exception("Invalid action." Action, -1)
@@ -59,2 +59,2 @@
- ControlFocus("Chrome Legacy Window")
- ControlSend(shortcut, "Chrome Legacy Window")
+ ControlFocus Chrome Legacy Window
+ ControlSend Chrome Legacy Window, % shortcut
@@ -63 +63 @@
- try PostMessage(WM_APPCOMMAND, 0x000C, 0xA0000, , hWnd)
+ try PostMessage WM_APPCOMMAND, 0x000C, 0xA0000, , % "ahk_id" hWnd
@@ -65 +65 @@
- WinActivate(hActive)
+ WinActivate % "ahk_id" hActive
@anonymous1184
Copy link
Author

Glad you got it working @OmTatSat!

Regarding the artists, that's something you have to find out for yourself. I've been listening exclusively to metal for the last 30 years and having multiple artists is something not seen so I never looked into that.

For the sake of simplicity I used a JSON parser from a COM object, is very restrictive as it doesn't let you iterate (I explained that in the original posts). You might want to use cocobelgica's (or any other) that loads the JSON string into a native AHK object. The screenshot I previously shared of VSCode is precisely that.

However if you stick with the COM object use try statements:

artist0 := ""
try artist0 := res.item.artist.0.name
artist1 := ""
try artist1 := res.item.artist.1.name
artist2 := ""
try artist2 := res.item.artist.2.name
artist3 := ""
try artist3 := res.item.artist.3.name
MsgBox % "Artist 0: " artist0
    . "`n Artist 1: " artist1
    . "`n Artist 2: " artist2
    . "`n Artist 3: " artist3

I know it looks awful but you can't do it with a loop. If you have issues I recommend you to use r/AutoHotkey as there's lots of people that can help.

@OmTatSat
Copy link

@anonymous1184
Great solution! Thank you, it is very useful for me

@NeonLightning
Copy link

i feel like i'm missing something but surprisingly this is pretty much the only script i've found that'll handle spicefied version. but i've noticed when i first run a hotkey it focuses the window(well activates it in the background) but doesn't do anything until i then minimize and restore spotify.

@anonymous1184
Copy link
Author

@NeonLightning: given that I no longer use v1.1, I added the v2.0 version and updated a little the code, adding an extra step to make sure the focused window was not lost (no core functionality was changed).

Check the README.md for version information and the Reddit post for overall details.

@NeonLightning
Copy link

ah shame i use it with vmr.ahk to control voicemeeter and that's a 1.0 thanks anyways

@anonymous1184
Copy link
Author

@NeonLightning like I've said, check the README.md it contains both versions.

@NeonLightning
Copy link

yup finally figured it out. my apologies

@NeonLightning
Copy link

workin great btw.
the other one i used that used api calls stopped working for me but your um... unique method. is working still :)

@anonymous1184
Copy link
Author

@NeonLightning Thanks for letting me know, I'll check out the API calls and revisit the code as there are improved methods I'd like to implement for that one too.

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