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

Ric*_*ich 5 autohotkey

我正在构建一个脚本,当我锁定我的工作站时,它会暂停我的音乐。我使用 spotify,通过检查窗口标题来获取它的播放状态应该很简单。当不播放任何内容时,它的标题只是“Spotify”,但在播放媒体时,窗口标题更改为当前正在播放的曲目的标题。我可以使用 Window Spy 看到这一点。

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

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

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

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

谢谢

use*_*297 6

属于“Spotify.exe”进程的“Chrome_WidgetWin0”类窗口可能不止一个。

尝试这个:

#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
Run Code Online (Sandbox Code Playgroud)

编辑:要确定它是否真的有效,请运行以下测试:

#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
Run Code Online (Sandbox Code Playgroud)