spotify - 如何在最小化时使用 Autohotkey 为 Spotify 歌曲加注星标?

标签 spotify autohotkey hotkeys minimized

如何在最小化时为 Spotify 歌曲加注星标?

好吧,Windows 上的 Spotify 应用程序没有对全局热键和非常基本的热键的内置支持,即使应用程序窗口当前处于事件状态也是如此。令人沮丧的是,即使窗口处于事件状态,“主演”当前正在播放的歌曲也没有键盘快捷键。

所以我有一个 Autohotkey 脚本,它为我提供了用于播放控制、音量和复制歌曲标题(包括破折号修复)的全局热键,但我在试图弄清楚如何为当前歌曲加注星标时遇到了困难。

为了让事情变得更困难,我只希望 Autohotkey 脚本为尚未加星标的歌曲加星标。如果它已经加星标,那么就不要管它。

我希望这一切在应用程序位于托盘中时发生,而不打开窗口。

<小时/>

编辑:半工作解决方案

所以我想出了一个合理的解决方案,即使程序最小化,它也会打开一个上下文菜单,并执行所有操作,包括不取消加星标的歌曲。不幸的是,上下文菜单意味着它将瞬间最小化游戏等全屏应用程序。根据游戏的不同,这可能会很痛苦,但这是我能做的最好的事情,不需要花哨的 DLL 调用和其他东西。

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;|---------------|
;|--[ SOURCES ]--|
;|---------------|
;Base Spotify Script from: http://www.autohotkey.com/board/topic/36239-spotify-global-hotkeys/
;Base Starring Script from: http://superuser.com/questions/324416/any-spotify-tweaks-with-keyboard-shortcut-to-star-tracks

;|------------------|
;|--[ SETTING UP ]--|
;|------------------|
DetectHiddenWindows, On ;Detect Spotify even if it's minimized
#IfWinExist ahk_class SpotifyMainWindow ;Only do the following if Spotify is running
spotify = ahk_class SpotifyMainWindow ;Set variable for Spotify Window Name

;|---------------|
;|--[ HOTKEYS ]--|
;|---------------|
; "CTRL + ALT + PAGEUP" for previous 
^!PGUP:: 
{
    ControlSend, ahk_parent, ^{Left}, %spotify% 
    return 
}

; "CTRL + ALT + PAGEDOWN" for next 
^!PGDN:: 
{ 
    ControlSend, ahk_parent, ^{Right}, %spotify% 
    return 
} 

; "CTRL + ALT + HOME" for pause
^!Home::
{ 
    ControlSend, ahk_parent, {Space}, %spotify% 
    return
} 

; "CTRL + ALT + END" for track-name
^!End:: 
{ 
    WinGetTitle, spotify_playing, %spotify% ;Get the title of Spotify which contains the track-name

    StringTrimLeft, trimmed_playing, spotify_playing, 10 ;Get rid of extra text and place into 'trimmed_playing'
    StringReplace, replaced_playing, trimmed_playing, –, -, All ;Replace en dash with normal dash and place into 'replaced_playing'

    clipboard = %replaced_playing% ;Copy the fixed text to clipboard
    return 
} 

; "CTRL + ALT + UP" for volume up
^!Up::
{ 
    ControlSend, ahk_parent, ^{Up}, %spotify% 
    return 
} 

; "CTRL + ALT + DOWN" for volume down
^!Down::
{ 
    ControlSend, ahk_parent, ^{Down}, %spotify% 
    return 
} 

