Big Sur下用AppleScript实现菜单栏NowPlaying控件5秒快进快退
macOS Big Sur下控制NowPlaying菜单栏组件5秒快进/快退的AppleScript实现
优先方案:原生播放命令实现(无UI依赖,稳定性最高)
绝大多数适配系统播放框架的应用(包括本地播放器、浏览器网页视频、主流流媒体桌面端)都支持原生播放控制接口,不需要定位UI元素即可直接操作:
5秒快进脚本
tell application "System Events" -- 获取当前正在播放的应用 set activePlayers to every application whose has audio playback is true and playing is true if (count of activePlayers) > 0 then set targetApp to item 1 of activePlayers try tell application (name of targetApp) step forward by 5 end tell on error -- 原生接口不兼容时兜底模拟媒体键操作 key code 100 -- 快进媒体键 delay 0.2 key code 100 end try end if end tell
5秒快退脚本
tell application "System Events" set activePlayers to every application whose has audio playback is true and playing is true if (count of activePlayers) > 0 then set targetApp to item 1 of activePlayers try tell application (name of targetApp) step back by 5 end tell on error key code 99 -- 快退媒体键 delay 0.2 key code 99 end try end if end tell
备选方案:动态UI定位实现(适配特殊场景)
如果原生命令不支持你的播放场景,可以用动态属性匹配定位NowPlaying组件的进度条,不要使用UI浏览器抓取的静态路径,控制中心UI层级每次展开都会动态变化,静态路径极易失效:
前置要求
运行前需要给脚本编辑器/执行脚本的工具开启「系统设置-隐私与安全性-辅助功能」权限,DRM加密的受保护视频无法通过该方案控制。
5秒快进UI版脚本
tell application "System Events" tell process "ControlCenter" -- 展开NowPlaying面板 click menu bar item "Now Playing" of menu bar 1 delay 0.1 -- 匹配进度条控件 set playSlider to slider 1 of group 1 of window "Control Center" -- 计算5秒对应偏移量 set totalTime to value of attribute "AXMaxValue" of playSlider set currentTime to value of playSlider set offset to 5 / totalTime * (max value of playSlider - min value of playSlider) set value of playSlider to currentTime + offset -- 收起面板 click menu bar item "Now Playing" of menu bar 1 end tell end tell
5秒快退UI版脚本
仅需将上述脚本中set value of playSlider to currentTime + offset改为set value of playSlider to currentTime - offset即可。
内容的提问来源于stack exchange,提问作者hydrogene75
相关产品推荐
相关产品推荐

