How to start a Spotify song using Autohotkey while minimizing it?

How to show a song Spotify while minimizing it?

Well, that's why the Spotify application on Windows does not have built-in support for global hot keys and very simple hot keys, even if the application window is active. The disappointment of the "main role" of the song that is currently playing does not have a keyboard shortcut, even when the window is active.

So, I have an Autohotkey script that gives me global hot keys for controlling playback, volume and copying the name of the song (including em dash fixing), but I hit a brick wall trying to figure out how to draw the current song.

To make things even more complicated, I want the Autohotkey script to launch a song if it wasn’t removed. If he has already starred, just leave him alone.

I want all this to happen when the application is in the tray without opening the window.


EDIT: Semi-Separate Solution

So, I came up with a reasonable solution, it opens a context menu, even when the program is minimized and does everything, including uncontrolled songs. Unfortunately, the context menu means that it minimizes full-screen applications, such as games within a second. Depending on the game, which can be a pain, but this is the best I can do without fancy calls, etc.

#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 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 } 

I changed some existing scripts that were hidden on the Internet. The sources of which can be found at the top of the source code, this can help if someone wants to try to get it to work completely without the context menu appearing.


How can you play a song in normal use of the application?

There seem to be only 2 routes to play a song on Spotify.

  • Right-click on the album cover in the lower left corner and use the context menu option.
  • Right-click on a song in the list of playlists and use the context menu option.

To make things uncomfortable, the star menu option is replaced by the Noar menu option in the same place if something is already taken. They also have the same hotkey when the menu is open (t key). Thus, you cannot just make a blind menu choice without any verification.

So what are the possible routes we can take?

Right-clicking on the album cover in the lower left is the only realistic route.

If we can read the text from the context menu and minimize it, we can say that something has already been marked by changing the text from “star” to “nestar”. After this test, we can decide whether to press the T key to actually play the song.

I also spent some time with Window Detective to see if I could just send the relatively simple PostMessage / SendMessage to play the song. But I have very little experience, and I don’t know how to tie them together to get the result I want.

I found out that the Star and Custom Context menu options are actually different:

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

However, I do not know how to actually combine PostMessages / SendMessages to open the menu, and then select only item No. 11, and then press enter.

List of messages that Spotify receives when playing something

Just in case, this helps determine if the PostMessage / SendMessage route is possible.

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

Afterword

I searched for a while trying to find some good examples of PostMessages / SendMessages that are used to select such a context menu, but didn't find anything.

Thank you for your time.

Current Autohotkey Script

 #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 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 } 
+6
source share
1 answer

The one I'm currently using during the game is pretty simple:

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

A clean replacement for your other code; Spotify responds to multimedia buttons, so they work:

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

Edit : Spotify has changed their layout. "+" Moves with the length of the song name. To make it work more consistently, I changed the function to right click and "Save to your music."

 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 } 
+2
source

Source: https://habr.com/ru/post/945949/


All Articles