; "CTRL + ALT + INSERT" for starring the current song
^!Insert::
{ 
    ;Store active window and mouse position.
    MouseGetPos, , , winID

    ;Right click near the song title in the "Now Playing" box.
    WinGetPos,  ,  ,  , spotifyHeight, %spotify%
    clickX := 100
    clickY := spotifyHeight-70 
    ControlClick, x%clickX% y%clickY% , %spotify%, , Right, , NA

    ;Get the contents of the context menu.
    WinWait, ahk_class #32768
    SendMessage, 0x1E1 ;MN_GETHMENU
    allContextMenuInfo := ErrorLevel

    ;The "Star" command is the 2nd menu item.
    ;If the song is Unstarred the text is Star, and vice versa. But sometimes some wierd characters are included.
    ;The only reliable way I found is to check if the first letter is S.
    menuText_StarUnstar := GetContextMenuItemText(allContextMenuInfo, 2)
    StringGetPos, positionOfS, menuText_StarUnstar, S

    ;If S is the first letter, star the song.
    notStarred := (%positionOfS% = 0)
    If notStarred 
    {
        ;Arrow down to the Star menu item and press enter.
        ControlSend, ahk_parent, {Down}{Down}{Enter}, %spotify% 
    } 
    Else 
    {
        ;Just close the context menu.
        ControlSend, ahk_parent, {Escape}, %spotify% 
    }

    ;Restore original window and mouse position.
    WinActivate ahk_id %winID%

    return
}

;|-----------------|
;|--[ FUNCTIONS ]--|
;|-----------------|

;Context menu helper function.
GetContextMenuItemText(hMenu, nPos)
{
    length := DllCall("GetMenuString"
            , "UInt", hMenu
            , "UInt", nPos
            , "UInt", 0 ; NULL
            , "Int", 0  ; Get length
            , "UInt", 0x0400)   ; MF_BYPOSITION
        VarSetCapacity(lpString, length + 1)
        length := DllCall("GetMenuString"
            , "UInt", hMenu
            , "UInt", nPos
            , "Str", lpString
            , "Int", length + 1
            , "UInt", 0x0400)
    return lpString
}

我修改了一些隐藏在互联网上的现有脚本。其来源可以在源代码的顶部找到,如果有人想尝试让它在不出现上下文菜单的情况下完全工作,这可能会有所帮助。

<小时/>

正常使用应用程序时如何为歌曲加注星标?

在 Spotify 中为歌曲加星标似乎只有 2 种途径。

  1. 右键单击左下角的专辑封面并使用星形上下文菜单选项。
  2. 右键点击播放列表中的歌曲并使用星号上下文菜单选项。

为了让事情变得尴尬,如果某些内容已经加星标,则加星标菜单选项将在完全相同的位置替换为取消加星标菜单选项。当菜单打开时,它们也具有相同的快捷键(t 键)。因此,您不能在没有先进行某种检查的情况下进行盲目的菜单选择。

那么我们可以采取哪些可能的路线?

右键单击左下角的专辑封面是唯一现实的路线。

如果我们可以在最小化时从上下文菜单中读取文本,我们可以通过文本从“星号”到“取消星号”的变化来判断某些内容是否已经加星标。测试结束后,我们可以决定是否按 T 键来真正为歌曲加星。

我还花了一些时间与 Window Detective 合作,看看是否可以只发送一个相对简单的 PostMessage/SendMessage 来为这首歌加注星标。但我的经验很少,我不知道如何将它们链接在一起以获得我想要的结果。

我发现加星标和取消加星标上下文菜单选项实际上是不同的:

Star   -> WM_MENUSELECT (0x11F) | item identifier = 11 | flags = NONE | MF_HILITE
Unstar -> WM_MENUSELECT (0x11F) | item identifier = 12 | flags = NONE | MF_HILITE

但是我不知道如何实际链接 PostMessages/SendMessages 来打开菜单,然后只选择项目 #11,然后按 Enter 键。

Spotify 在为某内容加注星标时收到的消息列表

以防万一,这有助于确定是否可以执行 PostMessage/SendMessage 路由。

->WM_RBUTTONDOWN (0x204)
->WM_RBUTTONUP (0x205)
->WM_MENUSELECT (0x11F)
<-WM_MENUSELECT (0x11F)
->WM_KEYDOWN (0x100)
->WM_MENUSELECT (0x11F)
<-WM_MENUSELECT (0x11F)
->WM_KEYUP (0x101)

后记

我搜索了一段时间,试图找到用于执行此类上下文菜单选择的 PostMessages/SendMessages 的好示例,但没有找到任何内容。

感谢您的宝贵时间。

当前 Autohotkey 脚本

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; "CTRL + ALT + PAGEUP"  for previous 
^!PGUP:: 
{
    DetectHiddenWindows, On 
    ControlSend, ahk_parent, ^{Left}, ahk_class SpotifyMainWindow 
    DetectHiddenWindows, Off 
    return 
}

