Get iTunes Streaming Song Title Using Applescript

How can I get the song name of the current track in iTunes when this track is a radio?

I mean the line (s) that appears right under the name of the radio :)

enter image description here

Tapping his name as shown below gives me the name of the radio ( Trance Channel - DIGGITALLY IMPORTED - we can't define it! ), But not the song

 tell application "iTunes" set thisTrack to current track set trackName to the name of thisTrack set trackTime to thisTrack time end tell 

which is expected since the information in my library: enter image description here but is there a way to specifically deal with these streaming tracks? and get their info right, like iTunes does in the first shot? I know that the current track is radio, because its time will be missing value , and not MM:SS form, if that helps a little.

Is it possible?

+8
source share
6 answers

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

 tell application "iTunes" current stream title end tell 
+7
source

ITunes 12.2 seems to have a number of interesting things going on.

  • current stream title returns the missing value when prompted for the name of the stream coming from "For You" (for example, something is not in your current music library). name of the current track does not exist. For example, I listen 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 an album that plays the track in order to play something else, the missing value and error -1728 on the name of current track also.

  • When listening to Beats 1 on @ivan above, I also get the missing value , but for name of the current track I get "Beats 1". As @dougscripts points out, the stream title stuff changes across the map.

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

So, in short, chaos.

+2
source

Not all streamers will format the stream data in the same way, so the results from the current stream header property may not be compatible.

+1
source

A lot more hacking, and I finally found a way to get data directly from iTunes using the SDK.

This method will check currentTrack as usual, but when it detects that artist missing (a general understanding of when a track is being broadcast), we fall for receiving values ​​from the LCD using the 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() 

Output Example:

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

Be sure to run chmod +x this script.

Please note that the calling application is required to be added to Accessibility Privacy.

+1
source

After several weeks, tear his hair out, trying to figure it out on his own. I managed to find a very hacky solution to this problem.

You will not like it, though, be careful.

To get the current playable track, even if the sound is transmitted from Beats radio, you will need to consider the OCR approach (taking a screenshot, converting the image to text).

The following Ruby code will help you get started with this solution.

It will check the current track, and if the artist field is empty (which is the case when streaming a track), then it falls into the OCR method.

 require 'json' require 'rtesseract' class CurrentTrack def self.check js_command = %Q{var itunes = Application("iTunes"); var currentTrack = itunes.currentTrack; JSON.stringify({ window_bounds: itunes.windows[0].bounds(), name: currentTrack.name(), artist: currentTrack.artist(), position: itunes.playerPosition() }) } command = "osascript -l JavaScript -e '#{js_command}'" result = '#{command}' json = JSON.parse(result, symbolize_names: true) json[:position] = json[:position].to_i json[:cue] = Time.at(json[:position]).utc.strftime('%H:%M:%S') if json[:artist] == '' sc_command = %Q{screencapture -R #{json[:window_bounds][:x]},#{json[:window_bounds][:y].to_i + 30},#{json[:window_bounds][:width]},#{json[:window_bounds][:height]} capture.png} '#{sc_command}' image = RTesseract.new("capture.png", processor: 'none') ocr = image.to_s.split("\n") # Getting the value unless ocr.first == 'Soulection' json[:name] = ocr.first json[:artist] = ocr[1].split(' — ').first end end json.delete :window_bounds json end end 

You will need to install rtesseract for this to work.

Caveats, this script requires the iTunes mini-player window to be visible somewhere on your desktop.

0
source

Another option - given the inconsistency of the output - might consist in playing back in VLC and requesting it instead - AppleScript support is pretty limited, but at least the source code is available so you know what you can request.

 osascript -e 'tell application "VLC" to get name of current item' 
0
source

All Articles