在 Applescript 中监控 Spotify 曲目变化?

2024-05-04

我正在尝试找出通过 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 不支持 Growl 1.3。

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

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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Applescript 中监控 Spotify 曲目变化? 的相关文章

  • 授权和 Web API 调用结束后如何从 Spotify 注销用户

    我使用 Spotify Web API 来获取播放列表和用户曲目列表 授权工作正常 我也确实得到了曲目详细信息 但之后我想从 Spotify 注销用户并允许新登录 用户自动从 Spotify 帐户注销之前有一个会话时间 但用户可能没有耐心等
  • music.listen 返回一个空数组

    我正在开发 Spotify 应用内应用程序 我想知道我的朋友们在听什么 我目前正在使用音乐 听打开graph api 但它对大多数用户返回空数据 我注册了一个新的 Facebook 帐户 然后使用新帐户登录了 Spotify 我点了一首歌然
  • AppleScript 在 Finder 中设置目录路径

    我正在尝试通过 AppleScript 删除计算机上的文件 当我应用下面的代码时 似乎从桌面上删除了该文件 我想删除 Users andrew Documents 中的文件 下面是从桌面删除文件的代码 tell application Fi
  • 使用 applescript 我想将文件夹中的每个文件移动到根文件夹

    我一直在尝试制作一个脚本将文件夹中的每个文件移动到根文件夹以包含每个子文件夹 我不想创建新文件夹 只需将其移动到根文件夹即可 我希望能够选择文件夹 然后仅在该特定文件夹上完成操作 原因是为了组织 我的确切情况是我有超过 TB 的电影 并且文
  • 无法运行growl通知并且gemgrowl_notify抛出错误

    当我运行 guard 时 我无法收到咆哮通知 我需要growl notify 的任何特定版本吗 咆哮版本 1 2 这是我的宝石文件 gem rails 3 1 3 gem sqlite3 group development test do
  • 获取输入文件的目录(Applescript)

    我很困惑 我已经在谷歌上搜索了一个小时 并且尝试了大约十种不同的形式set posixDirectory to POSIX path of parent of path to aFile as string 但我似乎无法做对 我通过执行以下
  • Spotify API - 在 Google Apps 脚本中检索有效访问令牌

    以下是 Spotify API 的文档 我使用的是隐式授权流程 https beta developer spotify com documentation general guides authorization guide implic
  • 如何从 bash 脚本中识别当前的终端模拟器?

    我有一个脚本可以在 OS X 终端应用程序中打开一个新选项卡 并且我正在尝试添加对 iTerm2 的支持 不幸的是 在两个终端模拟器中打开选项卡的方法不同 我如何知道这两个中的哪一个正在被使用或打开 以有条件地运行正确的脚本 我不确定如何区
  • 别名使 emacs 在新缓冲区(不是框架)中打开文件并被激活/来到前面?

    到目前为止我所拥有的是 alias em open a Applications Emacs app osascript e tell application Emacs app to activate 但我很困惑 使用该代码 em fil
  • 如何使用 AppleScript 按类别过滤 Outlook for Mac 日历事件

    我正在尝试在 OSX 上编写一个 Applescript 以根据事件类别 例如 日历事件 过滤 Outlook for Mac 2011 日历事件 查找标记为 会议 的所有活动 例如 我有一个名为 WWDC 的日历事件 可以通过以下脚本找到
  • -canOpenURL:URL 失败:“spotify:” - 错误:“(null)”

    不确定这是否属实 但从我读到的内容来看 人们说这个问题仅发生在 Xcode 模拟器上 因此您必须在实际设备上进行测试 问题是我当前的服务器是本地节点服务器http localhost 3000 并且我的iOS设备无法访问该服务器 1 有没有
  • 使用邮件规则和 Applescript 将 vcard 添加到联系人

    我通过特定的电子邮件地址收到了很多客户电子名片 我想通过邮件规则和 AppleScript 自动将电子名片添加到我的联系人中 我搜索了很多并找到了一些东西 我稍微修改了一下 并且打开和添加过程运行良好 但只有当我选择一个文件时 我无法将文件
  • Applescript 将启动 Chrome(具体内容)

    我真的很难创建一些在 osx 上启动浏览器窗口 chrome 的方法 具体细节包括窗口大小 没有选项卡等 传统上 我通过带有 IE 的窗口使用 vb 脚本 这是一个非常简单的练习 但我 我将是第一个承认的人 当谈到 Mac 时 我感到非常挣
  • 获取 Spotify 当前播放的曲目

    编辑 让我们尝试澄清这一切 我正在编写一个 python 脚本 我希望它告诉我 Spotify 当前正在播放的歌曲 我尝试寻找可以帮助我的库 但没有找到任何仍在维护和工作的库 我还浏览了 Spotify 的 Web API 但它没有提供任何
  • 如何查找正在执行的 AppleScript 的文件名

    如何找到正在执行的 AppleScript 的名称 原因 我想创建一个根据文件名更改其行为的脚本 就像是 if myname is Joe then ACTION1 else if myname is Frank then ACTION2
  • Spotify 中的 Facebook 个人资料图片

    我想在我的 Spotify 应用程序中显示 Facebook 个人资料图片 但我应该在清单文件中允许哪些 URL 我知道我可以通过 http graph facebook com user id picture 检索图像 但这只是到实际图像
  • 如何获取 iTunes 选择的文件路径

    我正在尝试使用 AppleScript 确定在 iTunes 中选择的曲目的路径 貌似不是该公司的财产track班级 谁能告诉我如何获取文件路径 尝试这个 gets file path of selected song tell appli
  • 如何指示 Applescript 打开带有链接的新 Firefox 窗口?

    我的代码看起来像这样 tell application Firefox open location http rubyquicktips tumblr com end tell 但如果我打开 Firefox 该链接将在新选项卡中打开 但我希
  • 如何将字符串从 Applescript 传递到 Objective C

    我正在开发一个应用程序 我需要能够传递一个字符串变量 from 苹果脚本 to 目标C 我已经弄清楚如何从 Objective C 类中的方法运行 Applescript 但我需要能够将 NSString 设置为 Applescript 中
  • 如何在可编写脚本的应用程序中将任意 AppleScript 记录传递给 Cocoa?

    我有一个 Cocoa 应用程序 其中包含 sdef XML 文件中描述的 AppleScript 字典 sdef 中定义的所有 AppleScript 类 命令等都是工作属性 除了我的 提交表单 命令 提交表单 命令是我尝试将任意信息哈希表

随机推荐