监控Applescript中的Spotify轨迹更改?

监控Applescript中的Spotify轨迹更改?,applescript,growl,spotify,Applescript,Growl,Spotify,我正试图通过Spotify的Applescript库找出检测音轨变化的最佳方法。到目前为止,我已经试着检查玩家的位置——如果它等于0,这是一个新的曲目,咆哮通知再次出现。(如果有人重新开始一首歌,这通常不起作用,等等) 我想知道是否更合理的方法是让一个空闲的iTunes脚本运行,并每隔几秒钟检查一次当前曲目名称的更改。我担心这可能是一个有点猪记忆明智。我也不能让这个代码正常工作 tell application "System Events" -- checks for instance

我正试图通过Spotify的Applescript库找出检测音轨变化的最佳方法。到目前为止,我已经试着检查玩家的位置——如果它等于0,这是一个新的曲目,咆哮通知再次出现。(如果有人重新开始一首歌,这通常不起作用,等等)

我想知道是否更合理的方法是让一个空闲的iTunes脚本运行,并每隔几秒钟检查一次当前曲目名称的更改。我担心这可能是一个有点猪记忆明智。我也不能让这个代码正常工作

tell application "System Events"
    -- checks for instance of Growl and Spotify, does not run if both programs are not active
    set isRunning to ¬
        (count of (every process whose name is "Growl")) > 0
    (count of (every process whose name is "Spotify")) > 0
end tell

--establish empty variable to be filled by song title later
global latest_song
set latest_song to ""

on idle
    tell application "Spotify"
        if player state is playing then
            copy name of current track to current_tracks_name
            -- runs match between last and current song titles
            if current_tracks_name is not latest_song then
                copy current_tracks_name to latest_song
                set who to artist of current track
                set onwhat to album of current track
                tell application "Growl"
                    -- Make a list of all the notification types 
                    -- that this script will ever send:
                    set the allNotificationsList to ¬
                        {"SpotifyNewTrack"}

                    -- Make a list of the notifications 
                    -- that will be enabled by default.      
                    -- Those not enabled by default can be enabled later 
                    -- in the 'Applications' tab of the growl prefpane.
                    set the enabledNotificationsList to ¬
                        {"SpotifyNewTrack"}

                    -- Register our script with growl.
                    -- You can optionally (as here) set a default icon 
                    -- for this script's notifications.
                    register as application ¬
                        "Spotify" all notifications allNotificationsList ¬
                        default notifications enabledNotificationsList ¬
                        icon of application "Spotify"

                    --  Send a Notification...
                    notify with name ¬
                        "SpotifyNewTrack" title ¬
                        current_tracks_name description ¬
                        who application name "Spotify"
                end tell
            end if
            return 5
        end if
    end tell
end idle

这可能有点复杂,但非常感谢您的帮助。

值得一提的是,Spotify在最近的更新中对此进行了修补。

我编写了一个小脚本,因为Spotify不支持Growl 1.3

编程不是很好,但我认为,通知非常花哨;)

