为什么我对 WinGetTitle 的调用返回一个空字符串?

Why is my call to WinGetTitle returning an empty string?

我正在构建一个脚本,当我锁定工作站时,如果音乐正在播放,它会暂停音乐。我使用 spotify,通过检查 window 标题应该很容易获得它的播放状态。当不播放任何内容时,它的标题只是 "Spotify",但当它播放媒体时,window 标题会更改为当前播放曲目的标题。我可以使用 Window Spy.

看到这个

我尝试找到 spotify window 并使用 WinGetTitle, title, ahk_exe Spotify.exe 读取它的标题,这应该将标题写入 var title。这不起作用,title 是一个空字符串。有趣的是,如果将 spotify window 最小化,它会起作用。

#L::
{
   WinGetTitle, title, ahk_exe Spotify.exe
   if(title != "Spotify")
   {
      Send {Media_Play_Pause}
   }
   DllCall("LockWorkStation")
   return
}

这是在 Windows 10 上。WinGetClass, c, ahk_exe Spotify.exe 正确找到了 window,但 class 名称是 Chrome_WidgetWin0,因为我猜是应用程序是用电子写的。其他电子应用程序似乎具有相同的 class 名称,只是在末尾递增数字。

我喜欢的是一种挂钩任何 windows API spotify 用来报告其当前播放状态的方法,因为 Windows 10 将其识别为媒体应用程序并在任务栏和 windows 音量控制叠加层中将 play/pause 按钮添加到它的选项卡中。

谢谢

可能有 windows 个 class "Chrome_WidgetWin0" 属于进程 "Spotify.exe"。

试试这个:

#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

编辑:要了解它是否真的有效,运行 这个测试:

#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