Jump to content

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

Spotify notification with album art and album name


  • Please log in to reply
14 replies to this topic
kyf
  • Members
  • 16 posts
  • Last active: Sep 26 2011 12:19 PM
  • Joined: 12 Sep 2009
Hi,

I think there is a few scripts up here already that show which artist and track are currently playing in spotify.

However, the following script also gets the album name and artwork from last.fm (i haven't found a way of getting this from spotify directly)

NB: when there isn't artwork available on last.fm it will look for a file called spotify.png in the same directory as the script... I use this image for that purpose:
<!-- m -->http://i35.tinypic.com/2cxx820.png<!-- m -->



run, %A_ProgramFiles%\spotify\spotify.exe
check:= 0
#NoTrayIcon

#Persistent
     SetTimer, Toast, 1000
return 



Title = Spotify


Toast:

DetectHiddenWindows, On

if check < 10
{
check:= check + 1   
} else {
IfWinNotExist, ahk_class SpotifyMainWindow
     {

	 ExitApp
	 }
}
	  

WinGetTitle, text, ahk_class SpotifyMainWindow
StringTrimLeft, text2, text, 10
      test = 0
   IfNotInString, Title, %text2%
     {
	  ;show toast...
      	Title = %text2%  

	  ;split name to artist and track title...
	  StringReplace, text2, text2, &, and
	  split:= " – "
	  StringReplace, text2, text2, %split%, *
	  StringSplit, trackInfo, text2, *
	  
	  ;download info from last.fm
	  tempfile = %temp%\LFinfo
	  urldownloadtofile, http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=%trackInfo1%&track=%trackInfo2%, %tempfile%
	  fileRead, LFinfo, %tempfile%
	  
	  FileDelete, %tempfile%
	  
	 ; get album name	  
	  album:= RegExReplace(LFinfo, ".*<title>(.*)</title>.*","$1")
	   IfInString, album, <?xml
	         album =
	  ; get album art...
	  artURL:= RegExReplace(LFinfo, ".*<image size=""medium"">(.*)</image>.*","$1")
	  artURL:= RegExReplace(artURL, "</image>.*")
	  IfInString, artURL, <?xml
	  {
	          artwork = spotify.png
	  Gui, Add, Picture, x12 y10 w60 h60 , %artwork%      
	  }
	  else	 
      {	  
      urldownloadtofile, %artURL%, %tempfile%.jpg
      artwork = %tempfile%.jpg	  
  	  Gui, Add, Picture, x10 y8 w70 h70 , %artwork%
	  }
	
      	  
	  Gui, Color, 111133
       	  Gui +LastFound  ; Make the GUI window the last found window...
          WinSet, Transparent, 210
	  Gui +AlwaysOnTop
          Gui, +ToolWindow 
	  gui, font, s10 w700 cddddff, Arial Italic
	  Gui, Add, Text, x90 y10 w270 h40 , %trackInfo2%
	  
	  
	  
	  StringLen, len, trackInfo2
	  y1 = 30
	  y2 = 50
	  if (len > 30 )
	  {
      y1 = 45
	  y2 = 62
	  }
	  gui, font, s9 w400, Arial
	  Gui, Add, Text, x90 y%y1% w280 h20 , %trackInfo1%
	  gui, font, s9 w500, Arial italic
	  Gui, Add, Text, x90 y%y2% w270 h20 , %album%
	  Gui -caption
	 
	  Gui, Show, x910 y690 h85 w320 NoActivate , New GUI Window
	 
	  ;--------------------------------------
      sleep, 3000
	  Gui, destroy
	  filedelete, %tempfile%.jpg
      ;Progress, Off
	  
	  }
      
     
DetectHiddenWindows, Off


return


#F4::
 process, close, spotify.exe
ExitApp


OnExit, ExitSub

ExitSub:
   process, close, spotify.exe
return


#a::
DetectHiddenWindows, On
ControlSend, ahk_parent, ^{left}, ahk_class SpotifyMainWindow
DetectHiddenWindows, Off
return



#s::
DetectHiddenWindows, On
ControlSend, ahk_parent, ^{right}, ahk_class SpotifyMainWindow
DetectHiddenWindows, Off
return


#z::
DetectHiddenWindows, On
ControlSend, ahk_parent, {space}, ahk_class SpotifyMainWindow
DetectHiddenWindows, Off
return


a few other things to note:
--running script will open spotify automatically, and script closes itself when spotify is closed
--there are some global hot keys which work even when spotify minimised to tray:
win-z: pause/play
win-a: previous track
win-s: next track
win-F4: close spotify
--I've hided the script icon from the tasktray, but just delete the line
#NoTrayIcon if you want it there


....at the moment it downloads the artwork again everytime you play a track, but going to update my own version to store the images locally[/code]

kelvbran
  • Members
  • 4 posts
  • Last active: Nov 15 2009 07:24 AM
  • Joined: 25 Sep 2009
Hey... this is really great :)

