MacOS – Where is the Replace/With list stored

keyboardmacospreferences

This list, the one accessed by System Preferences/Keyboard/Text, vanished when I updated to 10.9.2 (it survived the initial update to Mavericks). I do a lot of writing for the sciences, and I've spent years building that list. (recent MacBook Pro, 10.9.2).

Some sites say I should get the /Users/username/Library/Preferences/.GlobalPreferences.plist file from a backup, but I've searched the HD of my other computer (same specs but running 10.9.1, Replace/With list intact), and I can find no such file.

Other sites say it's in the "NSUserDictionaryReplacementItems array" in 10.9, but unfortunately I don't know what that means. (I'm a user, not a programmer.)

I've heard that it may be linked to Mavericks' new ability to sync this list to iOS devices, but that still doesn't explain why I can't find it on my 10.9.1 machine, which has been syncing to my phone (iPhone 4, iOS 5.x) for years.

Best Answer

In 10.9 the text replacements are also stored in ~/Library/Dictionaries/CoreDataUbiquitySupport/$USER~*/UserDictionary/local/store/UserDictionary.db. UserDictionary.db is used even if iCloud is disabled and it has precedence over .GlobalPreferences.plist.

If you for example run defaults write -g NSUserDictionaryReplacementItems '({on=1;replace=aa;with=bb;})' and quit and reopen TextEdit, aa is replaced with bb in TextEdit, but the changes are reverted if you open the Text tab of the Keyboard preference pane.

You can change both UserDictionary.db and .GlobalPreferences.plist by using a script like this:

date=$(date +%s)
while read -r replace with; do
  plist+="{on=1;replace=\"$replace\";with=\"$with\";},"
  sql+="INSERT INTO 'ZUSERDICTIONARYENTRY' VALUES($((++i)),1,1,0,0,0,0,$date,NULL,NULL,NULL,NULL,NULL,\"$with\",\"$replace\",NULL);"
done < <(sed 's/\\/\\\\/g;s/"/\\"/g' ~/replacements.txt)
sqlite3 ~/Library/Dictionaries/CoreDataUbiquitySupport/$USER~*/UserDictionary/local/store/UserDictionary.db "delete from ZUSERDICTIONARYENTRY;$sql"
defaults write -g NSUserDictionaryReplacementItems "(${plist%?})"

In 10.8 and earlier versions of OS X the text replacements are stored in the NSUserReplacementItems array (not NSUserDictionaryReplacementItems) in ~/Library/Preferences/.GlobalPreferences.plist.