Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is my call to WinGetTitle returning an empty string?

Tags:

autohotkey

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

like image 349
Rich Avatar asked Feb 07 '19 09:02

Rich


1 Answers

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
like image 155
user3419297 Avatar answered Sep 23 '22 16:09

user3419297