If you can figure out a way to get the http link or spotify URI of the current playing track (without having to activate the spotify window), you could use http://spotify.url.fi/ to get all the track information including cover art.

kyf
  • Members
  • 16 posts
  • Last active: Sep 26 2011 12:19 PM
  • Joined: 12 Sep 2009
Wow... thank you very much for the link kelvbran!
I will have a think about how you might extract the spotify URL

When i have the time i will also look into how you might get images from Amazon (i think thats where some Songbird add-ons get images from)

In the meantime, here is the slighlty updated version i'm using, which accounts for the fact that some last.fm images are in png format, and also saves the images to your hard drive (to a folder called SpotArt) so it doesn't have to keep downloading the same one:
(also win+w shows the notification again if you missed it)


run, %A_ProgramFiles%\spotify\spotify.exe
check:= 0
#NoTrayIcon

#Persistent
     SetTimer, Toast, 1000
return 



Title = Spotify


;====main code: send notification to screen....
Toast:


DetectHiddenWindows, On

;--- close the script with spotify (but not at the start)...
if check < 10
{
ScreenX:= CurrentDisplaySettings(1)
ScreenY:= CurrentDisplaySettings(2)
check:= check + 1   
} else {
IfWinNotExist, ahk_class SpotifyMainWindow
      ExitApp
}


