Spotify提供当前曲目的错误值(AppleScript)

时间:2016-07-13 19:58:54

标签: applescript spotify

我目前正在尝试创建一个能够在Spotify中播放当前歌曲持续时间的应用程序。要做到这一点,我使用的是AppleScript。

这是我的代码:

tell application "Spotify"
    return the duration of the current track
end tell

歌曲的长度为2.52分钟,代码告诉我172026秒。

通过查看返回值,它看起来像是返回毫秒而不是轨道长度的秒数。这可以通过毫秒* 1000轻松修复。问题是172026ms不是2.52分钟,而是2.8671分钟。

我的意思是如何获得歌曲持续时间的真正价值?

以下是Spotify AppleScript文档:

  

追踪n:Spotify曲目。
  duration(整数,r / o):以秒为单位的轨道长度。

2 个答案:

答案 0 :(得分:1)

刚发现我需要做一些数学运算。

以下是代码:

tell application "Spotify"
    set tM to round (((duration of current track) / 1000) / 60) rounding down
    set tS to round (((duration of current track) / 1000) mod 60) rounding down
    set myTime to ((tM as text) & "," & tS as text)
    return myTime
end tell

感谢dronir在https://github.com/dronir/SpotifyControl

的代码

答案 1 :(得分:0)

您还可以使用以下代码读取歌曲和歌手的姓名(如果需要):

“ inline”:“如果应用程序\” Spotify \“正在运行,则\ rtell应用程序\” Spotify \“ \ rreturn \” \“&(获取当前曲目的艺术家)&\” – \“&(获取名称的当前曲目)\ rend告诉\ rend \ rreturn \“ \” \ r“

(感谢https://github.com/Toxblh/MTMR/blob/master/Resources/aadi_vs_anand.json的Toxblh)