Sending hotkeys to Spotify Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Selbi

Sending hotkeys to Spotify  Topic is solved

26 Apr 2018, 08:33

This used to work a while ago, but recently Spotify severely messed with the way the Windows interface is identified by AHK. This is about as much info as you can get: https://i.imgur.com/IAvKZgB.png

So I tried many things. My most recent attempt was identifying the window ID and send it via ahk_id, but nothing happens. Sending keystrokes while the window is active is no problem, but I want to be able to do this while playing games, hence the background approach.

Code: Select all

Spotify_HotkeySend(hotkeyString) {
	if WinActive("ahk_exe Spotify.exe") {
		Send, %hotkeyString%
	} else {
		id := Get_Spotify_Id()
		ControlSend, ahk_parent, %hotkeyString%, ahk_id %id%
	}
}


Get_Spotify_Id() {
	WinGet, id, list,,, Program Manager
	Loop, %id% {
		this_id := id%A_Index%
		if (Is_Spotify(this_id)) {
			return, this_id
		}
	}
	return, -1
}


Is_Spotify(this_id) {
	WinGetClass, this_class, ahk_id %this_id%
	if (InStr(this_class, "Chrome_WidgetWin_0")) {
		WinGetText, this_text, ahk_id %this_id%
		if (InStr(this_text, "Chrome Legacy Window")) {
			return, true
		}
	}
	return, false
}
Selbi

Re: Sending hotkeys to Spotify

26 Apr 2018, 12:34

Uhh, why was this marked as solved? Not a single person even responded.
Selbi

Re: Sending hotkeys to Spotify

29 Apr 2018, 09:51

Fixed it.

Code: Select all

; Volume Up/Down (u and d only examples here)
d::Spotify_HotkeySend("^{Down}")
u::Spotify_HotkeySend("^{Up}")

; Global variable to cache the Spotify Window ID once it's been found
global cached_spotify_window := 0

; FUNCTION: Send a hotkey string to Spotify 
Spotify_HotkeySend(hotkeyString) {
    DetectHiddenWindows, On
    winId := Get_Spotify_Id()
    ControlFocus, , ahk_id %winId%
    ControlSend, , %hotkeyString%, ahk_id %winId%
    DetectHiddenWindows, Off
    return
}

; FUNCTION: Get the ID of the Spotify window (using cache)
Get_Spotify_Id() {
    if (Is_Spotify(cached_spotify_window)) {
        return cached_spotify_window
    }

    WinGet, windows, List, ahk_exe Spotify.exe
    Loop, %windows% {
        winId := windows%A_Index%
        if (Is_Spotify(winId)) {
            cached_spotify_window = %winId%
            return winId
        }
    }
}

; FUNCTION: Check if the given ID is a Spotify window
Is_Spotify(winId) {
    WinGetClass, class, ahk_id %winId%
    if (class == "Chrome_WidgetWin_0") {
        WinGetTitle, title, ahk_id %winId%
        return (title != "")
    }
    return false
}
albertocasanova
Posts: 9
Joined: 01 Jul 2017, 04:32

Re: Sending hotkeys to Spotify

03 Jul 2018, 17:03

Works like a charm!

Can you make it work when Spotity is minimized to the notification area?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: hedehede81 and 273 guests