;---get name of artist and track from spotify title...
WinGetTitle, text, ahk_class SpotifyMainWindow
StringTrimLeft, text2, text, 10
	  
   ;---if name has change:	  
   IfNotInString, Title, %text2%
     {
      	Title = %text2%  
	  
	  ;-----split name to artist and track title...
	  StringReplace, text2, text2, &, and
	  split:= " – "
	  StringReplace, text2, text2, %split%, *
  	  StringReplace, text2, text2, ', ,all
	  StringSplit, trackInfo, text2, *
	  
	  ;-----download info from last.fm
	  tempfile = %temp%\LFinfo	  
	  urldownloadtofile, http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=%trackInfo1%&track=%trackInfo2%, %tempfile%
	  fileRead, LFinfo, %tempfile%

	  FileDelete, %tempfile%
	  
      ;----search for album name...	  
	  album:= RegExReplace(LFinfo, ".*<title>(.*)</title>.*","$1")
	   IfInString, album, <?xml
	         album =
	  
	  ;----search for album art...
	  artwork = 0
	  ifExist, SpotArt\%trackInfo1%_%album%.png
	      artwork = SpotArt\%trackInfo1%_%album%.png
  	  ifExist, SpotArt\%trackInfo1%_%album%.jpg
	      artwork = SpotArt\%trackInfo1%_%album%.jpg

       if (	artwork == 0 )
       {
	  ifExist, SpotArt\%trackInfo1%_%trackInfo2%.png
	      artwork = SpotArt\%trackInfo1%_%trackInfo2%.png
  	  ifExist, SpotArt\%trackInfo1%_%trackInfo2%.jpg
	      artwork = SpotArt\%trackInfo1%_%trackInfo2%.jpg
       }	   
	  
	  
	  if (artwork == 0)
	  {
	  FoundPos := RegExMatch(LFinfo,"<image size=""medium"">")
	  
	  if ( FoundPos == 0) ;---if none found, use spotify logo
	  {
	      artwork = spotify.png
	      Gui, Add, Picture, x12 y10 w60 h60 , %artwork%      
	  }
	  else	 
      {
      artURL:= RegExReplace(LFinfo, ".*<image size=""medium"">(.*)</image>.*","$1")
	  artURL:= RegExReplace(artURL, "</image>.*")	  
	    
		if (album == )
		    artPath = SpotArt\%trackInfo1%_%trackInfo2%
	    else
		    artPath = SpotArt\%trackInfo1%_%album%
		
	  ifInString, artURL, png
      {	  	  
	     urldownloadtofile, %artURL%, %artPath%.png
		 artwork = %artPath%.png
	  }
	  else
	  {
	     urldownloadtofile, %artURL%, %artPath%.jpg
         artwork = %artPath%.jpg	  
  	  }
	  Gui, Add, Picture, x10 y8 w70 h70 , %artwork%
	  }
	  
	  }
	  else
	  {
	  Gui, Add, Picture, x10 y8 w70 h70 , %artwork%
	  }
      	  
	  Gui, Color, 111133
	  Gui +LastFound  ; Make the GUI window the last found window for use by the line below.
      WinSet, Transparent, 210
	  Gui +AlwaysOnTop +border
	  Gui, +ToolWindow 
	  gui, font, s10 w700 cddddff, Arial Italic
	  Gui, Add, Text, x90 y10 w270 h40 , %trackInfo2%
	  
	  
	  
	  StringLen, len, trackInfo2
	  y1 = 30
	  y2 = 50
	  if (len > 30 )
	  {
      y1 = 45
	  y2 = 62
	  }
	  gui, font, s9 w400, Arial
	  Gui, Add, Text, x90 y%y1% w280 h20 , %trackInfo1%
	  gui, font, s9 w500, Arial italic
	  Gui, Add, Text, x90 y%y2% w270 h20 , %album%
	  Gui -caption
	 
	  SX:= ScreenX -370
	  SY:= ScreenY -110
	  Gui, Show, x%SX% y%SY% h85 w320 NoActivate , New GUI Window
	 
	 
	  sleep, 3000
	  Gui, Cancel
	  ;filedelete, %tempfile%.jpg
      	 
     }
	 ; end of IfNotInString, Title, %text2%
     
	 DetectHiddenWindows, Off

return



;--------------Global hotkeys....
#F4::
 process, close, spotify.exe
ExitApp


OnExit, ExitSub

ExitSub:
   process, close, spotify.exe
return


#a::
DetectHiddenWindows, On
ControlSend, ahk_parent, ^{left}, ahk_class SpotifyMainWindow
DetectHiddenWindows, Off
return


#s::
DetectHiddenWindows, On
ControlSend, ahk_parent, ^{right}, ahk_class SpotifyMainWindow
DetectHiddenWindows, Off
return


#z::
DetectHiddenWindows, On
ControlSend, ahk_parent, {space}, ahk_class SpotifyMainWindow
DetectHiddenWindows, Off
return

#w::
	  Gui, Show, x910 y690 h85 w320 NoActivate , New GUI Window
	  sleep, 3000
	  Gui, Cancel
return


;---get screen resolution....
CurrentDisplaySettings(in=0) {   ; 1=width   2=height   3=quality   4=frequency
VarSetCapacity(device_mode,156,0),NumPut(156,2,&device_mode,36)
success := DllCall("EnumDisplaySettings","uint",0,"uint",-1,"uint",&device_mode)
Out_1:=NumGet(&device_mode,108,"uint4")   ;width
Out_2:=NumGet(&device_mode,112,"uint4")   ;height
Out_3:=NumGet(&device_mode,104,"uint4")   ;quality
Out_4:=NumGet(&device_mode,120,"uint4")   ;frequency
if in = 0
Return Out_1 "/" Out_2 "/" Out_3 "/" Out_4
else Return (Out_%in%)
}


kyf
  • Members
  • 16 posts
  • Last active: Sep 26 2011 12:19 PM
  • Joined: 12 Sep 2009
Mmm... Sending ctrl-c to spotify gets the URL of the selected track, but not the one currently playing :?

...a little more thought needed

