Adding words to the spelling dictionary

Adding words to the spelling dictionary can be a chore; this script helps simplify the process. After running the script, type a word into the dialog that appears, and press enter (or click the “Add word” button); repeat for as many words as you want to enter. When you’re finished, click the “Finished” button.

A dialog will appear asking you to choose the dictionary file to add the words to. It will default open to the Spelling folder in your user account’s Library folder. Normally, you would choose the “en” file for English words. Click OK. That’s it.

Words entered into this file will pass spell checking, but they will not show up as suggestions. This is true even if you use the Learn function when spell-checking a document.


set additions to ""
set finished to false
repeat until finished is true
	display dialog ¬
		"Enter the next word to add to the user dictionary." & return & ¬
		"Press the Finished button when you're finished" with title ¬
		"Add words to spelling dictionary" default answer ¬
		"" buttons {"Cancel", "Finished", "Add word"} default button 3
	if button returned of the result is "Finished" then
		exit repeat
	else
		set additions to ¬
			(additions & the text returned of the result as text) ¬
				& (ASCII character 0)
	end if
end repeat

set UserFile to choose file with prompt ¬
	"Please choose the dictionary file to add the words to." default location ¬
	alias ((path to library folder from user domain as text) & "spelling")
set fileRef to open for access UserFile with write permission
set starting_point to (get eof of fileRef) + 1
write additions to fileRef starting at starting_point
close access fileRef

Nice one, Michelle. Simple and effective and a good basis for other user-dictionary-editing scripts.

It errored on one of my machines, because I’d never used “Learn” on that particular one and there wasn’t a “Spelling” folder. I’ve written an insert for your script that looks after that. It creates the folder and a file in it named after the code for the user’s preferred language as returned by “defaults read”. It works correctly for me (“en_GB”), but it would be nice to know if the file name’s what the spell-checker expects on machines with other preferred languages.

set additions to ""
set finished to false
repeat until finished is true
	display dialog ¬
		"Enter the next word to add to the user dictionary." & return & ¬
		"Press the Finished button when you're finished" with title ¬
		"Add words to spelling dictionary" default answer ¬
		"" buttons {"Cancel", "Finished", "Add word"} default button 3
	if button returned of the result is "Finished" then
		exit repeat
	else
		set additions to ¬
			(additions & the text returned of the result as text) ¬
				& (ASCII character 0)
	end if
end repeat

set spellingFolderPath to (path to library folder from user domain as Unicode text) & "Spelling:"
try
	-- Test the existence of the "Spelling" folder.
	spellingFolderPath as alias
on error
	-- If there's no "Spelling" folder in the user Library, create one and, inside it,
	-- an empty file named after the code for the user's preferred spelling language.
	set spellingFolderPOSIX to quoted form of POSIX path of spellingFolderPath
	do shell script "defaults read 'Apple Global Domain' 'AppleLanguages'"
	set preferredLang to text from word 1 to word -1 of text 1 thru (offset of "," in result) of result
	do shell script ("mkdir " & spellingFolderPOSIX & " ; echo -n '' > " & spellingFolderPOSIX & preferredLang)
end try

set UserFile to choose file with prompt ¬
	"Please choose the dictionary file to add the words to." default location ¬
	alias spellingFolderPath
set fileRef to open for access UserFile with write permission
try
	write additions to fileRef starting at eof
end try
close access fileRef

I’m sure the two shell scripts and the intervening AppleScript could be combined into one shell script. I leave that for those with the necessary skills. I originally had a single shell script there myself:

set spellingFolderPath to (path to library folder from user domain as Unicode text) & "Spelling:"
try
	-- Test the existence of the "Spelling" folder.
	spellingFolderPath as alias
on error
	-- If there's no "Spelling" folder in the user Library, create one and, inside it,
	-- an empty file named after the code for the user's preferred spelling language.
	set spellingFolderPOSIX to quoted form of POSIX path of spellingFolderPath
	do shell script ("pssl=$(defaults read 'Apple Global Domain' 'NSPreferredSpellServerLanguage') ; mkdir " & spellingFolderPOSIX & " ; echo -n '' > " & spellingFolderPOSIX & "$pssl")
end try

This worked with the main users on both my machines, but not with a French sub-user that I use for testing purposes. The sub-user doesn’t have the ‘NSPreferredSpellServerLanguage’ key for some reason.