Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Spotify and AHK


  • Please log in to reply
37 replies to this topic
rob_henry
  • Members
  • 1 posts
  • Last active: Sep 29 2009 12:02 PM
  • Joined: 29 Sep 2009
That's a neat script ^dallas! I had just knocked something together very similar, although I missed the check for Spotify in the tray!

Is there a specific reason you use
WinActivate, ahk_class SpotifyMainWindow
and then
WinWaitActive, ahk_class SpotifyMainWindow
I checked the docs but I don't understand the reason for both checks?

Great work again!
cheers,
Rob

dallas^
  • Guests
  • Last active:
  • Joined: --

That's a neat script ^dallas! I had just knocked something together very similar, although I missed the check for Spotify in the tray!

Is there a specific reason you use
WinActivate, ahk_class SpotifyMainWindow
and then
WinWaitActive, ahk_class SpotifyMainWindow
I checked the docs but I don't understand the reason for both checks?

Great work again!
cheers,
Rob


Hi Rob,

WinWaitActive was just to make it absolutely sure the window is active before proceeding, but you're right, it's probably unnecessary quoting the docs "Six attempts will be made to activate the target window over the course of 60ms. Thus, it is usually unnecessary to follow WinActivate with WinWaitActive or IfWinNotActive."

And now when I look more closely the docs it looks like it might actually wait indefinitely in a case of some error. To make it more reliable I think it should look more like this:

^+F11::
{		
	playlist_number = 1 ;Playlist number from the top
	
	SetDefaultMouseSpeed, 0
	CoordMode, Mouse, Screen
	MouseGetPos, original_mouse_x, original_mouse_y
	WinGetActiveTitle, original_window_title
	
	;Spotify in the taskbar
	IfWinExist, ahk_class SpotifyMainWindow 
	{
		WinActivate, ahk_class SpotifyMainWindow
		WinWaitActive, ahk_class SpotifyMainWindow,,5
		if ErrorLevel
		{
		    MsgBox, WinWait timed out.
			return
		}
		add_song()
		WinMinimize, ahk_class SpotifyMainWindow

		MouseMove, original_mouse_x, original_mouse_y
		WinActivate, %original_window_title%
		
		return
	}
	
	;Spotify in the traybar
	DetectHiddenWindows, On
	IfWinExist, ahk_class SpotifyMainWindow 
	{
		WinRestore, ahk_class SpotifyMainWindow
		WinActivate, ahk_class SpotifyMainWindow
		WinWaitActive, ahk_class SpotifyMainWindow,,5
		if ErrorLevel
		{
		    MsgBox, WinWait timed out.
			return
		}
		add_song()
		WinClose, ahk_class SpotifyMainWindow
		
		MouseMove, original_mouse_x, original_mouse_y
		WinActivate, %original_window_title%
		
		return
	}
	
	;Spotify active
	add_song()
	{
		WinGetPos, X, Y, Width, Height, ahk_class SpotifyMainWindow
		MouseClick, right, X+50, Y+Height-70
		Sleep, 50
		ControlSend, ahk_parent, {DOWN}{DOWN}{DOWN}{RIGHT}, ahk_class SpotifyMainWindow
		
		x = 1
		While x < playlist_number
		{
			x++
			ControlSend, ahk_parent, {DOWN}, ahk_class SpotifyMainWindow
		}
		ControlSend, ahk_parent, {ENTER}, ahk_class SpotifyMainWindow
		Sleep, 50
	}
}


genecyber
  • Members
  • 12 posts
  • Last active: Sep 05 2006 11:07 AM
  • Joined: 11 Mar 2006
This is the early stages of my script, I have some other code that can parse the album/artist/track data from the spotify metadata api.

features,
New context menu for Spotify (disabled)
Next Track
Previous Track
Smart hide / show
Url Catcher
Auto Play Playlist
Auto Play caught Urls

;
; AutoHotkey Version: 1.x
; Language:       English
; Platform:       Win9x/NT
; Author:         Shannon Prue <genecyber@gmail.com>
;
; Script Function:
;	Automate Spotify,  folder)
;

#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.
#Persistent
#SingleInstance force
#Include TrayIcon.ahk
#Include COM.ahk

currentlyPlaying := "loading"

  menuCreate()
  hideIcon()
  SetTimer, spotState, 50
  SetTimer, clipMonitor, 50
  return
  


