Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Spotify Global Hotkeys - Volume Control, Skip songs, Current Song to Clipboard, Star Songs without opening Spotify


  • Please log in to reply
10 replies to this topic
bluesatin
  • Members
  • 13 posts
  • Last active: Jan 31 2015 12:26 AM
  • Joined: 29 Aug 2007

Spotify Global Hotkeys

So there's a bunch of Spotify global hotkey scripts lurking around, but a lot of them are missing stuff like proper copy song-title copying (Spotify uses en-dashes)  and none of them had song starring without opening Spotify.

 

So this script will allow you control Spotify even when minimized, including starring songs. The script will only a star song if it hasn't been starred already, so it won't remove any of your favourite songs by accident! :D

 

Current Limitations

Unfortunately I can't get it working exactly as I had planned, the starring part will open up a context-menu for a split second to star the song (without opening Spotify). This means it will minimize full-screen games for a split second, which is obviously a pain depending on the game.

 

As Spotify doesn't even have a hotkey to star a song even when the program is open, this is the best I can do without fancy DLL calls or memory edits that I've no idea how to do. I've tried asking for help in regards to this but didn't get anywhere, if anyone can improve it without the context-menu opening it would be awesome! :D

 

It's not completely automatic and I've had to adjust it recently as the position of the 'star' and 'unstar' context-menu selections has changed in a recent patch. You may need to manually adjust this in the future, the code is pretty liberally commented if you need to make adjustments.

 

Hotkeys

  • CTRL + ALT + HOME - Pause/Play
  • CTRL + ALT + PAGE UP/DOWN - Previous/Next song
  • CTRL + ALT + UP/DOWN Arrow Keys - Adjust volume up/down
  • CTRL + ALT + INSERT - Star current song
  • CTRL + ALT + END - Copy the current song title to your clipboard

The 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.

;|---------------|
;|--[ 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
}

A couple of the scripts I used as bases are listed at the top of the script for anyone wondering about sources. Big shout out to the guy who did the base of the starring part of the script, I only had to modify it a little to get it working without opening up Spotify.


<3

patchie
  • Members
  • 31 posts
  • Last active: May 08 2014 01:51 PM
  • Joined: 22 Feb 2008

Bluesatin,

 

Thanks a lot  :D


..:: Patchie ::..

beng92
  • Members
  • 1 posts
  • Last active: Jun 05 2014 03:50 PM
  • Joined: 26 May 2014

As Spotify has moved away from the star feature, here's a shortcut to add the currently playing song into your 'Your Music' collection. 

; "CTRL + DOWN"  for adding currently playing song to Your Music (clicks the plus by the playing song)
^Down::
{
CoordMode, Mouse, Screen
MouseGetPos, OrigX, OrigY
WinActivate, ahk_class SpotifyMainWindow
WinGetPos, X, Y, Width, Height, ahk_class SpotifyMainWindow
MouseClick, left, X+233, Y+Height-65
WinMinimize, ahk_class SpotifyMainWindow
MouseMove, OrigX, OrigY
return 
}

It saves your current mouse position, activates the spotify window, clicks the + next to the track name, above the bottom left play button (this works with album art showing or minimized), minimizes spotify and returns your mouse to its original position on the screen.



flyjdix11
  • Members
  • 1 posts
  • Last active: Mar 11 2015 02:40 AM
  • Joined: 11 Mar 2015

Anyone know if having spotify premium would change this script at all. 

 

Would SpotifyMainWindow change to Spotify Premium ? 

 

Can't figure this out, just learned about auto hot key today, awesome program and would love to get these scripts working for spotify right away. Thanks for any help!



Godric
  • Members
  • 19 posts
  • Last active: Jun 02 2016 09:11 PM
  • Joined: 11 Nov 2013

I have Spotify Premium with the latest version and this script doesn't work for it.



hilux
  • Members
  • 1 posts
  • Last active: Mar 11 2015 10:48 PM
  • Joined: 11 Mar 2015

Trying to edit this script for the newer version of Spotify Premium that just released. What do I change SpotifyMainWindow to? Or where to do i get the proper title for Spotify Premium to make AHK identify it in this script?



jso0003auburn
  • Members
  • 1 posts
  • Last active: Mar 16 2015 03:04 PM
  • Joined: 12 Mar 2015

Trying to edit this script for the newer version of Spotify Premium that just released. What do I change SpotifyMainWindow to? Or where to do i get the proper title for Spotify Premium to make AHK identify it in this script?

 

One of the developers for Spotify actually posted about this on another thread on Stack Overflow...

 

It was a bug on their part and they're going to fix it in the next build.

 

In the meantime you can use the generic media key commands, although it could get confusing if you use other players...

; "CTRL + RIGHT" for next
^Right::Media_Next

; "CTRL + LEFT"  for previous 
^Left::Media_Prev

; "CTRL + UP"  for pause
^Up::Media_Play_Pause



JimJimJimBob
  • Members
  • 3 posts
  • Last active: May 09 2015 05:20 PM
  • Joined: 09 May 2015

How do I install it? Complete noob here.



JimJimJimBob
  • Members
  • 3 posts
  • Last active: May 09 2015 05:20 PM
  • Joined: 09 May 2015

Okay, I installed it, but the pause hotkey doesn't work.



kakamaus
  • New members
  • 1 posts
  • Last active: Aug 12 2015 09:45 AM
  • Joined: 12 Aug 2015

Okay, I installed it, but the pause hotkey doesn't work.

 

ControlSend, ahk_parent, {space}, ahk_class SpotifyMainWindow does not work anymore (at the moment) due to a Spotify Update!!

 

The Media_Play_Pause works, but beware of Google Chrome, it seems to intercept the command.

It works for me if I start the Script first, then Spotify and Chrome last. And as the script propably starts with the system for the most of us, just keep in mind to open Spotify before Chrome. 



masalm
  • New members
  • 1 posts
  • Last active: Aug 28 2015 11:56 AM
  • Joined: 28 Aug 2015
The following updated script works fine for me to add a song to "my music" in the latest version of Spotify Premium.
I just had to change the location of the click.
^Insert::
{
#IfWinExist ahk_class SpotifyMainWindow ;Only do the following if Spotify is running
spotify = ahk_class SpotifyMainWindow ;Set variable for Spotify Window Name


CoordMode, Mouse, Screen
MouseGetPos, OrigX, OrigY
WinActivate, ahk_class SpotifyMainWindow
WinGetPos, X, Y, Width, Height, ahk_class SpotifyMainWindow
MouseClick, left, X+180, Y+Height-88
WinMinimize, ahk_class SpotifyMainWindow
MouseMove, OrigX, OrigY
return 
}