Skip to main content
Participant
October 19, 2017
Question

Set InDesign spelling and hyphenation dictionaries using Javascript

  • October 19, 2017
  • 2 replies
  • 2202 views

First time to post to the the forums.

I am trying to create a script that, among other things, sets the spelling and hyphenation dictionaries of a document to Hunspell. I can't seem to figure out how to address the right piece of the object model to change the dictionaries. I started with the Dictionary portion of Keith Gilbert's InDesign Preferences script and tried to strip out the bits that don't appear to apply to my specific situation, which ends up looking like this:

var myDoc = app.activeDocument;

        myDoc.textDefaults.appliedLanguage = "English: USA";

//        app.languagesWithVendors.itemByName('English: USA').addDictionaryPath(File('~/Library/Application Support/Adobe/Linguistics/UserDictionaries/Adobe Custom Dictionary/en_US'));

//        var fullPathOfUDC = "<user disctionary path>";               

//        if (File(fullPathOfUDC).exists) { 

            var languages = app.languagesWithVendors.everyItem().getElements();   

            for (var n=0; n<languages.length; n++) { 

                if (languages.name == app.translateKeyString("$ID/English: USA")) {

//                    var result = languages.addDictionaryPath(fullPathOfUDC); 

                    languages.hyphenationVendor = "Hunspell";

                    languages.spellingVendor = "Hunspell";

                    // languages.doubleQuotes = ;

                    // languages.singleQuotes = ;

                }

            }

//        }

Didn't work.

I then tried a number of variations that tried addressing the objects directly, like this:

app.languagesWithVendors.itemByName('English: USA').hyphenationVendor = "Hunspell";

app.languagesWithVendors.itemByName('English: USA').spellingVendor = "Hunspell";

That didn't work either. Obviously I don't really understand the object model.

I found another discussion asking about the same situation, but it didn't look like there was a satisfactory answer.

Can anyone point me in the right direction? Just trying to solve a problem and learn some stuff in the process.

Thanks in advance for any help you're able to provide!

This topic has been closed for replies.

2 replies

Sunil Yadav
Legend
May 10, 2021

This is just working perfectly fine for me too..

    var doc = app.activeDocument;
    var languageWithVendors = app.languagesWithVendors.itemByName("English: USA");
    languageWithVendors.hyphenationVendor = "Hunspell";
    languageWithVendors.spellingVendor  = "Hunspell";
    doc.textDefaults.appliedLanguage = languageWithVendors;

Sunil

Kasyan Servetsky
Legend
October 22, 2017

It seems to me you're over complicating things:

Main();

function Main() {

    var doc = app.activeDocument;

   

    var languageWithVendors = app.languagesWithVendors.itemByName("English: USA");

    languageWithVendors.hyphenationVendor = "Hunspell";

    languageWithVendors.spellingVendor  = "Hunspell";

   

    doc.textDefaults.appliedLanguage = languageWithVendors;

}

Before

After

You may need translateKeyString if you have a non English (localized) version of InDesign.

Hope it helps.

— Kas

Participant
November 8, 2017

Thanks for the suggestion, Kas.

I'm sure I am overcomplicating things...it's what I do. :-/

I follow what your code is doing. It looks like it should work, but for some reason, for me, it doesn't.

Unfortunately, I don't know enough about how to use the Data Browser to be able to see whether spellingVendor and hyphenationVendorhave been set to "Hunspell." What I do know is that it isn't changing the preferences inside an actual document.

I'm at a loss. Any further suggestions?

Community Expert
November 8, 2017

https://forums.adobe.com/people/one+of+many  wrote

… What I do know is that it isn't changing the preferences inside an actual document.

How do you know that?

Did a test with Kasyan's code and see no flaw.

I changed my settings for English USA from "Hunspell" to "Proximity" in the preferences (UI) before doing a new document. Then I added a new document and ran the following code with my German InDesign CC 2018 that reads out all property/value pairs of the applied language of the text defaults of the active document:

var appliedLanguage = app.documents[0].textDefaults.appliedLanguage;

for(x in appliedLanguage)

{

    $.writeln(x+"\t"+appliedLanguage);

};

Result was this:

name    Deutsch: 2006 Rechtschreibreform

singleQuotes    ‚‘

doubleQuotes    „“

hyphenationVendor    Duden

spellingVendor    Duden

thesaurusVendor    LILO

untranslatedName    de_DE_2006

dictionaryPaths   

icuLocaleName    de_DE

spellingVendorList    Hunspell,Proximity,Duden: Konservativ,Duden: Presse,Duden: Tolerant,Duden,User Dictionary Only

hyphenationVendorList    Hunspell,Proximity,Duden,User Dictionary Only

id    77

label   

isValid    true

parent    [object Application]

index    18

properties    [object Object]

events    [object Events]

eventListeners    [object EventListeners]

isValid    true

Then I ran Kasyan's script and did my little snippet from above again on the same document.

Results had changed to that:

name    Englisch: USA

singleQuotes    ‘’

doubleQuotes    “”

hyphenationVendor    Hunspell

spellingVendor    Hunspell

thesaurusVendor    LILO

untranslatedName    English: USA

dictionaryPaths   

icuLocaleName    en_US

spellingVendorList    Hunspell,Proximity,User Dictionary Only

hyphenationVendorList    Hunspell,Proximity,User Dictionary Only

id    81

label   

isValid    true

parent    [object Application]

index    22

properties    [object Object]

events    [object Events]

eventListeners    [object EventListeners]

isValid    true

And indeed, my standard paragraph style has changed language to "English: USA" reflected in my results as "Englisch USA" according to my German version of InDesign.

Regards,
Uwe