; "CTRL + ALT + PAGEDOWN"  for next 
^!PGDN:: 
{ 
    DetectHiddenWindows, On 
    ControlSend, ahk_parent, ^{Right}, ahk_class SpotifyMainWindow 
    DetectHiddenWindows, Off 
    return 
} 

; "CTRL + ALT + HOME"  for pause
^!Home::
{ 
    DetectHiddenWindows, On 
    ControlSend, ahk_parent, {space}, ahk_class SpotifyMainWindow 
    DetectHiddenWindows, Off 
    return
} 

; "CTRL + ALT + END"  for info 
^!End:: 
{ 
    DetectHiddenWindows, On 
    WinGetTitle, spotify_playing, ahk_class SpotifyMainWindow 
    DetectHiddenWindows, Off 

    StringTrimLeft, trimmed_playing, spotify_playing, 10 
    StringReplace, replaced_playing, trimmed_playing, –, -, All

    clipboard = %replaced_playing%
    return 
} 

; "CTRL + ALT + UP"  for volume up
^!Up::
{ 
    DetectHiddenWindows, On 
    ControlSend, ahk_parent, ^{Up}, ahk_class SpotifyMainWindow 
    DetectHiddenWindows, Off 
    return 
} 

; "CTRL + ALT + DOWN"  for volume down
^!Down::
{ 
    DetectHiddenWindows, On 
    ControlSend, ahk_parent, ^{Down}, ahk_class SpotifyMainWindow 
    DetectHiddenWindows, Off 
    return 
} 

/*
PLAN: I want to make this star the current song if it's unstarred
NOTES: 
    WM_MENUSELECT for star seems to be item identifier 11, unstar is 12
    Star   -> WM_MENUSELECT (0x11F) | item identifier = 11 | flags = NONE | MF_HILITE
    Unstar -> WM_MENUSELECT (0x11F) | item identifier = 12 | flags = NONE | MF_HILITE
*/
; "CTRL + ALT + INSERT"  for starring the current song
^!Insert::
{ 
    DetectHiddenWindows, On 

    DetectHiddenWindows, Off 
    return 
}

最佳答案

我目前在玩游戏时使用的非常简单:

MouseGetPos, x, y, origwin
WinActivate, ahk_class SpotifyMainWindow
click 183, 1000 ;
WinActivate, ahk_id %origwin%
MouseMove, %x%, %y%

完美替代您的其他代码; Spotify 响应媒体按钮,因此这些按钮非常有用:

^!Left::Media_Prev
^!Right::Media_Next
^!Up::Volume_Up
^!Down::Volume_Down
^!Space::Media_Play_Pause

编辑:Spotify 更改了布局。 + 随歌曲标题的长度而移动。为了使其工作更加一致,我将功能更改为右键单击并“保存到您的音乐”。

FavouriteSpotifySong()
{
    WinGetPos, , , SPOTIFY_X_AXIS, SPOTIFY_Y_AXIS, ahk_class SpotifyMainWindow
    yPos := SPOTIFY_Y_AXIS - 30
    ControlClick, X25 Y%yPos%, ahk_class SpotifyMainWindow,, RIGHT
    Sleep 75
    yPos -= 54
    ControlClick, X33 Y%yPos%, ahk_class SpotifyMainWindow
}

关于spotify - 如何在最小化时使用 Autohotkey 为 Spotify 歌曲加注星标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16775905/

相关文章:

autohotkey - 如何为 AutoHotkey 脚本添加管理员权限?

ios - 如何播放 Spotify 播放列表

html - 在 ahk/html 脚本中更改图像颜色

android - 在 Android 上加载 libpthread.so 时出错

autohotkey - 切换键盘布局有效但不总是

c++ - X11 : how to implement a global hotkey without focusloss

c# - 使用 WPF C# 创建后台热键的正确方法? (最好不使用遗留代码)

javascript - 此热键是否适用于 Mac 用户?

html - 为什么我的 Spotify 应用程序中的共享按钮图标未对齐?

java - Postman 和 RestTemplate 的 API 调用中的主体参数相同,但只有 Postman 有效