将延迟时间设置为1
--登记咆哮
告诉应用程序“咆哮”
将allNotificationsList设置为{“SpotifyNewTrack”}
将enabledNotificationsList设置为{“SpotifyNewTrack”}
注册为申请
“Spotify”所有通知所有通知列表
默认通知启用通知列表
应用程序“Spotify”的图标
结束语
将myspace设置为“ "
将myspace2设置为myspace&myspace
将myspace3设置为myspace&myspace&myspace
将旧轨道id设置为“”
将旧_状态设置为“”
重复
尝试
将boolStarted设置为false
将boolpa设置为false
--主代码
告诉应用程序“Spotify”将曲目id设置为当前曲目的id
告诉应用程序“Spotify”将状态设置为字符串形式的播放器状态
如果((旧_状态为“停止”)或(旧_状态为“暂停”)且状态为“播放”,则将boolStarted设置为true
如果(旧状态为“播放”)且状态为“暂停”,则将boolPaused设置为true
将旧_状态设置为状态
如果(旧轨道id不是轨道id)或boolStarted或boolpa,则使用
告诉应用程序“Spotify”
将标题设置为当前曲目的名称
将艺人设置为当前曲目的艺人
将唱片集设置为当前曲目的唱片集
将持续时间设置为当前曲目的持续时间
将计数设置为当前曲目的播放计数
将当前曲目的starred设置为starred
将“流行度”设置为当前曲目的流行度
将覆盖设置为当前曲目的插图
将位置设置为玩家位置
结束语
如果是明星的话
将星号设置为“♥“&myspace
其他的
将stringstarted设置为“”
如果结束
将stringPopularity设置为“”
重复(人口/20为整数)次
将stringPopularity设置为stringPopularity&“✭"
结束重复
重复(5-(人口/20为整数))次
将stringPopularity设置为stringPopularity&“✩"
结束重复
将分钟设置为四舍五入(持续时间/60)四舍五入
将秒设置为四舍五入(持续时间-(分钟*60))
如果秒数小于10,则将秒数设置为“0”&秒数
将PosMinutes设置为四舍五入(位置/60)四舍五入
将PossSeconds设置为整数位置-(PosMinutes*60)
如果秒数小于10,则将秒数设置为“0”&秒数
将stringPos设置为PosMinutes&“:”和PosSeconds
如果使用boolStarted或boolpa,则
将PosMinutes设置为四舍五入(位置/60)四舍五入
将PossSeconds设置为整数位置-(PosMinutes*60)
如果秒数小于10,则将秒数设置为“0”&秒数
将stringPos设置为PosMinutes&“:”和PosSeconds
如果结束
将growlTitle设置为artist&“:”&myspace2&theTitle
将增长消息设置为album&myspace&“| |”&myspace&theMinutes&“:”&theSeconds&”
myspace&stringstarted&stringPopularity&(“&thePopularity&”)&myspace&theCount&“x”
如果boolStarted,则将growlTitle设置为growlTitle&myspace3&“▶"
如果使用boolpatle,则将growlTitle设置为growlTitle&myspace3&“❚❚"
--保存封面艺术
将f设置为打开以访问“Macintosh HD:tmp:spotify artwork.tiff”,并具有写入权限
将f的eof设置为0
把这封信写给f
封闭通道f
将百分比设置为整数(20*位置/持续时间)
将stringPercentage设置为“”
重复(百分比-1)次
将stringPercentage设置为stringPercentage&“·”
结束重复
将stringPercentage设置为stringPercentage&“◆"
如果百分比为0,则
重复(19-百分比)次
将stringPercentage设置为stringPercentage&“·”
结束重复
其他的
重复(20倍-百分比)次
将stringPercentage设置为stringPercentage&“·”
结束重复
如果结束
如果使用boolStarted或boolpa,则
将growlTitle设置为growlTitle&myspace3&stringPos&myspace3&stringPercentage
如果结束
告诉应用程序“咆哮”以姓名通知
“SpotifyNewTrack”标题成长信息描述成长信息
应用程序名称——
“Spotify”来自位置的图像“/tmp/Spotify artwork.tiff”
如果结束
将旧轨道id设置为轨道id
结束尝试
延迟时间
结束重复

我一直在使用下面的脚本。我相信它能回答您的问题。 此代码位于:


Spotify支持哪种版本的咆哮?
set delay_time to 1

-- register growl
tell application "Growl"
set the allNotificationsList to {"SpotifyNewTrack"}
set the enabledNotificationsList to {"SpotifyNewTrack"}
register as application ¬
    "Spotify" all notifications allNotificationsList ¬
    default notifications enabledNotificationsList ¬
    icon of application "Spotify"
end tell

set myspace to " "
set myspace2 to myspace & myspace
set myspace3 to myspace & myspace & myspace

set old_track_id to ""
set old_state to ""

repeat
try

    set boolStarted to false
    set boolPaused to false

    -- main code        
    tell application "Spotify" to set track_id to id of current track
    tell application "Spotify" to set theState to player state as string


    if ((old_state is "stopped") or (old_state is "paused")) and theState is "playing" then set boolStarted to true
    if (old_state is "playing") and theState is "paused" then set boolPaused to true

    set old_state to theState

    if (old_track_id is not track_id) or boolStarted or boolPaused then

        tell application "Spotify"
            set theTitle to name of the current track
            set theArtist to artist of the current track
            set theAlbum to album of the current track
            set theDuration to duration of the current track
            set theCount to played count of the current track
            set theStarred to starred of the current track
            set thePopularity to popularity of the current track
            set theCover to artwork of the current track
            set thePosition to player position
        end tell


        if theStarred then
            set stringStarred to "♥" & myspace
        else
            set stringStarred to ""
        end if

        set stringPopularity to ""
        repeat (thePopularity / 20 as integer) times
            set stringPopularity to stringPopularity & "✭"
        end repeat
        repeat (5 - (thePopularity / 20 as integer)) times
            set stringPopularity to stringPopularity & "✩"
        end repeat

        set theMinutes to round (theDuration / 60) rounding down
        set theSeconds to round (theDuration - (theMinutes * 60))
        if theSeconds < 10 then set theSeconds to "0" & theSeconds

        set thePosMinutes to round (thePosition / 60) rounding down
        set thePosSeconds to thePosition - (thePosMinutes * 60) as integer
        if thePosSeconds < 10 then set thePosSeconds to "0" & thePosSeconds
        set stringPos to thePosMinutes & ":" & thePosSeconds



        if boolStarted or boolPaused then
            set thePosMinutes to round (thePosition / 60) rounding down
            set thePosSeconds to thePosition - (thePosMinutes * 60) as integer
            if thePosSeconds < 10 then set thePosSeconds to "0" & thePosSeconds
            set stringPos to thePosMinutes & ":" & thePosSeconds
        end if


        set growlTitle to theArtist & ":" & myspace2 & theTitle
        set growlMessage to theAlbum & myspace & "||" & myspace & theMinutes & ":" & theSeconds & ¬
            myspace & stringStarred & stringPopularity & " (" & thePopularity & ")" & myspace & theCount & "x"

        if boolStarted then set growlTitle to growlTitle & myspace3 & "▶"
        if boolPaused then set growlTitle to growlTitle & myspace3 & "❚❚"


        -- save cover art
        set f to open for access "Macintosh HD:tmp:spotify-artwork.tiff" with write permission
        set eof of f to 0
        write theCover to f
        close access f


        set thePercentage to (20 * thePosition / theDuration) as integer

        set stringPercentage to ""

        repeat (thePercentage - 1) times
            set stringPercentage to stringPercentage & "·"
        end repeat

        set stringPercentage to stringPercentage & "◆"

        if thePercentage is 0 then
            repeat (19 - thePercentage) times
                set stringPercentage to stringPercentage & "·"
            end repeat
        else
            repeat (20 - thePercentage) times
                set stringPercentage to stringPercentage & "·"
            end repeat
        end if

        if boolStarted or boolPaused then
            set growlTitle to growlTitle & myspace3 & stringPos & myspace3 & stringPercentage
        end if


        tell application "Growl" to notify with name ¬
            "SpotifyNewTrack" title growlTitle description growlMessage ¬
            application name ¬
            "Spotify" image from location "/tmp/spotify-artwork.tiff"


    end if

    set old_track_id to track_id


end try
delay delay_time
end repeat
property update_delay : 0.1

tell application "Audio Hijack Pro"
    activate
    -- Create a new session "Spotify" if it doesn't already exist
    try
        set spotify_session to first session whose name is "Spotify"
        if output name format of spotify_session is not "%tag_artist - %tag_title" then
            display dialog "The existing profile for \"Spotify\" does not utilize %tag_artist and %tag_title in the output name format option. Please add this if you want your files to be named automatically or delete/rename your existing \"Spotify\" session."
        end if
    on error number -1719
        tell application "Finder"
            set spotify_path to POSIX path of (application file id "spty" as alias)
        end tell
        set spotify_session to make new application session at end of sessions
        set targeted application of spotify_session to spotify_path
        set output folder of spotify_session to "~/Desktop"
        set output name format of spotify_session to "%tag_artist - %tag_title"
        set recording format of spotify_session to {encoding:MP3, bit rate:320, channels:Stereo, style:VBR}
    end try
    set name of spotify_session to "Spotify"

    -- (Re-)start hijacking and recording on the spotify session
    if hijacked of spotify_session is true then
        stop hijacking spotify_session
    end if
    start hijacking spotify_session relaunch yes
end tell

tell application "Spotify"
    if not running then activate
    if player state is playing then pause

    display dialog "Start playing the tracks that you want to rip with Audio Hijack, quit spotify when done."
    if repeating then display dialog "Please notice that you have repeating enabled in Spotify!"

    set has_started to false -- Whether spotify have been playing yet

    -- Check for changes to current track until spotify exits
    repeat until application "Spotify" is not running
        -- Changed track
        if has_started and id of current track is not recording_id then
            tell application "Audio Hijack Pro" to stop recording spotify_session
            set has_started to false
        end if

        -- Started playing
        if has_started is not true and player state is playing then
            set has_started to true
            set recording_id to id of current track

            -- Get the metadata
            set track_name to name of current track
            set track_artist to artist of current track
            set track_album to album of current track

            tell application "Audio Hijack Pro"
                set title tag of spotify_session to track_name
                set artist tag of spotify_session to track_artist
                set album tag of spotify_session to track_album

                start recording spotify_session
            end tell
        end if

        -- Stopped playing
        if has_started and player state is not playing then
            tell application "Audio Hijack Pro" to stop recording spotify_session
            set has_started to false
        end if

        delay update_delay
    end repeat

    tell application "Audio Hijack Pro" to stop recording spotify_session
end tell