RButton Up::
  if WinActive("ahk_class SpotifyMainWindow")
  
  {
	;This is where a custom context menu can go
      mouseclick, right
      ;Menu, MyMenu, Show
      return
  
  }

  else

  mouseclick, right
return

^f5::
  reload
return

^f4::
  exitapp
return

MenuHandler:
  MsgBox You selected %A_ThisMenuItem% from the menu %A_ThisMenu%.
return

getUid(proc)
{
  DetectHiddenWindows, On
  TI := TrayIcons( proc )
  StringSplit,TIV, TI, |
  uID  := RegExReplace( TIV4, "uID: " )
  Msg  := RegExReplace( TIV5, "MessageID: " )
  hWnd := RegExReplace( TIV6, "hWnd: " )
  idn := RegExReplace(  TIV2, "idn: " )
  return idn
}

menuCreate()
{
  Menu, MyMenu, Add, Item1, MenuHandler
  Menu, MyMenu, Add, Item2, MenuHandler
  Menu, MyMenu, Add  ; Add a separator line.

; Create another menu destined to become a submenu of the above menu.
  Menu, Submenu1, Add, Item1, MenuHandler
  Menu, Submenu1, Add, Item2, MenuHandler

; Create a submenu in the first menu (a right-arrow indicator). When the user selects it, the second menu is 

displayed.
  Menu, MyMenu, Add, My Submenu, :Submenu1

  
  Menu, MyMenu, Add  ; Add a separator line below the submenu.
  Menu, MyMenu, Add, Item3, MenuHandler  ; Add another menu item beneath the submenu.

;Tray Menu
  Menu, Tray, NoStandard
  Menu, Tray, Add, > Next Song, nextSong
  Menu, Tray, Add, < Previous Song, prevSong
  Menu, Tray, Add

  Menu, Tray, Add, >> Next Playlist, nextPlaylist
  Menu, Tray, Add, << Previous Playlist, prevPlaylist
  Menu, Tray, Add
  Menu, Tray, Add, Open Spotify, showSpot
  Menu, Tray, Add, Hide Spotify, hideSpot
  Menu, Tray, Add
  Menu, Tray, Add, Exit Spotify, exitSpot
  return
}

showSpot:
  DetectHiddenWindows, on
	winactivate, ahk_class SpotifyMainWindow
  Process, Exist, spotify.exe
  If (ErrorLevel = 0) ; If it is not running
    {
      run, %A_ProgramFiles%\spotify\spotify.exe
      sleep, 1500
      WinHide, ahk_class SpotifyMainWindow
      sleep, 5000
	ControlSend, ahk_parent, {Tab}{Tab}{Tab}{Down}{Down}{Down}{Down}{Del}{Del}{Del}{Del}{Del}{Del}{Del}

{Enter}, ahk_class SpotifyMainWindow
       hideIcon()
      return
    }
  WinShow, ahk_class SpotifyMainWindow
  WinActivate, ahk_class SpotifyMainWindow
return

hideSpot:
  DetectHiddenWindows, on
  WinHide, ahk_class SpotifyMainWindow
return

exitSpot:
  Process, Close, spotify.exe
return

nextPlaylist:
  WinActivate, ahk_class SpotifyMainWindow
  ControlSend, ahk_parent, {Down}{Enter}, ahk_class SpotifyMainWindow
  WinActivate
return
prevPlaylist:
  WinActivate, ahk_class SpotifyMainWindow
  ControlSend, ahk_parent, {Up}{Enter}, ahk_class SpotifyMainWindow
  WinActivate
return

nextSong:
  WinActivate, ahk_class SpotifyMainWindow
  ControlSend, ahk_parent, ^{Right}, ahk_class SpotifyMainWindow
return

prevSong:
  WinActivate, ahk_class SpotifyMainWindow
  ControlSend, ahk_parent, ^{Left}, ahk_class SpotifyMainWindow
return

hideIcon()
{
  spotifyIconIdn := getUid("spotify.exe")
  hideTrayIcon(spotifyIconIdn, True)
}

spotState:
  DetectHiddenWindows, off
  IfWinExist, ahk_class SpotifyMainWindow
    windowState := "open"
  else
    windowState := "closed"

  DetectHiddenWindows, on
  WinGetTitle, clientTitle, ahk_class SpotifyMainWindow

  menuState(windowState)

  if currentlyPlaying = %clientTitle%
    return
  else
  {
    currentlyPlaying := clientTitle
    StringTrimLeft, currentlyPlayingTrim, currentlyPlaying, 10
    traytip, Now playing:, %currentlyPlayingTrim%., 10 , 16
  }
