You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:修复用于BTT触控栏的AppleScript,实现歌手歌名交替显示

修复BTT触控栏Spotify交替显示脚本

我来帮你搞定这个问题!你的原脚本没法实现交替显示的核心原因是:BTT每次执行AppleScript都是独立进程,没法记住上一次显示的内容,而且原脚本里的交替逻辑写得不对,根本触发不了切换。

下面是修复后的完整脚本,我会一步步解释关键改动:

-- 定义BTT用来保存上次显示状态的变量名
set lastDisplayedKey to "Spotify_Last_Displayed"

if application "Spotify" is running then
    tell application "Spotify"
        if player state is playing then
            set _artist to artist of current track
            set _name to name of current track
            
            -- 处理广告或无有效信息的情况
            if _name is "" or _artist is "" or _name is "Listen Now" then
                return "Ad"
            end if
            
            set _full_string to _artist & " - " & _name
            set _length to length of _full_string
            
            -- 获取BTT中保存的上次显示状态
            tell application "BetterTouchTool"
                set lastDisplayed to get_string_variable lastDisplayedKey
            end tell
            
            if _length > 20 then
                -- 根据上次显示内容切换本次显示项
                if lastDisplayed is "artist" or lastDisplayed is "" then
                    -- 本次显示歌名,同时更新状态标记
                    tell application "BetterTouchTool"
                        set_string_variable lastDisplayedKey to "name"
                    end tell
                    return _name
                else
                    -- 本次显示歌手,同时更新状态标记
                    tell application "BetterTouchTool"
                        set_string_variable lastDisplayedKey to "artist"
                    end tell
                    return _artist
                end if
            else
                -- 长度足够短,直接显示完整字符串,同时重置状态
                tell application "BetterTouchTool"
                    set_string_variable lastDisplayedKey to ""
                end tell
                return _full_string
            end if
        else
            -- Spotify暂停时,清空显示并重置状态
            tell application "BetterTouchTool"
                set_string_variable lastDisplayedKey to ""
            end tell
            return ""
        end if
    end tell
else
    -- Spotify未运行时,清空显示并重置状态
    tell application "BetterTouchTool"
        set_string_variable lastDisplayedKey to ""
    end tell
    return ""
end if

关键改动说明:

  • 加入状态记忆:利用BTT自带的get_string_variable和set_string_variable函数,保存上次显示的是歌手还是歌名,让脚本每次运行时能明确知道该切换到哪一项。
  • 修正交替逻辑:删掉了原脚本里无效的判断语句,改成基于BTT变量的状态切换逻辑,当歌手+歌名总长度超过20字符时,自动在两者间交替显示。
  • 完善边界处理:当Spotify暂停、未运行,或者显示完整短字符串时,都会重置保存的状态,避免后续播放时出现逻辑混乱。
  • 简化冗余代码:优化了原脚本里重复的return ""和嵌套判断,让整体逻辑更清晰易懂。

你把这段代码替换到BTT的触控栏脚本里,就能实现你想要的效果啦!

内容的提问来源于stack exchange,提问作者Tim van der Hoorn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:39:54