【问题标题】:Spotify giving wrong value of current track (AppleScript)Spotify 给出当前曲目的错误值(AppleScript)
【发布时间】:2016-11-16 13:42:33
【问题描述】:

我目前正在尝试制作一个应用程序来获取当前歌曲在 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 文档:

track n:Spotify 曲目。
duration (integer, r/o) : 曲目的长度,以秒为单位。

【问题讨论】:

    标签: applescript spotify


    【解决方案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 提供的代码

    【讨论】:

      【解决方案2】:

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

      "inline": "if application \"Spotify\" is running then\rtell application \"Spotify\"\rreturn \" \" & (获取当前曲目的艺术家) & \" – \" & (获取名称当前曲目的)\rend tell\rend if\rreturn \"\"\r"

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

      【讨论】:

        猜你喜欢
        • 2012-09-22
        • 2012-07-21
        • 2016-02-26
        • 2011-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-22
        相关资源
        最近更新 更多