kelvbran
  • Members
  • 4 posts
  • Last active: Nov 15 2009 07:24 AM
  • Joined: 25 Sep 2009
Well, double clicking on the cover art goes to the playing track, or selecting go to from the cover art context menu... don't know if there's a way to do that without activating spotify.

kyf
  • Members
  • 16 posts
  • Last active: Sep 26 2011 12:19 PM
  • Joined: 12 Sep 2009
Ok, here's version 3...

Thanks kelvbran!! it is possible to send a click, without the window being brought forward:

YY:= ScreenY - 150
Controlclick, x100 y%YY%, ahk_class SpotifyMainWindow
YY is a guess at where the cover art is, may not work on all resolutions but works on 1280x800

So this now get the correct info from <!-- m -->http://spotify.url.fi<!-- m -->
however i've left in a subroutine to get info from last.fm if <!-- m -->http://spotify.url.fi/<!-- m --> is down (as it was while coding)

There is one problem!! it doesn't distinguish between adverts so when an advert is playing it will click on it and open the corresponding website or itunes :roll:

There are some possible solutions
--get a premium account ;)
--find some way of suppressing a webpage being openned
--dectect an advert, and don't click when one is playing

This last one is possible, as there are other scripts on here that detect (and then block) adverts.... but i think i'd prefer the 2nd option if its possible


run, %A_ProgramFiles%\spotify\spotify.exe
check:= 0
#NoTrayIcon

#Persistent
     SetTimer, Toast, 1000
return 



Title = Spotify


;====main code: send notification to screen....
Toast:


DetectHiddenWindows, On

;--- close the script with spotify (but not at the start)...
if check < 10
{
ScreenX:= CurrentDisplaySettings(1)
ScreenY:= CurrentDisplaySettings(2)
check:= check + 1   
} else {
IfWinNotExist, ahk_class SpotifyMainWindow
      ExitApp
}


;---get name of artist and track from spotify title...
WinGetTitle, text, ahk_class SpotifyMainWindow
StringTrimLeft, text2, text, 10
	  
   ;---if name has change:	  
   IfNotInString, Title, %text2%
     {
      	Title = %text2%  
	  
      Gosub, GetSpotifyURI  ;subroutine to get spotify URI and download info from http://spotify.url.fi/
	  
	  if URIerror   ;if http://spotify.url.fi/ down, get from last.fm
	     Gosub, getLastfm
	  
	  Gui, Add, Picture, x10 y8 w70 h70 ,%albumArt%
	  
	  ifInString, albumArt, spotify
	     {
		 Gui, destroy
	     Gui, Add, Picture, x10 y8 w60 h60 ,%albumArt%
	     }
	  
      	  
	  Gui, Color, 111133
	  Gui +LastFound  ; Make the GUI window the last found window for use by the line below.
      WinSet, Transparent, 210
	  Gui +AlwaysOnTop +border
	  Gui, +ToolWindow 
	  gui, font, s10 w700 cddddff, Arial Italic
	  Gui, Add, Text, x90 y10 w270 h40 , %trackName%
	  
	  
	  
	  StringLen, len, trackInfo2
	  y1 = 30
	  y2 = 50
	  if (len > 30 )
	  {
      y1 = 45
	  y2 = 62
	  }
	  gui, font, s9 w400, Arial
	  Gui, Add, Text, x90 y%y1% w280 h20 , %artistName%
	  gui, font, s9 w500, Arial italic
	  Gui, Add, Text, x90 y%y2% w270 h20 , %albumName%
	  Gui -caption
	 
	  SX:= ScreenX -370
	  SY:= ScreenY -110
	  Gui, Show, x%SX% y%SY% h85 w320 NoActivate , New GUI Window
	 
	 
	  sleep, 3000
	  Gui, Cancel
	  ;filedelete, %tempfile%.jpg
      	 
     }
	 ; end of IfNotInString, Title, %text2%
	 
     filedelete, %tempfile%
	 DetectHiddenWindows, Off

return



;--------------Global hotkeys....
#F4::
 process, close, spotify.exe
ExitApp


OnExit, ExitSub

ExitSub:
   process, close, spotify.exe
return


