使用Applescript在iTunes中获取流媒体曲目的歌曲名称

问题描述 投票:8回答:6

当此曲目是收音机时,如何在iTunes中获取当前曲目的歌曲名称?

我的意思是出现在收音机名称正下方的字符串:)

查询下面的名字给了我收音机的名字(qazxsw poi)但不是歌曲

Trance Channel - DIGGITALLY IMPORTED - we can't define it!

这是预期的,因为我的库中的信息是:但有没有办法专门处理这个流媒体轨道?并像第一张图片中的iTunes一样正确获取他们的信息?我知道当前的轨道是收音机,因为它的时间将是tell application "iTunes" set thisTrack to current track set trackName to the name of thisTrack set trackTime to thisTrack's time end tell 而不是missing value的形式,如果这有点帮助。

这可能吗?

macos applescript itunes
6个回答
8
投票

我查看了苹果脚本词典中的iTunes并搜索了“流”...

MM:SS

2
投票

看来,在iTunes 12.2中,各种各样有趣的事情正在发生。

  1. tell application "iTunes" current stream title end tell 在请求来自“For You”的流的名称时返回current stream title(例如,当前音乐库中没有的内容)。 missing value不存在。例如,我现在正在听“For You”中的“Alternative Gems:1994”(Yay- grad school days),我无法获得有关播放内容的任何信息。如果我去专辑那个曲目正在播放其他东西,name of the current trackmissing value上的错误-1728。
  2. 按照上面的@ivan收听Beats 1收音机时,我也得到了name of current track,但是对于missing value,我得到了“Beats 1”。正如@dougscripts指出的那样,name of the current track的东西在地图上各不相同。
  3. 听一个通过“For You”创建的广播电台似乎给了我正确的stream title

所以,总之,混乱。


1
投票

并非所有的流式传输器都会将流数据格式化为相同,因此当前流标题属性的结果可能不一致。


1
投票

更多黑客攻击,我终于找到了一种使用SDK直接从iTunes获取数据的方法。

这个方法会像正常一样检查name of the current track,但是当检测到currentTrack丢失时(在流式传输轨道时常见的理解),我们会使用辅助功能提供的值从LCD显示屏中获取值。

artist

示例输出:

#!/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()

一定要{ "name": "Wild Smooth (Gundam Radar Rip)", "position": "34:06", "artist": "MUBLA" } 这个脚本。

注意,它需要将调用应用程序添加到chmod +x


0
投票

经过数周的撕扯我的头发试图弄清楚自己。我已经设法找到一个非常hacky解决这个问题。

你不会喜欢它,请注意。

为了获得当前播放曲目,即使音频是从Beats收音机流式传输的,您还是必须考虑采用OCR方法(采用屏幕截图,将图像转换为文本)

以下Ruby代码将帮助您使用此解决方案。

它将检查当前曲目,如果Accessibility Privacy字段为空白(在流式传输轨道时就是这种情况),那么它将落入OCR方法。

artist

您需要安装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 才能实现此功能。

注意事项,此脚本要求iTunes迷你播放器窗口在桌面上的某个位置可见。


0
投票

另一个选择 - 考虑到输出的不一致 - 可能是在rtesseract中回放并查询 - AppleScript支持非常有限,但至少VLC可用,因此您知道可以查询的内容。

the source code
© www.soinside.com 2019 - 2024. All rights reserved.