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

如何在MacOS上通过编程方式切换Touch Bar布局,无需打开系统偏好设置

系统环境

  • M1 MacBook Pro
  • MacOS Big Sur

问题描述

我日常使用时偏好Mac默认的Touch Bar布局,但编程时希望直接显示F1-F12按键,不想长按fn键触发。为此我编写了两段AppleScript实现布局切换(代码见下文)。
现有脚本可以运行,但稳定性不佳,原因是脚本依赖打开「系统偏好设置」应用并操作菜单选项。我通过Automator将脚本打包为简易应用,还给它们绑定了快捷键。
这个方案勉强能用,但我想要更优雅的实现:理想情况下脚本可以后台静默运行,瞬间完成Touch Bar布局切换,不需要打开系统偏好设置、点选下拉菜单再关闭应用的冗长流程。
在使用Automator之前,我尝试了很久shell命令都没有成功,请问有没有熟悉相关开发的朋友能给出更好的实现思路?

代码示例

以下脚本可将F1-F12按键设为Touch Bar默认布局:

tell application "System Preferences"
    set the current pane to pane id "com.apple.preference.keyboard"
    delay 0.25
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell

tell application "System Events"
    tell process "System Preferences"
        click pop up button 2 of tab group 1 of window "Keyboard"
    end tell
end tell

tell application "System Events"
    tell process "System Preferences"
        click menu item "F1, F2, etc. Keys" of menu 1 of pop up button 2 of tab group 1 of window "Keyboard"
    end tell
end tell

tell application "System Events"
    tell process "System Preferences"
        click pop up button 4 of tab group 1 of window "Keyboard"
    end tell
end tell

tell application "System Events"
    tell process "System Preferences"
        click menu item "Show App Controls" of menu 1 of pop up button 4 of tab group 1 of window "Keyboard"
    end tell
end tell

quit application "System Preferences"

以下脚本执行反向操作,将应用控件设为Touch Bar默认布局:

tell application "System Preferences"
    set the current pane to pane id "com.apple.preference.keyboard"
    delay 0.25
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell


tell application "System Events"
    tell process "System Preferences"
        click pop up button 2 of tab group 1 of window "Keyboard"
    end tell
end tell

tell application "System Events"
    tell process "System Preferences"
        click menu item "App Controls" of menu 1 of pop up button 2 of tab group 1 of window "Keyboard"
    end tell
end tell

tell application "System Events"
    tell process "System Preferences"
        click pop up button 4 of tab group 1 of window "Keyboard"
    end tell
end tell

tell application "System Events"
    tell process "System Preferences"
        click menu item "Show F1, F2, etc. Keys" of menu 1 of pop up button 4 of tab group 1 of window "Keyboard"
    end tell
end tell

quit application "System Preferences"

解决方案

方案1:静默Shell脚本实现(推荐)

直接修改系统配置项实现切换,全程无窗口弹出,毫秒级生效,完全不需要启动系统偏好设置,也不需要额外权限。

切换为默认显示F1-F12按键:

defaults write com.apple.touchbar.agent PresentationModeGlobal -string functionKeys
killall ControlStrip

切换为默认显示应用控件:

defaults write com.apple.touchbar.agent PresentationModeGlobal -string app
killall ControlStrip

你可以直接把这两段shell脚本保存为.sh文件,赋予执行权限后绑定快捷键即可使用。

方案2:优化现有AppleScript

如果偏好使用AppleScript,可通过隐藏系统偏好设置窗口、替换固定延迟为元素等待逻辑提升稳定性,优化后不会弹出系统偏好设置窗口,也不会因为系统卡顿导致执行失败:

优化后切换F1-F12的脚本参考:

tell application "System Preferences"
    set current pane to pane id "com.apple.preference.keyboard"
    reveal anchor "keyboardTab" of pane id "com.apple.preference.keyboard"
    set visible to false
end tell
tell application "System Events"
    tell process "System Preferences"
        repeat until exists tab group 1 of window "Keyboard"
            delay 0.1
        end repeat
        tell pop up button 2 of tab group 1 of window "Keyboard"
            click
            click menu item "F1, F2, etc. Keys" of menu 1
        end tell
        tell pop up button 4 of tab group 1 of window "Keyboard"
            click
            click menu item "Show App Controls" of menu 1
        end tell
    end tell
end tell
quit application "System Preferences"

内容的提问来源于stack exchange,提问作者Johnny Rocketfingers

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 00:57:02