Spotify 给出当前曲目的错误值 (AppleScript)

Spotify giving wrong value of current track (AppleScript)

我目前正在尝试制作一个应用程序来获取当前在 Spotify 中播放的歌曲的持续时间。为此,我使用 AppleScript。

这是我的代码:

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

这首歌的长度是 2.52 分钟,代码告诉我 172026 秒。

通过查看 return 值,它看起来像是 return 毫秒而不是轨道长度的秒数。我可以通过执行毫秒*1000 轻松解决这个问题。问题是 172026 毫秒不是 2.52 分钟,而是 2.8671 分钟。

我要如何获得歌曲时长的真实值?

这是 Spotify AppleScript 文档:

track n : A Spotify track.
duration (integer, r/o) : The length of the track in seconds.

刚发现我需要做一点数学。

代码如下:

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

提供的代码

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

"inline": "if application \"Spotify\" 是 运行 then\rtell 应用程序\"Spotify\"\rreturn \" \" &(获取当前曲目的艺术家) & \" – \" & (获取当前曲目的名称)\rend tell\rend if\rreturn \"\"\r"

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