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

如何通过CLI/AppleScript/Hammerspoon等模拟点击macOS Spotify的DJ按钮?

用Hammerspoon实现全局快捷键触发Spotify DJ切换(macOS)

可行方案:UI Scripting定位元素

由于Spotify官方API暂未提供触发DJ切换的接口,通过UI Scripting(借助macOS系统事件定位界面元素)是当前可行的方案,无需依赖固定坐标,适配窗口大小变化。

步骤1:开启辅助功能权限

给Hammerspoon开启「辅助功能」权限:

  • 打开「系统设置」→「隐私与安全性」→「辅助功能」
  • 勾选列表中的Hammerspoon

步骤2:Hammerspoon配置脚本

将以下代码添加到你的Hammerspoon配置文件(~/.hammerspoon/init.lua),绑定Shift + 右箭头为全局触发快捷键:

-- 全局快捷键触发Spotify DJ切换
hs.hotkey.bind({"shift"}, "right", function()
    local ok, err = hs.osascript.applescript([[
        tell application "System Events"
            tell process "Spotify"
                try
                    -- 通过 accessibility label 定位DJ按钮
                    set djBtn to first button whose accessibility label contains "DJ"
                    click djBtn
                on error
                    -- 若当前窗口未激活,先激活再尝试
                    set frontmost to true
                    delay 0.2
                    set djBtn to first button whose accessibility label contains "DJ"
                    click djBtn
                end try
            end tell
        end tell
    ]])
    if not ok then
        hs.alert.show("触发Spotify DJ失败:" .. err)
    end
end)

关键说明

  • 脚本通过按钮的accessibility label定位,比坐标点击更稳定。如果Spotify更新界面导致定位失败,可通过Script Editor的「UI元素检查器」查看按钮的最新属性,修改accessibility label的匹配条件。
  • 脚本包含异常处理:如果Spotify窗口未处于前台,会先激活窗口再尝试点击按钮。

备选临时方案(如果UI Scripting失效)

如果上述方法因界面更新失效,可暂时通过模拟播放队列操作替代,但体验不如直接触发DJ:

-- 模拟跳过5首歌(原替代方案的Hammerspoon实现)
hs.hotkey.bind({"shift"}, "right", function()
    for i=1,5 do
        hs.spotify.next()
        hs.timer.usleep(100000)
    end
end)

内容的提问来源于stack exchange,提问作者Steve Kehlet

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 05:03:13