Getting song name of streaming track in iTunes with Applescript

I looked in the applescript dictionary for iTunes and searched for "stream"...

tell application "iTunes"
    current stream title
end tell

It appears that, in iTunes 12.2, a whole variety of interesting things are going on.

  1. current stream title returns missing value when requesting the name of a stream coming from "For You" (e.g. something not in your current music library). name of the current track doesn't exist. For example, I'm listening to "Alternative Gems: 1994" from "For You" right now (Yay- grad school days) and I can't get any information about what is playing. If I go to the album the track is playing from to play something else, missing value and error -1728 on name of current track too.

  2. When listening to Beats 1 radio as per @ivan above, I also get missing value but for name of the current track I get "Beats 1". As @dougscripts points out, the stream title stuff varies all over the map.

  3. Listening to a radio station created via a "For You" seems to give me the correct name of the current track.

So, in short, chaos.


Much more hacking and I finally have found a way to get the data directly out from iTunes using the SDK.

This method will check the currentTrack like normal, but when it's detected that the artist is missing (a common understanding when the track is being streamed), we fall down to getting the values from the LCD display using values provided by Accessibility.

#!/usr/bin/env osascript -l JavaScript
/* globals Application */
function main () {
  var itunes = new Application('iTunes')
  var currentTrack = itunes.currentTrack
  var output = {
    name: currentTrack.name(),
    artist: currentTrack.artist(),
    position: itunes.playerPosition()
  }
  if (currentTrack.artist() === '') {
    var app = new Application('System Events')
    var itunesProcess = app.applicationProcesses.byName('iTunes')
    // Get the text values from the first scrollable area which is the LCD Display
    var arr = itunesProcess.windows[0].scrollAreas[0].staticTexts

    output.name = arr[0].name()
    // Clean up the artist name, as it may contain the Show Name. 
    output.artist = arr[2].name().replace(' — ' + currentTrack.name(), '')
  }
  return JSON.stringify(output, null, 2)
}
main()

Example output:

{
    "name": "Wild Smooth (Gundam Radar Rip)",
    "position": "34:06",
    "artist": "MUBLA"
}

Be sure to chmod +x this script.

Note, it requires the calling application to be added to Accessibility Privacy


Not all streamers will format the stream data the same, so results from the current stream title property may not be consistent.