#a::
DetectHiddenWindows, On
ControlSend, ahk_parent, ^{left}, ahk_class SpotifyMainWindow
DetectHiddenWindows, Off
return


#s::
DetectHiddenWindows, On
ControlSend, ahk_parent, ^{right}, ahk_class SpotifyMainWindow
DetectHiddenWindows, Off
return


#z::
DetectHiddenWindows, On
ControlSend, ahk_parent, {space}, ahk_class SpotifyMainWindow
DetectHiddenWindows, Off
return

#w::
	  Gui, Show, x910 y690 h85 w320 NoActivate , New GUI Window
	  sleep, 3000
	  Gui, Cancel
return


;---get screen resolution....
CurrentDisplaySettings(in=0) {   ; 1=width   2=height   3=quality   4=frequency
VarSetCapacity(device_mode,156,0),NumPut(156,2,&device_mode,36)
success := DllCall("EnumDisplaySettings","uint",0,"uint",-1,"uint",&device_mode)
Out_1:=NumGet(&device_mode,108,"uint4")   ;width
Out_2:=NumGet(&device_mode,112,"uint4")   ;height
Out_3:=NumGet(&device_mode,104,"uint4")   ;quality
Out_4:=NumGet(&device_mode,120,"uint4")   ;frequency
if in = 0
Return Out_1 "/" Out_2 "/" Out_3 "/" Out_4
else Return (Out_%in%)
}


