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

如何设置macOS双显示器布局切换的快捷键/热键?

我之前刚好解决过一模一样的需求,用Alfred配合脚本就能完美实现一键切换双显示器布局,下面给你两种可行方案,你可以根据自己的情况选择:

方案一:原生AppleScript(无需额外安装工具)

这个方案依赖macOS的原生脚本能力,不需要装第三方工具,但需要你根据自己的显示器名称做一点修改:

  1. 先获取你的显示器名称:打开「System Settings >> Displays >> Arrangement」,把鼠标悬停在显示器图标上,会显示对应的名称(比如“Built-in Retina Display”或者你的外接显示器型号)。
  2. 复制下面的AppleScript,替换其中的显示器名称为你自己的:
tell application "System Settings"
    activate
    reveal pane id "com.apple.Displays-Settings.extension"
end tell

tell application "System Events"
    tell application process "System Settings"
        -- 等待面板加载完成
        repeat until exists tab group 1 of window 1
            delay 0.5
        end repeat
        click button "Arrangement" of tab group 1 of window 1
        
        -- 替换成你的主、副显示器名称
        set mainDisplay to first item of every UI element of scroll area 1 of tab group 1 of window 1 whose value of attribute "AXDescription" contains "Built-in Retina Display"
        set secondaryDisplay to first item of every UI element of scroll area 1 of tab group 1 of window 1 whose value of attribute "AXDescription" contains "DELL U2720QM"
        
        set mainPos to position of mainDisplay
        set secondaryPos to position of secondaryDisplay
        
        -- 判断当前布局并切换
        if item 2 of secondaryPos = item 2 of mainPos then
            -- 从并排切换到上下:副显示器对齐主显示器左侧,顶部在主显示器底部
            set item 1 of secondaryPos to item 1 of mainPos
            set item 2 of secondaryPos to item 2 of mainPos + (size of mainDisplay)'s item 2
        else
            -- 从上下切换到并排:副显示器对齐主显示器顶部,左侧在主显示器右侧
            set item 1 of secondaryPos to item 1 of mainPos + (size of mainDisplay)'s item 1
            set item 2 of secondaryPos to item 2 of mainPos
        end if
        
        -- 应用新布局并关闭设置窗口
        set position of secondaryDisplay to secondaryPos
        tell application "System Settings" to quit
    end tell
end tell

注意:这个脚本依赖System Settings的UI结构,如果你用的是Ventura之前的系统(比如Monterey),需要把System Settings改成System Preferences,对应的pane id也需要调整为com.apple.preference.displays。

方案二:用displayplacer(更稳定,推荐)

如果觉得UI脚本不够可靠,推荐用displayplacer这个命令行工具,它能直接读取和设置显示器布局,兼容性更好:

  1. 先安装displayplacer:打开终端,运行brew install displayplacer(如果没装Homebrew,先装一下Homebrew)。
  2. 获取两种布局的命令:
    • 先手动把显示器设置成并排布局,然后在终端运行displayplacer list,复制输出中以displayplacer开头的完整命令(这就是并排布局的配置命令)。
    • 再手动设置成上下布局,同样运行displayplacer list,复制对应的配置命令。
  3. 编写切换脚本:把下面的bash脚本中的布局命令替换成你刚才复制的内容:
#!/bin/bash

# 替换成你自己的两种布局命令
LAYOUT_SIDE_BY_SIDE='displayplacer "id:12345678-1234-1234-1234-123456789012 res:2560x1440 origin:(0,0) degree:0" "id:87654321-4321-4321-4321-210987654321 res:2560x1440 origin:(2560,0) degree:0"'
LAYOUT_TOP_BOTTOM='displayplacer "id:12345678-1234-1234-1234-123456789012 res:2560x1440 origin:(0,0) degree:0" "id:87654321-4321-4321-4321-210987654321 res:2560x1440 origin:(0,1440) degree:0"'

# 获取副显示器的当前X坐标(替换成你的副显示器ID)
CURRENT_X=$(displayplacer list | grep -A5 "id:87654321-4321-4321-4321-210987654321" | grep origin | awk -F'origin:\\(' '{print $2}' | awk -F',' '{print $1}')

# 判断当前布局并切换
if [ "$CURRENT_X" -eq 2560 ]; then
    eval $LAYOUT_TOP_BOTTOM
else
    eval $LAYOUT_SIDE_BY_SIDE
fi

如何在Alfred中配置工作流

不管用哪种方案,都可以通过Alfred绑定快捷键:

  1. 打开Alfred Preferences,切换到「Workflows」标签页。
  2. 点击左上角的+号,选择「Blank Workflow」,给工作流起个名字(比如“Display Layout Switcher”),点击「Create」。
  3. 右键点击工作流画布,选择「Inputs >> Hotkey」,设置你想要的触发快捷键(比如Cmd + Option + Ctrl + D),勾选「Alfred runs this workflow when the hotkey is pressed」。
  4. 再右键点击画布,选择「Actions >> Run Script」:
    • 如果用AppleScript,把脚本粘贴进去,在下拉菜单选择「/usr/bin/osascript」。
    • 如果用bash脚本,把脚本粘贴进去,下拉菜单选择「/bin/bash」。
  5. (可选)添加一个「Notification」动作,连接到Run Script后面,设置切换成功后的提示文本(比如“已切换到上下布局”),这样更直观。
  6. 保存后,按下你设置的快捷键,就能一键切换显示器布局了!

注意:如果用AppleScript方案,第一次运行可能会弹出权限提示,需要在「System Settings >> Privacy & Security >> Accessibility」中添加Alfred,允许它控制你的电脑。

额外小技巧

  • 如果你有更多布局需要切换,可以扩展脚本,比如添加三种布局的判断,或者在Alfred中用Keyword触发(比如输入layout side切换并排,layout top切换上下)。
  • 可以把脚本保存成独立文件,然后在Alfred的Run Script中调用这个文件,方便后续修改。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:55:24