Currently playing song in iTunes on command

Written . Tagged AppleScript, OS X.

[Screenshot]

Though iScrobbler provides me with Growl notifications of track details whenever iTunes starts playing a song, I wanted a way to get those details at any time, on my command.

There is an updated version of this script.

I don’t want to have to switch focus to iTunes just to find out what the current song is, if I forgot to take note of the details as the song started playing.

I ended up writing an AppleScript that collects song details from iTunes, including any album art, and then passes those details to a Growl notification.

I choose to trigger the script with Quicksilver, similar to in a previous post.

The script (download):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
-- The Album art code is from http://www.macosxhints.com/article.php?story=20060406080910401

set theTitle to "Currently playing"

set tempArt to "/tmp/growlart.tiff"
set tempArtPath to POSIX file tempArt
set tempArtFile to tempArtPath as file specification

set doMusic to false
set doArt to false

tell application "iTunes"
  if player state is playing then
    set theArtist to artist of current track
    set theSong to name of current track
    set theAlbum to album of current track
    if (count of artwork of current track) > 0 then -- artwork is present

      do shell script "rm -rf " & tempArt

      set artworkData to data of artwork 1 of current track

      set fileRef to (open for access tempArtPath with write permission)
      try
        set eof fileRef to 512
        write artworkData to fileRef starting at 513
        close access fileRef
      on error errorMsg
        try
          close access fileRef
        end try
        error errorMsg
      end try

      -- Convert to tiff

      tell application "Finder" to set creator type of tempArtPath to "????"

      tell application "Image Events"
        set theImage to open tempArtFile
        save theImage as TIFF in tempArtFile with replacing
      end tell

      set doArt to true

    end if

    set doMusic to true

  end if
end tell

set appName to theTitle
set notification to theTitle
set myAllNotesList to {notification}

tell application "System Events"
   set isRunning to (name of processes) contains "GrowlHelperApp"
end tell

if isRunning then
  tell application "GrowlHelperApp"
    register as application appName  all notifications myAllNotesList  default notifications myAllNotesList  icon of application "iTunes"

    if doMusic
      set theDesc to "Track: " & theSong & "\nAlbum: " & theAlbum & "\nArtist: " & theArtist
      if doArt
        notify with  name notification  title theTitle  description theDesc  application name appName  image from location tempArt
      else
        notify with  name notification  title theTitle  description theDesc  application name appName
      end if
    else
      notify with  name notification  title theTitle  description "Nothing."  application name appName
    end if
  end tell
end if

If anyone knows how to avoid the duplication in the end of the script, please let me know. I’m new to AppleScript.