AppleScript to Delete Misspelled Words in TextEdit

After getting a new dictionary to add more words to Word Lab’s dictionary, there were a few issues that I had to solve. Word Lab only allows words of three letters or more, and the dictionary was filled with misspelled words. I used python to parse the text file to make all the words lower case and only write words of three or more letters long. The tricky part after this was figuring out how to delete misspelled words without having to do it manually. This is what I ended up with:

set c to ASCII character 0—nullset t to 0

tell (ASCII character 8) to set d to it & it—backspace

tell application “System Events” to tell process “TextEdit”

set frontmost to true

set s to the clipboard

keystroke (ASCII character 30)


set t to (t + 1)

set the clipboard to c

keystroke “;c” using {command down}

repeat while (the clipboard) is c and (t < (100))

end repeat

if (the clipboard) is c then

exit repeat

end if

keystroke d

end repeat

set the clipboard to s

end tell

I had to play with the t < amount because it was going too fast and would quit, but it definitely did the job, and my first trip into AppleScript territory.