Spotify info on the Desktop via NerdTool
July 14, 2011 at 9:43 PM by Dr. Drang
So, like all the other cool internet kids (in the US), you went off and got yourself a Spotify account today. Me too. I don’t see it replacing iTunes on my office computer, mainly because I hate making playlists and don’t know how to get Spotify to do the equivalent of iTunes’ smart playlists (or Pandora’s “music DNA” thing). I will give it a try on my Macbook Air, though, because its “hard drive” is too small to keep an iTunes library on.
One thing I was curious about was whether the Spotify application for the Mac had a decent AppleScript library, because I’d want to have the current track information appearing on my Desktop, just as I have for iTunes at work. As it happens, Spotify has an excellent AppleScript library that mirrors the relevant commands of the iTunes AS library. So I set about making a set of scripts I could have NerdTool run to show the track and album art on my Desktop.
It took about ten minutes to write the scripts. Well, write may be too strong a word for what I did. Basically, I made copies of the track info and album art scripts I wrote for iTunes and modified them to work with Spotify. I wouldn’t say the modifications were simply a matter of s/iTunes/Spotify/g
, but it wasn’t much more complicated than that.
Here’s the script that prints the artist, track, and album info. I call it spotify-playing.scpt
and keep it in my ~/bin
directory.
1: on run
2: set info to ""
3: tell application "System Events"
4: set num to count (every process whose name is "Spotify")
5: end tell
6: if num > 0 then
7: tell application "Spotify"
8: if player state is playing then
9: set who to artist of current track
10: set what to name of current track
11: set onwhat to album of current track
12: set info to "“" & what & "”" & " by " & who & "
13: " & "from " & onwhat
14: end if
15: end tell
16: end if
17: return info
18: end run
I have NerdTool run it every 10 seconds through the command
osascript ~/bin/spotify-playing.scpt
Here’s the script for getting the album artwork from Spotify and saving it to an image file that NerdTool can access and display. It’s called spotify-art.scpt
and is also kept in ~/bin
.
1: -- Paths and stuff
2: set ArtworkFromSpotify to ((path to home folder) as text) & ¬
3: "Pictures:Spotify Artwork:From Spotify:albumArt.tiff" as alias
4: set SpotifyArtwork to ((path to home folder) as text) & ¬
5: "Pictures:Spotify Artwork:From Spotify:albumArt.tiff"
6: set DefaultArtwork to ((path to home folder) as text) & ¬
7: "Pictures:Spotify Artwork:Default:albumArt.tiff"
8: set displayArtwork to ((path to home folder) as text) & ¬
9: "Pictures:Spotify Artwork:albumArt.tiff"
10:
11: -- Unix versions of the above path strings
12: set unixSpotifyArtwork to the quoted form of POSIX path of SpotifyArtwork
13: set unixDefaultArtwork to the quoted form of POSIX path of DefaultArtwork
14: set unixDisplayArtwork to the quoted form of POSIX path of displayArtwork
15:
16: set whichArt to "blank"
17: tell application "System Events"
18: if exists process "Spotify" then -- Spotify is running
19: tell application "Spotify"
20: if player state is playing then -- Spotify is playing
21: set aTrack to current track
22: set aTrackArtwork to null
23:
24: set aTrackArtwork to artwork of aTrack
25: set fileRef to ¬
26: (open for access ArtworkFromSpotify with write permission)
27: try
28: write aTrackArtwork to fileRef
29: close access fileRef
30: on error errorMsg
31: try
32: close access fileRef
33: end try
34: error errorMsg
35: end try
36:
37: tell application "Finder" to ¬
38: set creator type of ArtworkFromSpotify to "????"
39:
40: set whichArt to "Spotify"
41: end if
42: end tell
43: end if
44: end tell
45:
46: if whichArt is "Spotify" then
47: do shell script "ditto -rsrc " & unixSpotifyArtwork & space & unixDisplayArtwork
48: else
49: do shell script "ditto -rsrc " & unixDefaultArtwork & space & unixDisplayArtwork
50: end if
This script assumes I have a set of nested folders in my ~/Pictures
folder arranged this way.
NerdTool displays the albumArt.tiff
file in the Spotify Artwork
directory. The contents of that file are controlled by spotify-art.scpt
and depend on whether Spotify is playing or not.
If Spotify is playing, the script
- saves the track’s album art in the
From Spotify
subfolder (Lines 24-29), and - copies the image file in
From Spotify
toSpotify Artwork
(Line 47).
If Spotify isn’t playing, the script copies the image file in Default
to Spotify Artwork
(Line 49). The image in Default
is a 1×1 transparent TIFF, so if Spotify isn’t playing the Desktop shows through.
I have NerdTool run this script every 10 seconds through the command
osascript ~/bin/spotify-art.scpt
The Spotify people deserve praise for including a complete and useful set of AppleScript commands. Cross-platform applications often leave that out.