【问题标题】:Why is my call to WinGetTitle returning an empty string?为什么我对 WinGetTitle 的调用返回一个空字符串?
【发布时间】:2019-02-07 09:34:17
【问题描述】:

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

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

#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 正确找到了窗口,但类名是 Chrome_WidgetWin0,因为我猜该应用程序是用 Electron 编写的。其他电子应用程序似乎具有相同的类名,只是在末尾增加了数字。

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

谢谢

【问题讨论】:

    标签: autohotkey


    【解决方案1】:

    可能有多个属于进程“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
    

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

    #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
    

    【讨论】:

    • 谢谢,成功了。你是对的,看任务管理器,我可以看到 5 个进程属于 spotify。如果我删除 break 我可以看到 ahk 正在找到 5 个与 ahk_exe spotify.exe 匹配的窗口。
    猜你喜欢
    • 1970-01-01
    • 2017-09-04
    • 2012-03-19
    • 1970-01-01
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    相关资源
    最近更新 更多