I'm building a script that will pause my music if it's playing when I lock my workstation. I use spotify, which should be simple to get it's play state by inspecting the window title. When not playing anything, it's title is simply "Spotify", but when it's playing media, the window title changes to the title of the track currently playing. I can see this using the Window Spy.
I've tried finding the spotify window and reading it's title, using WinGetTitle, title, ahk_exe Spotify.exe
which should write the title into the var title
. This doesn't work, title
is an empty string. Intriguingly however it works if the spotify window is minimized.
#L::
{
WinGetTitle, title, ahk_exe Spotify.exe
if(title != "Spotify")
{
Send {Media_Play_Pause}
}
DllCall("LockWorkStation")
return
}
This is on Windows 10. WinGetClass, c, ahk_exe Spotify.exe
correctly finds the window, but the class name is Chrome_WidgetWin0
because I'm guessing the app is written in Electron. Other electron apps seem to have the same class name, just incrementing the number at the end.
What I'd love is a way of hooking into whatever windows API spotify is using to report it's current play state, as Windows 10 recognises it as a media application and adds play/pause buttons to it's tab in the taskbar, and in the windows volume control overlay.
Thanks
There are probably more than one windows of class "Chrome_WidgetWin0" belonging to the process "Spotify.exe".
Try this:
#IfWinExist ahk_exe Spotify.exe
#l::
WinGet, id, list, ahk_exe Spotify.exe
Loop, %id%
{
this_ID := id%A_Index%
WinGetTitle, title, ahk_id %this_ID%
If (title = "")
continue
If (title != "Spotify")
{
Send {Media_Play_Pause}
break
}
}
DllCall("LockWorkStation")
return
#IfWinExist
EDIT: To find out whether it really works, run this test:
#IfWinExist ahk_exe Spotify.exe
#q:: ; Win+Q
WinGet, id, list, ahk_exe Spotify.exe
Loop, %id%
{
this_ID := id%A_Index%
WinGetTitle, title, ahk_id %this_ID%
; MsgBox, "%title%"
If (title = "")
continue
MsgBox, "%title%"
If (title != "Spotify")
{
Send {Media_Play_Pause}
break
}
}
return
#IfWinExist
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With