Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
… 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
Copy link to clipboard
Copied
Found this thread when trying to do de same thing.
Kas script modified to change the document (myDoc):
Main();
function Main() {
var myDoc = app.activeDocument;
var languageWithVendors = app.languagesWithVendors.itemByName("Swedish");
myDoc.languageWithVendors.hyphenationVendor = "Hunspell";
myDoc.languageWithVendors.spellingVendor = "Hunspell";
myDoc.textDefaults.appliedLanguage = languageWithVendors;
}
But that did not work. Perhaps I need to translate with app.translateKeyString, but can't get it to work.
Tried the other script and it is working when i create a new document, but not for all our templates. Anyone know why?:
Main();
function Main() {
if (app.documents.length > 0) {
var myDoc = app.activeDocument;
var languages = app.languagesWithVendors.everyItem().getElements();
for (var n=0; n<languages.length; n++) {
if (languages
.name == app.translateKeyString("$ID/Swedish")) { myDoc.languages
.hyphenationVendor = "Hunspell"; myDoc.languages
.spellingVendor = "Hunspell"; }
}
}
}
Copy link to clipboard
Copied
After a little further experimentation, I've discovered that when I run the script on my machine, it will set the default preferences for InDesign, but doesn't appear to set the preferences for the document itself. I'm sure I'm missing something, but I just don't know what.
Did figure out how to use the Data Browser, and it does show the values of spellingVendor and hyphenationVendor changing from Proximity (when the ID default is set to Proximity) to Hunspell. However, the preferences of the document don't change.
Any further suggestions would be welcome.
Copy link to clipboard
Copied
Old question, but I had the same problem, but I found the way to do it, and if someone still wonders, here it is.
Example for Swedish, but you can change to any of the languages available:
//First define which language to do the setting on:
Copy link to clipboard
Copied
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