Search code examples
applescriptautomator

Native macOS Text Replacement (adding records)


I found a 2020 year script on the Internet, with which you can create pairs (key value) in the keyboard settings text replacement

Here's the source

Here's the script

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

But it doesn't work, I get an error Unable to get pane id "com.apple.preference.keyboard"

Does anyone have a working version? I would be very grateful!


Solution

  • GUI scripting is fragile, and depends a lot on the particular layout of the user interface, which can change with new application and OS versions. Your linked script would be for Catalina or Big Sur, but there have been a few OS upgrades since then. Apple tends to rearrange things, so the UI hierarchy of the System Preferences/Settings application would need to be explored to get the new layout, which in this case is completely different and uses sheets.

    Another option would be to use the preference pane URL scheme. A bit of GUI scripting is still needed, but it can just be keystrokes for navigating the sheets.

    The following script contains handlers that use both approaches for adding text replacements - note that depending on your particular setup you may need to adjust the delays (for example, the REVEAL_ANCHOR_ERR_MODAL error is from trying to reveal an anchor while a modal dialog/sheet is still open):

    on run -- examples (macOS 14 Sonoma)
       try
          textReplacementWithGUI("testing", "one, two, three")
          textReplacementWithURLScheme("another", "whatever")
       on error errmess
          log errmess
       end try
       # tell application "System Settings" to quit
    end run
    
    on textReplacementWithGUI(replaceText, withText) -- text replacements via GUI scripting (English)
       tell application "System Settings" -- reveal target window and activate
          reveal anchor "TextReplacements" of pane id "com.apple.Keyboard-Settings.extension"
          delay 0.5 -- give the window time to set up
          activate
       end tell
       tell application "System Events"
          tell application process "System Settings" to tell window 1 to tell sheet 1 -- initial replacements sheet
             tell group 1 to tell scroll area 1 to tell group 1 to click button 1 -- the add (+) button
             delay 0.25 -- give the second sheet time to appear
             tell sheet 1 to tell group 1 to tell scroll area 1 to tell group 1 -- now in the add sheet
                set value of text field "Replace" to replaceText
                tell text field "With"
                   set focused to true
                   set value to withText
                end tell
                keystroke return -- dismiss the add sheet (default button)
             end tell
          end tell
          delay 0.25
          keystroke return -- dismiss the replacements sheet
       end tell
       delay 0.25 -- give sheets time to close
    end textReplacementWithGUI
    
    on textReplacementWithURLScheme(replaceText, withText) -- text replacements via preference pane URL scheme
       set isRunning to running of application "System Settings"
       do shell script "open \"x-apple.systempreferences:com.apple.Keyboard-Settings.extension?TextReplacements\"" -- initial replacements sheet
       if not isRunning then delay 0.5
       tell application "System Settings" to activate
       delay 0.5
       tell application "System Events"
          if isRunning then keystroke tab -- move to the add (+) button
          keystroke space -- bring up add dialog
          delay 0.25
          keystroke replaceText -- Replace
          keystroke tab -- move to the next text field
          keystroke withText -- With
          keystroke return -- dismiss add sheet
          delay 0.25
          keystroke return -- dismiss replacements sheet
       end tell
       delay 0.25 -- give sheets time to close
    end textReplacementWithURLScheme