GetSpotifyURI:
;---get spotify URI      
      YY:= ScreenY - 150
      Controlclick, x100 y%YY%, ahk_class SpotifyMainWindow
      sleep, 100
      ControlSend, ahk_parent, ^c, ahk_class SpotifyMainWindow
      ClipWait
	  StringTrimLeft, SpotURI, Clipboard, 23
	  SpotURI = http://spotify.url.fi%SpotURI%
	  tempfile = %temp%\SPinfo
	  ;---download info....
	  urldownloadtofile, %SpotURI%, %tempfile%
	  ;---get artist, album, track, 
	  FileReadLine, LineIn, %tempfile%, 7
	  if ErrorLevel
	     URIerror = 1
	  else
	  {
	  LineIn:= RegExReplace(LineIn,".*<title>(.*)</title>.*","$1")
	  URIerror = 0
	  split = —
	  StringReplace, trackInfo, LineIn, %split%, *, all
	  	  StringSplit, trackInfo,trackInfo , *
	  StringTrimLeft, trackInfo2, trackInfo2, 1
	  StringTrimLeft, trackInfo3, trackInfo3, 1	  
	  ;---get album info (to get artwork link)
	  FileRead, LineIn, %tempfile%
	  AlbumURL:= RegExReplace(LineIn,".*<span>Album</span> <a href=""(.*)<span>Year.*","$1")
	  AlbumURL:= RegExReplace(AlbumURL,""">.*")
	  
      urldownloadtofile, %AlbumURL%, %tempfile%
      FileRead, LineIn, %tempfile%
	  
	  ;---get artwork
      filedelete, %tempfile%.jpg
  	  ArtURL:= RegExReplace(LineIn,".*<p><img src=""(.*)"" alt=""Album cover.*","$1")
	  urldownloadtofile, http://spotify.url.fi%ArtURL%, %tempfile%.jpg
	  
	  artistName = %trackInfo1%
	  albumName = %trackInfo2%
	  trackName = %trackInfo3%
	  albumArt = %tempfile%.jpg
	  }
return

getLastfm:
;-----split name to artist and track title...
	  StringReplace, text2, text2, &, and
	  split:= " – "
	  StringReplace, text2, text2, %split%, *
  	  StringReplace, text2, text2, ', ,all
	  StringSplit, trackInfo, text2, *
	  
	  ;-----download info from last.fm
	  tempfile = %temp%\LFinfo	  
	  urldownloadtofile, http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=b25b959554ed76058ac220b7b2e0a026&artist=%trackInfo1%&track=%trackInfo2%, %tempfile%
	  fileRead, LFinfo, %tempfile%

	  FileDelete, %tempfile%
	  
      ;----search for album name...	  
	  album:= RegExReplace(LFinfo, ".*<title>(.*)</title>.*","$1")
	   IfInString, album, <?xml
	         album =
	  
	  ;----search for album art...
	   
	  FoundPos := RegExMatch(LFinfo,"<image size=""medium"">")
	  
	  if ( FoundPos == 0) ;---if none found, use spotify logo
	  {
	      albumArt = spotify.png     
	  }
	  else	 
      {
      artURL:= RegExReplace(LFinfo, ".*<image size=""medium"">(.*)</image>.*","$1")
	  artURL:= RegExReplace(artURL, "</image>.*")	  
	    
	  ifInString, artURL, png
      {	  	  
	     urldownloadtofile, %artURL%, %tempfile%.png
		 albumArt = %tempfile%.png
	  }
	  else
	  {
	     urldownloadtofile, %artURL%, %tempfile%.jpg
         albumArt = %tempfile%.jpg	  
  	  }
	  
	  }
	  
	  artistName = %trackInfo1%
	  albumName = %album%
	  trackName = %trackInfo2%
return


kelvbran
  • Members
  • 4 posts
  • Last active: Nov 15 2009 07:24 AM
  • Joined: 25 Sep 2009

--find some way of suppressing a webpage being openned

did you figure out any way to do this?

also... if we're already on the subject of doing things without activating the spotify window...
I created shortcuts keys for skipping forward/backward in the song. This works by activating the window ImageSearch-ing for that little circle in the timebar and clicking a bit ahead/before it.
Is there any way of doing *this* without activating the window? or if you have some better way of skipping that would be cool(er)

frax
  • Members
  • 10 posts
  • Last active: Apr 14 2014 08:58 AM
  • Joined: 24 Jul 2007
I found out a way to supress the web page opening for ads - I set up a new user, and for that user I set a dummy default browser.

Then, using my normal user/login/session, I use runas or psexec to start Spotify under the new user credentials. No browser opening!

This script worked very neatly by the way, but I don't like that it fills the clipboard with the spotify links - so I stopped using it...

Ruso
  • Guests
  • Last active:
  • Joined: --

This script worked very neatly by the way, but I don't like that it fills the clipboard with the spotify links - so I stopped using it...


That problem has an easy solution: you just have to save the contents of the clipboard to a variable before using it and replace them in the clipboard once finished. This way, you'll only have problems if you try to copy/paste in between, which is unlikely.

frax
  • Members
  • 10 posts
  • Last active: Apr 14 2014 08:58 AM
  • Joined: 24 Jul 2007
Unfortunately it is not that simple. I use a clipboard management tool (ArsClip) and I frequently go back and re-use things on my clipboard - and this would mess up my history. Maybe if one could suspend ArsClip while one replaces things, maybe... ?

frax
  • Members
  • 10 posts
  • Last active: Apr 14 2014 08:58 AM
  • Joined: 24 Jul 2007
ok, now I've got it, after some thinking I thought - why not get all the information from last.fm in one go, instead of goofing around?

Since spotify is scrobbling the playing track quite quickly after it has started, I just do an api request to last.fm and get the artist, trackname, album and albumart. The album and albumart are not always correct, but I think it is good enough.

I also added a little check to see if the artist from the last.fm matches the title bar, and if it doesn't, it just displays the titlebar info - since ads don't get scrobbled.

Feels kind of neat and clean to not have to mess with dirty hacks in the spotify user interface!

Deph
  • Members
  • 5 posts
  • Last active: Dec 13 2010 12:19 PM
  • Joined: 25 Oct 2010
i would be happy to use this if it didn't use the clipboard to get the info, there should be another way to do this then using the clipboard..

a script should never use the clipboard to "cache" things.
also, if u fix this, i might add it to my spotify music controller(i'm planning on releasing it here), as i'm using a traytip to show the current song right now.

atomh
  • Members
  • 45 posts
  • Last active: Mar 10 2012 03:55 AM
  • Joined: 17 Feb 2010
This is a great script, but agree that it would be better if you didn't have to use clipboard hooks.
One suggestion is that Spotify seems to natively send track info to MSN Messenger which can be intercepted like in the ListeningNowTracker
application.
Perhaps this script could be modified to grab that info from there instead of the clipboard monitoring?
From here:
<!-- m -->http://listeningnowt... ... README.TXT<!-- m -->

he mentions:
(1) Spotify starts to play a new song
- Spotify sends out WM_COPYDATA event to a window registered as "MsnMsgrUIManager" class.
- The event has 0x547 identifier number and data block uses following format
\0Music\0status\0format\0song\0artist\0album\0

Null terminated string values with "\0Music\0" prefix tag followed by
status: 1 = Playing, 0 = Stopped or paused
format: ? (no idea what this is in Spotify's case)
song: Title of the song
artist: Name of the artist
album: Name of the album

(2) MSN-Messenger listens "WM_COPYDATA, 0x547" event and updates its "Now playing" status info
whenever this message is received.


Can anybody assist making this code to do the intereception of the event?

Deph
  • Members
  • 5 posts
  • Last active: Dec 13 2010 12:19 PM
  • Joined: 25 Oct 2010
bump...

compujosh
  • Members
  • 1 posts
  • Last active: Nov 01 2014 08:57 AM
  • Joined: 28 Oct 2014

Thanks for making this guys; I modified it so it would just get the art and place it somewhere I could access. The main changes include removing toast and hotkeys, and replacing the broken site with the Spotify API.

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

;----Settings----

#NoTrayIcon ;Comment out to show program in tray (good for testing)

;Fetch Album Art
FetchImages = 1
ImageDest = spotify.png
SpotifyLocation = C:\Users\Josh\AppData\Roaming\Spotify\spotify.exe
DefaultImg = default.jpg ;Image to use if none is found. Leave blank to delte the previous image instead

;Toast Notifications
EnableToast = 0 ;Todo: readd

;Enable Hotkeys
EnableHotkeys = 0 ;Todo: readd

;Optional: fallback to last.fm if art not found
Enable_LF = 1;
LF_ImageDest = spotify.png ;Destination for Last.fm image
LF_APIkey = ;Insert your Last.fm API key here, you can get it from here: http://www.last.fm/api/account/create

;Destination for temporary art & results
temp = C:\Users\Josh\AppData\Local\Temp

;----End of Settings----

Debug = 0

Title := "Spotify"

;Menu, tray, add, Reload, reload
Menu, tray, Tip, Album Art Fetcher

#Persistent
    SetTimer, run, 3000
return

run:

DetectHiddenWindows, On

;--- Make sure Spotify is open
IfWinNotExist, ahk_class SpotifyMainWindow
{
    sleep 5000
    IfWinNotExist, ahk_class SpotifyMainWindow
    {
        run %SpotifyLocation%
    }
}

;---get name of artist and track from spotify title...
WinGetTitle, text, ahk_class SpotifyMainWindow
StringTrimLeft, text2, text, 10

;---if name has change:      
IfNotInString, Title, %text2%
{
    Title = %text2%
    
    ;-----split name to artist and track title...
    StringReplace, text2, text2, &, and
    split:= " – "
    StringReplace, text2, text2, %split%, *
    StringReplace, text2, text2, ', ,all
    StringSplit, trackInfo, text2, *
    
    if (ImageDest <> "")
        result := getSpotifyAPI()

    if (LF_ImageDest <> "" && result == "" && LF_APIkey <> "")
        result := getLastfm()
}

DetectHiddenWindows, Off

return

getSpotifyAPI() {
    global ImageDest,trackInfo1,trackInfo2,Debug,temp,DefaultImg,Enable_LF

    ;-----download info from last.fm
    tempfile = %temp%\LFinfo
    ;Searching for track and artist works best even though result is a track. Also it was parsing backwards so limit=1 helps there
    urldownloadtofile, https://api.spotify.com/v1/search?q=%trackInfo2% %trackInfo1%&type=track&limit=1, %tempfile%
    fileRead, LFinfo, %tempfile%

    ;----search for album name...      
    album := RegExReplace(LFinfo, ".*""name"" : ""([A-Za-z0-9 ]{5,20})""`,\s{0,20}""type"" : ""album""`,.*","$1", , ,-1)
    IfInString, album, `n
        album := ""
    
    ;Debugging
    if (Debug > 1) {
        FileAppend, % LFinfo . "`n-------------------------------`n", LFinfo.txt
        run LFinfo.txt
    }
    
    ;----search for album art...
    ;sizes are 640, 300, 64. 300 is probably most widely available and useful
    FoundPos := RegExMatch(LFinfo, """url"" : ""(.{15,70})""`,\s{0,20}""width"" : 300",artURL)
    artURL := artURL1
    
    IfInString, artURL, `n
        artURL := ""
        
    if ( FoundPos == 0 || artURL == "") ;---if none found
    {
        ;Delete image
        if (DefaultImg <> "" && Enable_LF <> 1) {
            albumArt = %DefaultImg%
            FileCopy %DefaultImg%, %ImageDest%, 1
        }
        else if (Enable_LF <> 1){
            FileDelete %ImageDest%
        }
    }
    else
    {
        urldownloadtofile, %artURL%, %tempfile%.png
        albumArt = %tempfile%.png
        FileCopy %tempfile%.png, %ImageDest%, 1
    }

    FileDelete, %tempfile%

    artistName = %trackInfo1%
    albumName = %album%
    trackName = %trackInfo2%
    
    if (Debug > 1)
        TrayTip, , Art result: %artURL%
    
    return %artURL%
}

getLastfm() {
    global LF_ImageDest,LF_APIkey,trackInfo1,trackInfo2,Debug,temp,DefaultImg
    
    ;Debugging
    if (Debug == 1) {
        TrayTip, , Fetching art from Last.fm
    }
    
    ;-----download info from last.fm
    tempfile = %temp%\LFinfo
    urldownloadtofile, http://ws.audioscrobbler.com/2.0/?method=track.getinfo&api_key=%LF_APIkey%&artist=%trackInfo1%&track=%trackInfo2%, %tempfile%
    fileRead, LFinfo, %tempfile%

    ;----search for album name...      
    album:= RegExReplace(LFinfo, ".*<title>(.*)</title>.*","$1")
    IfInString, album, <?xml
        album = ""
    
    ;----search for album art...

    FoundPos := RegExMatch(LFinfo,"<image size=""medium"">")
    artURL := ""

    if ( FoundPos == 0) ;---if none found, delete image
    {
        ;Comment out this line and uncomment the following two if you want to use a default icon instead
        if (DefaultImg <> "") {
            albumArt = spotify.ico
            FileCopy spotify.ico, %LF_ImageDest%, 1
        }
        else {
            FileDelete %LF_ImageDest%
        }
    }
    else    
    {
        artURL:= RegExReplace(LFinfo, ".*<image size=""medium"">(.*)</image>.*","$1")
        artURL:= RegExReplace(artURL, "</image>.*")

        ifInString, artURL, png
        {          
            urldownloadtofile, %artURL%, %tempfile%.png
            albumArt = %tempfile%.png
            FileCopy %tempfile%.png, %LF_ImageDest%, 1
        }
        else
        {
            urldownloadtofile, %artURL%, %tempfile%.jpg
            albumArt = %tempfile%.jpg
            FileCopy %tempfile%.jpg, %LF_ImageDest%, 1
        }

    }

    FileDelete, %tempfile%

    artistName = %trackInfo1%
    albumName = %album%
    trackName = %trackInfo2%
    
    if (Debug > 1)
        TrayTip, , LFM result: %artURL%
    
    return %artURL%
}

reload:
    reload
return


I've been using this to provide the art for the Enigma music player for Rainmeter. If anyone's interested in doing that you'll have to change the Music.inc file under Skins\Enigma\Sidebar\Music

Here's the new relevent part of that file:

[Icon]
Meter=IMAGE
MeterStyle=StyleMusic#Variant#Icon
ImageName=FULL_IMAGE_PATH_HERE
DynamicVariables=1
;MeasureName=MeasureCover
LeftMouseUpAction=!CommandMeasure "MeasureMediaPlayer" "OpenPlayer"
ToolTipTitle=Open Player

I also found Toastify, which has a similar functionality to the original script: https://toastify.codeplex.com/