简体   繁体   English

使用Applescript检测Spotify曲目的结尾?

[英]Detect the end of a Spotify track with Applescript?

I'm writing an Applescript that displays a notification with the name, album, and artist of the current Spotify track. 我正在编写一个Applescript,它显示一条通知,其中包含当前Spotify曲目的名称,专辑和艺术家。 Yes, I'm aware there is already an app that does this, but even with the latest version, it's very buggy and behaves erratically. 是的,我知道已经有一个应用程序可以执行此操作,但是即使使用最新版本,它也存在很多错误,并且行为异常。

So far, I have a good little script. 到目前为止,我有一个不错的小脚本。 Here it is: 这里是:

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

Basically, the problem with this is if I pause or change the track, delay stays the same and thus doesn't display the notification when the song starts. 基本上,问题是如果我暂停或更改曲目,延迟保持不变,因此在歌曲开始时不显示通知。

I used delay in this script because I don't really know a better way of checking for the end of a track. 我在此脚本中使用了延迟,因为我真的不知道一种检查曲目结束的更好方法。 Is there any Applescript event for the end of a song (or the beginning of a new one?) 歌曲结尾(或新歌曲开头)是否有任何Applescript事件?

Thanks. 谢谢。

PS, If this question is too complicated, feel free to ask more. PS,如果这个问题太复杂,请随时提出更多问题。

Use a loop to check the current track, exit the loop when the song change 使用循环检查当前曲目,当歌曲改变时退出循环

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM