How to modify .GlobalPreferences.plist file? my objective is to create a keyboard shortcut to activate/deactivate the &quot;replace with&quot; on Mac.
The idea is to create an automation that can be activated with a shortcut trough services.
What the shortcut does is switching two similar .GlobalPreferences.plist files, one with &quot;replace with&quot; keys one without.
Here is my code:

The problem is that for some reason it doesn't switch the files. But on a regular folder it works.
In detail: 
running mv ~/Library/Preferences/.GlobalPreferences.plist ~/Library/Preferences/.GlobalPreferencesTMP.plist perfectly works, the proprieties are copied.
On the other hand if I run mv /Library/Preferences/.GlobalPreferences2.plist ~/Library/Preferences/.GlobalPreferences.plist It doesn't keep the proprieties of .GlobalPreferences2.plist but instead I got exactly the same as .GlobalPreferences.plist which I started with.
In order to toggle the state of Text Substitutions by swapping the ~/Library/Preferences/.GlobalPreferences.plist file, you would need to have a way to update the cached copy of it in memory. If you are an Objective-C programmer and have Xcode you could roll-your-own app to update the file and what's cached in memory. Sans that, here is a method using a third-party command line utility, shortcuts, and a shell script that works for me.
The bash shell script I've written uses the shortcuts binary executable to backup, delete and restore the Text Substitutions. The backup and restore are simple and straight forward compared to deleting the Text Substitutions as shortcuts can only delete one shortcut at a time. It's relatively quick. I have 200+ items in my Replace | With list and the script takes ~4 seconds to delete them, and a fraction of a second to restore them.
This was tested under macOS Catalina and macOS Big Sur with Language &amp; Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

1  Assumes necessary and appropriate setting in System Preferences &gt; Security &amp; Privacy &gt; Privacy have been set/addressed as needed.


Note: First make a manual backup of your Text Substitutions by going to System Preferences &gt; Keyboard &gt; Text, pressing ⌘A to highlight all entries of Replace | With list, then click-hold with the mouse, then drag and drop to the Desktop, or any folder of a Finder window.
This will create a file named Text Substitutions.plist that can be dragged and dropped onto the Replace | With list in System Preferences &gt; Keyboard &gt; Text to manually restore.
The example shell script code in conjunction with the shortcuts command line utility does it own backup and restores from its backup, but always make the manual backup described above just to have if ever needed.

Install shortcuts by one of the methods mentioned in the link.
I downloaded the source code, reviewed and verified there was nothing ominous in the code, then built it using Xcode.
It's also available thru Homebrew, in Terminal brew install rodionovd/taps/shortcuts, or download as a pre-built binary.

Note: The following instructions and example shell script code are for use as an executable shell script in Terminal, and or can be used in a Run Shell Script action in an Automator Service/Quick Action and assigned a keyboard shortcut in: System Preferences &gt; Keyboard &gt; Shortcuts &gt; Services
I'd suggest testing as an executable shell script first to ensure that it works for you, then use the example shell script code in an Automator Service/Quick Action.
In Terminal, run the following compound command:
      Assumes the pwd is your HOME directory.
f=trw; touch &quot;$f&quot;; open -e &quot;$f&quot;; chmod +x &quot;$f&quot;

      Note: trw = Toggle Replace With (Had to name it something.)
In the opened trw document copy and paste the example shell script code into it, then save and close it.
Back in Terminal:
./trw

Will toggle the state of the Text Substitutions.
Note: After testing, move trw to a directory defined in the PATH to be available without typing: ./

Example shell script code:
#!/bin/bash

# set -e
# set -x 

shortcuts=&quot;/usr/local/bin/shortcuts&quot;
automator=false # true or false


message() {
    [ &quot;${1}&quot; == &quot;xmissing&quot; ] &amp;&amp; msg=&quot; The 'shortcuts' executable is missing!&quot;
    [ &quot;${1}&quot; == &quot;already&quot; ] &amp;&amp; msg=&quot; The text substitutions deletion is already in progress!&quot;
    [ &quot;${1}&quot; == &quot;backedup&quot; ] &amp;&amp; msg=&quot; The text substitutions have been backed up.&quot;
    [ &quot;${1}&quot; == &quot;nobackup&quot; ] &amp;&amp; msg=&quot; No text substitutions to backup or restore!&quot;
    [ &quot;${1}&quot; == &quot;restored&quot; ] &amp;&amp; msg=&quot; The text substitutions have been restored.&quot;
    [ &quot;${1}&quot; == &quot;deleting&quot; ] &amp;&amp; msg=&quot; The text substitutions deletion is in progress...&quot;
    [ &quot;${1}&quot; == &quot;finished&quot; ] &amp;&amp; msg=&quot; The text substitutions deletion has completed.&quot;
    [ $automator == false ] &amp;&amp; printf '%s\n' &quot;${msg}&quot;
    [ $automator == true ] &amp;&amp; printf '%s\n' &quot;$(date -j) ${msg}&quot; &gt;&gt; message.txt
    [ $automator == true ] &amp;&amp; [ -n &quot;${2}&quot; ] &amp;&amp; \
    osascript -e &quot;display notification \&quot;${msg}\&quot; with title \&quot;Toggle Replace With\&quot;&quot;
}

