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

求可用的macOS原生Text Replacement添加记录AppleScript脚本

修复用于添加键盘文本替换的AppleScript脚本

我在网络上找到一个2020年的AppleScript脚本,原本用于在键盘设置的Text Replacement功能中添加键值对,原脚本内容如下:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set theWord to the clipboard as text

tell application id "com.apple.systempreferences"
  reveal anchor "Text" of pane id "com.apple.preference.keyboard"
  activate
end tell

tell application id "com.apple.systemevents"
  tell application process "System Preferences"
    tell window "Keyboard"
      tell tab group 1
        tell group 1
          tell button 1
            click
            --delay 0.5
          end tell
        end tell
        tell scroll area 1
          tell table 1
            tell row -1
              tell UI element 1
                tell text field 1
                  set value to theWord
                  --confirm
                end tell
              end tell
              tell UI element 2
                tell text field 1
                  set value to theWord
                  set focused to true
                end tell
              end tell
            end tell
          end tell
        end tell
      end tell
    end tell
  end tell
end tell

但该脚本无法正常运行,报错信息为:Unable to get pane id "com.apple.preference.keyboard"。


原脚本报错的主要原因是macOS版本更新后,系统偏好设置的面板定位逻辑发生了变化,同时缺少必要的延迟导致界面未加载完成就执行后续操作。以下是适配较新macOS版本的可运行脚本:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set theWord to the clipboard as text

-- 打开键盘偏好设置并定位到文本替换标签
tell application "System Preferences"
    set current pane to pane "com.apple.preference.keyboard"
    reveal anchor "Text" of current pane
    activate
    delay 1 -- 等待界面加载完成
end tell

tell application "System Events"
    tell application process "System Preferences"
        -- 等待键盘窗口加载完毕
        repeat until exists window "Keyboard"
            delay 0.2
        end repeat
        
        tell window "Keyboard"
            tell tab group 1
                -- 点击"添加"按钮
                tell group 1
                    click button 1
                    delay 0.5 -- 等待新行出现
                end tell
                
                -- 在新行中填入剪贴板内容
                tell scroll area 1
                    tell table 1
                        tell last row
                            -- 设置"替换为"文本框
                            tell UI element 1's text field 1
                                set value to theWord
                            end tell
                            -- 设置"输入"文本框并回车确认
                            tell UI element 2's text field 1
                                set value to theWord
                                keystroke return
                            end tell
                        end tell
                    end tell
                end tell
            end tell
        end tell
    end tell
end tell

-- 自动关闭系统偏好设置(可选,可注释掉此行保留窗口)
tell application "System Preferences" to quit

关键调整说明

  • 改用set current pane to pane "com.apple.preference.keyboard"的方式定位面板,避免原脚本的锚点定位失效问题
  • 添加多段延迟和循环等待逻辑,确保界面元素加载完成后再执行操作
  • 新增回车确认步骤,确保键值对被成功保存
  • 可选自动关闭系统偏好设置的逻辑,简化操作流程

注意事项

  • 权限配置:需要在「系统设置」→「隐私与安全性」→「辅助功能」中,允许运行此脚本的应用(如Script Editor、终端等)获取权限,否则脚本无法控制系统界面
  • 延迟调整:若Mac运行较慢,可适当增大delay的数值(比如把delay 1改成delay 2)
  • 自定义内容:如果需要设置不同的「替换文本」和「输入文本」,可以将set theWord to the clipboard as text拆分为两个变量分别赋值

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 08:52:31