return

menuState(windowState)
{
  if windowState = closed
    {
      Menu, Tray, Disable, Hide Spotify
      Menu, Tray, Enable, Open Spotify
    }
  else
    {
      Menu, Tray, Enable, Hide Spotify
      Menu, Tray, Disable, Open Spotify
    }
}

clipMonitor:
if RegexMatch(Clipboard,"http://open.spotify.com.*") {
    ; conditions are meet   
	uri := Clipboard
	Clipboard := ""
	gosub,  openSpotUri
	

}
return

openSpotUri:
{
	run, %uri%,,Hide
	sleep, 1000
	gosub, hideSpot
	ControlSend, ahk_parent, {Enter}, ahk_class SpotifyMainWindow
}




  • Guests
  • Last active:
  • Joined: --

Hi!
I modded zzzTAG's latest version of the script and came up with this:

SetTitleMatchMode 2 

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


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

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

; "CTRL + DOWN"  for info 
^Down:: 
{ 
DetectHiddenWindows, On 
SetTitleMatchMode 2 
WinGetTitle, now_playing, ahk_class SpotifyMainWindow 
StringTrimLeft, playing, now_playing, 10 
DetectHiddenWindows, Off 
clipboard = %playing%`r`n
return 
} 

For my needs I removed the tray notification and added global hotkey for play/pause (Ctrl + UP). I've changed the info function (Ctrl + DOWN) so it doesn't show in tray either, but copies the song's artist and title to clipboard so you can paste it wherever you want.

Now playing: Eric Serra – Little Light of Love (End Titles version)
~

So the keys are:
ctrl + left - previous
ctrl + right - next
ctrl + up - play/pause
ctrl + down - info goes to clipboard

Hope somebody will find this useful!

PS. Any ideas how to get album's name?

Thanks :)

morxy49
  • Members
  • 1 posts
  • Last active: Jan 25 2011 08:19 AM
  • Joined: 23 Jan 2011

ok, happy with this for now :D

SetTitleMatchMode 2 

#Persistent 
SetTimer, RefreshTrayTip, 1000 
playingsave := "" 
Gosub, RefreshTrayTip  ; Call it once to get it started right away. 
return 

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


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

; "CTRL + UP"  for info
^Up:: 
{ 
DetectHiddenWindows, On 
SetTitleMatchMode 2 
WinGetTitle, now_playing, ahk_class SpotifyMainWindow 
StringTrimLeft, playing, now_playing, 10 
TrayTip, Now playing:, %playing%., 10 , 16
DetectHiddenWindows, Off 
return 
}

RefreshTrayTip: 
WinGetTitle, now_playing, ahk_class SpotifyMainWindow 
StringTrimLeft, playing, now_playing, 10 
if(playing != playingsave) { 
  TrayTip, Now playing:, %playing%., 10 , 16  
} 
playingsave := playing 
return


How can i make the info balloon pop up every time a new song has started?

EojjN
  • Guests
  • Last active:
  • Joined: --
I don't know if this is possible but can you add a hotkey for Spotify's "Queue" option?

omni
  • Guests
  • Last active:
  • Joined: --
ahk is a powerful tool that can also be used to capture the screen if this interests you more.

Print screen to file and load to gui is the simplest but not most elegant solution to do this, I think.

sumon
  • Moderators
  • 1317 posts
  • Last active: Dec 05 2016 10:14 PM
  • Joined: 18 May 2010
I'm gonna make a Spotify controller script at some time. For now, you'll have to do with this very simple /NP code snippet. Type /NP to get the effect of... Now playing: Spotify - Igor Stravinsky – Symphony Of Psalms: I. Exaudi Oratorionem Mea, Domine

::/np:: 
WinGetTitle, NP, ahk_class SpotifyMainWindow
Send,Now playing: %NP%
; Now playing: Spotify - Movie Sounds Unlimited – Now We Are Free (From Gladiator)

Edit: This might have been linked, but for those that wanna check out an awesome Spotify controller (called Stopify), check this topic: <!-- m -->http://www.autohotke...pic.php?t=66771<!-- m -->

EDIT2: It's done, atleast an initial version: Hotkeys & notifications, in a slim look: <!-- m -->http://www.autohotke...pic.php?t=69653<!-- m -->. Will implement a nicer looking GUI, a visible controller, some feedback on whether a song is playing or not & volume, and album image. Atleast some of them.