backupdir=&quot;$HOME/Library/Application Support/com.$USER.shortcuts&quot;
[ ! -d &quot;${backupdir}&quot; ] &amp;&amp; mkdir -p &quot;${backupdir}&quot;
cd &quot;${backupdir}&quot; || exit
backup=&quot;${backupdir}/Text_Substitutions.plist&quot;
lockdir=&quot;/private/tmp/com.$USER.shortcuts&quot;
[ ! -e &quot;${lockdir}&quot; ] &amp;&amp; mkdir -p &quot;${lockdir}&quot;
cd &quot;${lockdir}&quot; || exit

if [ ! -x &quot;${shortcuts}&quot; ]; then
    message &quot;xmissing&quot;
    [ $automator == true ] &amp;&amp; message &quot;xmissing&quot; &quot;notification&quot;
    exit    
fi

if [ -e &quot;.lock&quot; ]; then
    message &quot;already&quot;
    [ $automator == true ] &amp;&amp; message &quot;already&quot; &quot;notification&quot;
    exit
fi

contents=&quot;$(defaults read -g NSUserDictionaryReplacementItems)&quot;
empty='(
)'

if [ ! -e &quot;${backup}&quot; ] &amp;&amp; [ &quot;${contents}&quot; != &quot;${empty}&quot; ]; then
    &quot;${shortcuts}&quot; read --as-plist | tee &quot;${backup%.*}.$(date '+%s').plist&quot; &gt; &quot;${backup}&quot;
    message &quot;backedup&quot;
elif [ -e &quot;${backup}&quot; ] &amp;&amp; [ &quot;${contents}&quot; != &quot;${empty}&quot; ]; then
    &quot;${shortcuts}&quot; read --as-plist | tee &quot;${backup%.*}.$(date '+%s').plist&quot; &gt; &quot;${backup}&quot;
    message &quot;backedup&quot;
elif [ ! -e &quot;${backup}&quot; ] &amp;&amp; [ &quot;${contents}&quot; == &quot;${empty}&quot; ]; then
    message &quot;nobackup&quot;
    [ $automator == true ] &amp;&amp; message &quot;nobackup&quot; &quot;notification&quot;
    exit
fi

if [ &quot;${contents}&quot; == &quot;${empty}&quot; ]; then
    &quot;${shortcuts}&quot; import &quot;${backup}&quot;
    message &quot;restored&quot;
    [ $automator == true ] &amp;&amp; message &quot;restored&quot; &quot;notification&quot;
else
    touch &quot;.lock&quot;
    message &quot;deleting&quot;
    [ $automator == true ] &amp;&amp; message &quot;deleting&quot; &quot;notification&quot;
    while IFS= read -r line; do
        &quot;${shortcuts}&quot; delete &quot;${line}&quot;
    done &lt;&lt;&lt; &quot;$(&quot;$shortcuts&quot; read | awk 'sub(/^&quot;/, &quot;&quot;, $2) sub(/&quot;$/, &quot;&quot;, $2) $1 ~ /^[0-9]+:/ { print $2 }')&quot;
    sleep 1
    rm &quot;.lock&quot;
    message &quot;finished&quot;
    [ $automator == true ] &amp;&amp; message &quot;finished&quot; &quot;notification&quot;
fi



Notes:
As coded, the example shell script code assume the shortcuts executable is located at /usr/local/bin/shortcuts, change as applicable. It is coded like this as /usr/local/bin/ is not in the PATH passed to a Run Shell Script action in an Automator Service/Quick Action.
The first time the shell script is run it creates a backup of the Text Substitutions to restore from, so it is imperative that there actually be some text substitutions to backup! A current backup is made each time prior to deleting them from the ~/Library/Preferences/.GlobalPreferences.plist file and is restored from the last backup.
The reason a third-party application is being used is in this use case it does something that the macOS default utilities cannot do, which is dynamically update the changes to the system. In other words, the defaults command as well as the /usr/libexec/PlistBuddy command in this use case does not update the system to the changes and has the same issue as swapping out the ~/Library/Preferences/.GlobalPreferences.plist file.
When using the example shell script code in a Run Shell Script action in an Automator Service/Quick Action, you need to set automator=true in the line after, e.g., shortcuts=&quot;/usr/local/bin/shortcuts&quot;
Backups are saved to:
$HOME/Library/Application Support/com.$USER.shortcuts/Text_Substitutions.plist

The example shell script code when run as an executable shell script outputs messages to stdout and you see them as you go, however, when used in a Run Shell Script action in an Automator Service/Quick Action messages are displayed in Notification Center and also log to:
/private/tmp/com.$USER.shortcuts/message.txt

I will try you add comments to the code and or add additional notes here in the next couple of days.




Disclaimer Note: The example shell script code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any additional error handling as may be appropriate, needed or wanted.