I am really close to a fully working solution on this. The following script successfully toggles the spelling language between English and French in Keyboard settings, however the change isn’t effected in any application that is already open. It has to be restarted for the new spelling language setting to take effect.
# Check the current language setting
CURRENT_LANGUAGE="$(defaults read -g NSPreferredSpellServerLanguage)"
echo "Current language setting: $CURRENT_LANGUAGE"
# Toggle the language setting
if [ "$CURRENT_LANGUAGE" = "en" ]; then
defaults write -g NSPreferredSpellServerLanguage "fr"
echo "Spelling language set to French"
else
defaults write -g NSPreferredSpellServerLanguage "en"
echo "Spelling language set to English"
fi
That doesn’t happen when the language is changed manually in System settings. Placing your cursor at the end of any text already typed in and app already running and pressing Enter forces macOS’s spellcheck / autocorrect to update itself automatically to the new language. Doing the same using the script above doesn’t effect the change without restarting the app, unfortunately.
Changing the language manually through the UI must trigger an additional line of command that allows the change to take effect in whatever apps already running, but I don’t know what it is. ChatGPT has tried a few things things (killall cfprefsd, killall -HUP AppleSpell, killall SystemUIServer, get the name of currently running apps through osascript to effect the change…), but nothing has worked so far.
I’m not a coder so I’m fumbling my way through this. Hoping someone with actual knowledge of shell scripting and macOS will be able to assist.
Cheers.