【问题标题】:Detect the end of a Spotify track with Applescript?使用 Applescript 检测 Spotify 曲目的结尾?
【发布时间】:2014-11-11 00:12:49
【问题描述】:

我正在编写一个 Applescript,它会显示一条通知,其中包含当前 Spotify 曲目的名称、专辑和艺术家。是的,我知道已经有一个应用程序可以做到这一点,但即使使用最新版本,它也有很多错误并且行为不稳定。

到目前为止,我有一个很好的小脚本。这里是:

repeat until application "Spotify" is not running
    tell application "Spotify"
        set theSong to name of current track
        set theAlbum to album of current track
        set theArtist to artist of current track
        set theTime to player position
        set theDuration to duration of current track
    end tell


    (* This sets the amount of delay to the length of the song minus the current place in the song, which leaves us with the amount of time left until the next song plays.*)
    set delayTime to theDuration - theTime + 0.5

    tell application "Spotify"
        if player state is playing then
            display notification theArtist with title theSong subtitle theAlbum
        end if

        delay delayTime

    end tell
end repeat

基本上,问题是如果我暂停或更改曲目,延迟保持不变,因此在歌曲开始时不会显示通知。

我在这个脚本中使用了延迟,因为我真的不知道检查曲目结尾的更好方法。一首歌的结尾(或一首新歌的开头)是否有任何 Applescript 事件?

谢谢。

P.S.,如果这个问题太复杂,请随时问更多。

【问题讨论】:

  • 有没有人注意到duration of current track 似乎返回的是毫秒而不是秒? AppleScript 文档说“duration (integer, r/o) : The length of the track in seconds.”,但对于 1:19 的轨道,我得到的值为 76553。
  • @bonh,到今天还没有修复。

标签: macos applescript spotify playback


【解决方案1】:

使用循环检查当前曲目,当歌曲改变时退出循环

repeat until application "Spotify" is not running
    tell application "Spotify"
        set theSong to name of current track
        set theAlbum to album of current track
        set theArtist to artist of current track
        set thisTrack to current track
        if player state is playing then
            display notification theArtist with title theSong subtitle theAlbum
            repeat until thisTrack is current track
                delay 3
            end repeat
        end if
    end tell
end repeat

【讨论】:

  • 谢谢你!实际上,我自己想出了如何做到这一点,但我忘记将这个问题标记为已回答。但是,是的,如果我没有想到这一点,这将非常有帮助。
  • 你能发布你的答案吗?和这个一样吗?
猜你喜欢
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 2011-11-30
  • 2020-12-11
  • 2016-11-16
  • 2013-04-01
  • 1970-01-01
  • 2012-09-22
相关资源
最近更新 更多