I have a spell checking script for InDesign that isn’t catching all the misspelled words in the InDesign document. For instance, the designer had the word “defnece” instead of “defense”. I checked and the script is in fact running the word “defnece” through the part of the code that spell checks. It just doesn’t think the word “defnece” is misspelled apparently - unlike this forum which underlined every instance of the word. Ironically, if I add an a and change it to “defnecea”, it catches that. Is there some code I need to change to get the spell checker part of the script to catch all misspelled words?
Here’s the function that checks the spelling:
--FUNCTION NEEDED TO CHECK SPELLING
on check_spelling(theString, current_dictionary)
--Verifying that the script is actually checking the word -->Yes, it is
display dialog "Checking spelling for word: " & theString as string
set anNSString to (current application's NSMutableString's stringWithString:theString)
set theChecker to current application's NSSpellChecker's sharedSpellChecker()
(theChecker's setLanguage:"en_US") -- optional
set theProblems to (theChecker's checkString:anNSString range:{0, anNSString's |length|()} types:(current application's NSTextCheckingTypeSpelling) options:(missing value) inSpellDocumentWithTag:0 orthography:(missing value) wordCount:(missing value))
--Creating blank lists to add misspelled words not in dictionary
set susWords to {}
--Running each potentially misspelled word through the ok list
repeat with aFind in theProblems
set potential_prob to (anNSString's substringWithRange:(aFind's range())) as text
--Checking if the spell checker is picking up the misspelled word --> No it is not
display dialog "Potential misspelled word found: " & (potential_prob as string) as string
--Adding problem words to the problem word list IF the problem word is not in the ok list
if potential_prob as string is not in current_dictionary then -->excluding all words in the ok list (like brand names)
if potential_prob & return as string is not in current_dictionary then -->excluding the word if it's in the ok list but has a return after it
set susWords to susWords & (potential_prob as string) -->adding all potential misspellings to a list
end if
end if
end repeat
return susWords
end check_spelling
And the full code for reference:
--------------------------------------------------------------------------------------------------------------------------------
--ADDING WORDS TO THE SPELL CHECKING DICTIONARY
--------------------------------------------------------------------------------------------------------------------------------
--Creating a list of ok words to not spell check
--Vendor names would go here
set ok_words to {"Absorbine", "Adams", "Addie", "Advantix", "Agratronix", "Agrimaster", "Alfred", "Algoma", "Ameristep", "Andis", "Ardisam", "Armorall", "Armour", "Aruba", "Attwood", "Avalon", "Avantect", "Baraboo", "Barronett", "Benjamin", "Belcaro", "Behlen", "Berkley", "Blackstone", "Boehringer", "Boone", "Bostitch", "Bovikalc", "Buddeez", "California", "Camco", "Canidae", "Carhartt", "Carbor", "Carlisle", "Cathy", "Caviness", "cg/cg®", "Chippewa", "Citgo", "Columbia", "Contico", "Crayola", "Daiwa", "Danby", "Daniels", "Dasco", "Dayco", "Deere", "Delco", "Delonghi", "De’longhi", "Delvac", "Delmont", "Deroma", "Dewalt", "Dogzilla", "Doorbusters", "Doritos", "Doskocil", "Doug", "Delavan", "Dramm", "Drive-Maxx", "Drylok", "Dupont", "Durabilt", "Duracraft", "Durafork", "Durvet", "Eco-Flo", "Ecolution", "Edsal", "Enoz", "Eppinger", "Eprinex", "Erika", "Ertl", "Esoterra", "Estwing", "equaline", "Equimax", "Eukanuba", "Farnam", "Farberware", "Fats Tackle", "Fats Tackle ", "Fimco", "Fiora", "Firstect", "Fiskars", "Flexbrew", "Flotec", "Flueriares", "Folgers", "Flexzilla", "Freshtech", "Friskies", "Frogg", "Gabriel", "Generac", "Geneseo", "Genflex", "Geotek", "Giftables", "Gilmour", "Gojo", "Graco", "Greenleaf", "Grubex", "Gumout", "Hackzall", "Hamilton", "Harvestpro", "Havahart", "Heinz", "Herculiner", "Hershey's", "Hobart", "Holmes", "Homeright", "Homz", "Honda", "Hornady", "Husqvarna", "Hypro", "Iams", "Ingelheim", "Intex", "Invizx", "Irwin", "Jakks", "Jobes", "Kaboodle", "Kärcher", "Karcher", "Kawasaki", "Kaytee", "Kilz", "KitchenAid", "Klean-Strip", "Kote", "Krups", "Kurgo", "Larin", "Leviton", "Lews", "Lincoln", "Livingston", "Lucas", "Lund", "Lunex", "Marklyn", "Masterbuilt", "Maxx", "Mccloskey", "Mcsunley", "Meguiar’s", "Meguiar's", "Melissa", "Merial", "Merrick", "Michelin", "Milorganite", "Milwaukee", "Minnkota", "Minwax", "Miracle-Gro", "Monroe", "Moonrays", "Morton", "Motorcraft", "Muckster", "Multifloor", "Munsing", "Mystik", "Naptha", "Nesco", "Neverkink", "Nexave", "Norpro", "Nutrena", "Nutrish", "Nyjer", "Nylabone", "Odessa", "Oespectrum", "Oil", "Okeechobee", "O’keeffe’s", "O'keeffe's", "Okuma", "Oxy-Acetylene", "Peg-Pérego", "Petlodge", "Permatex", "Petsafe", "Pflueger", "Philips", "Piranhamax", "Pradco", "Proheat", "Progrip", "Proplan", "Prozap", "Purolator", "Rachael", "Rapala", "Rayovac", "Realtree", "Reebok", "Reese", "Remington", "Rinehart", "Roebic", "Rotella", "Sakrete", "Sandusky", "Sawzall", "Scalibor", "Scotts", "Seaguar", "Seaquest", "Seasense", "Sellars", "Seresto", "Seymour", "Shademaker", "Sheba", "Shelterlogic", "Shimano", "Shur", "Shur-tred", "Shur-Line", "Skechers", "Sonicare", "Sorel", "Speeco", "Spiderwire", "Standlee", "Stax", "Stearns", "Stihl", "Stik", "Stratton", "Sperry", "Sturtevant", "Suncast", "Sundolphin", "Sunfilm", "Sunjoy", "Sweepfire", "Swiffer", "Swiveleze", "Tannerite", "Teske", "T-Fal", "Thermacell", "Thompson", "Toggs", "Tonka", "Toughmax", "Tricam", "Trico", "Trilene", "Trimec", "Troy-Bilt", "Trufuel", "Truper", "Twizzlers", "Ultra-Maxx", "Uquality", "Valvoline", "Vanderbilt", "Varathane", "Ventamatic", "Vetality", "Wahl", "Weatherbeater", "Weatherguard", "Werner", "Wyoming", "Zareba", "Zecol", "ZeroXposur", "Zimecterin", "Zinsser", "Zuke's"}
--------------------------------------------------------------------------------------------------------------------------------
--ENABLING SCRIPTING ADDITIONS
--------------------------------------------------------------------------------------------------------------------------------
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
--------------------------------------------------------------------------------------------------------------------------------
--CHECKING SPELLING
--------------------------------------------------------------------------------------------------------------------------------
tell application "Adobe InDesign CC 2018"
activate
--Setting user interaction settings to allow all prompts to come up --> necessary to make sure the misspelled word dialog comes up
set user interaction level of script preferences to interact with all
--Creating a blank list to add misspelled words to
set potential_prob_words to {}
--Looping through every word of every story in the InDesign document
tell active document to set my_stories to every story
set the_doc to active document
repeat with my_story in my_stories
tell the_doc to set parent_text_frame to (every text frame whose contents contains (contents of my_story))
set my_words to every word of my_story
set word_count to count my_words
set current_count to 1
repeat word_count times
set my_word to word current_count of my_story
--Running the check_spelling function towards the bottom of the script
set misspelled_word to (my check_spelling(my_word, ok_words))
--Selecting the potentially misspelled word
if misspelled_word is not {} then
select parent_text_frame
select word current_count of my_story
--------------------------------------------------------------------------------------------------------------------------------
--DISPLAYING THE SPELL CHECK DIALOG
--------------------------------------------------------------------------------------------------------------------------------
--Creating the dialog
set my_field_width to 200
set dialog_name to "CHECK SPELLING FOR WORD " & misspelled_word as string
set myDialog to make dialog with properties {name:dialog_name}
tell myDialog
set can cancel to false
tell (make dialog column)
--Creating the page size fields
tell (make dialog row)
make static text with properties {static label:"Enter the correct spelling (or hit enter, if word is ok):"}
end tell
tell (make dialog row)
make static text with properties {static label:(misspelled_word as string)}
set respell_field to make text editbox with properties {edit contents:(misspelled_word as string), min width:my_field_width}
end tell
end tell
end tell
--Getting the respelled word
if (show myDialog) = true then
set respelled_word to edit contents of respell_field
end if
--Making the dialog go away
destroy myDialog
--Replacing the problem word with the respelled word
if misspelled_word as string is not respelled_word as string then
my find_replace((misspelled_word as string), respelled_word)
end if
end if
--Incrementing the current count by 1
set current_count to current_count + 1
end repeat
end repeat
--------------------------------------------------------------------------------------------------------------------------------
--DISPLAYING THE FINISHED DIALOG
--------------------------------------------------------------------------------------------------------------------------------
if potential_prob_words is not {} then
display dialog "SPELL CHECK COMPLETE" & return & "Spell check is done checking the page(s)" with icon 1 giving up after 5400
else
display dialog "HALLELUIAH!" & return & "No misspelled words found" with icon 1 giving up after 5400
end if
end tell
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--FUNCTIONS
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--FUNCTION NEEDED TO CHECK SPELLING
on check_spelling(theString, current_dictionary)
--Verifying that the script is actually checking the word -->Yes, it is
display dialog "Checking spelling for word: " & theString as string
set anNSString to (current application's NSMutableString's stringWithString:theString)
set theChecker to current application's NSSpellChecker's sharedSpellChecker()
(theChecker's setLanguage:"en_US") -- optional
set theProblems to (theChecker's checkString:anNSString range:{0, anNSString's |length|()} types:(current application's NSTextCheckingTypeSpelling) options:(missing value) inSpellDocumentWithTag:0 orthography:(missing value) wordCount:(missing value))
--Creating blank lists to add misspelled words not in dictionary
set susWords to {}
--Running each potentially misspelled word through the ok list
repeat with aFind in theProblems
set potential_prob to (anNSString's substringWithRange:(aFind's range())) as text
--Checking if the spell checker is picking up any misspelled words --> No it is not
display dialog "Potential misspelled word found: " & (potential_prob as string) as string
--Adding problem words to the problem word list IF the problem word is not in the ok list
if potential_prob as string is not in current_dictionary then -->excluding all words in the ok list (like brand names)
if potential_prob & return as string is not in current_dictionary then -->excluding the word if it's in the ok list but has a return after it
set susWords to susWords & (potential_prob as string) -->adding all potential misspellings to a list
end if
end if
end repeat
return susWords
end check_spelling
-------------------------------------------------------------
-------------------------------------------------------------
--FUNCTION TO REPLACE TEXT
on find_replace(search_term, replace_term)
tell application "Adobe InDesign CC 2018"
set find text preferences to nothing
set change text preferences to nothing
set find what of find text preferences to search_term
set change to of change text preferences to replace_term
change text document 1
set find text preferences to nothing
set change text preferences to nothing
end tell
end find_replace