17 Replies Latest reply on Apr 14, 2015 6:19 AM by Paulo Jorge Carvalho

    Checking spellingVendor and hyphenationVendor

    Paulo Jorge Carvalho

      I have script that checks the used languages in a document and alse the spellingVendor and hyphenationVendor.

      It works almost perfect except when the language is Spanish.

       

      Can anyone tell me why?

       

      Here's the script

      ---------------------------------

       

      main();

      function main(){

          var myDocument = app.activeDocument;

          var dataTemp = [];

          myParStyles=myDocument.allParagraphStyles;

          myCharStyles=myDocument.allCharacterStyles;

       

          for (oneStyle=1;oneStyle<myParStyles.length;oneStyle++){

              dataTemp.push(myParStyles[oneStyle].appliedLanguage.name);

              }

          for (oneStyle=1;oneStyle<myCharStyles.length;oneStyle++){

              try{

                  dataTemp.push(myCharStyles[oneStyle].appliedLanguage.name);

                  }

              catch(err){}

              }

          dataTemp.sort();

          unik(dataTemp);

          var i = 0;

          var dataTemp2 = [];   

          for (i = 0; i < dataTemp.length; i++){

              var temp0 = myDocument.languages.itemByName(dataTemp[i]).name;

       

      var temp1 = myDocument.languages.itemByName(dataTemp[i]).spellingVendor;

              var temp2 = myDocument.languages.itemByName(dataTemp[i]).hyphenationVendor;

              if (temp1 == "Hunspell" | temp2 == "Hunspell"){

                  dataTemp2.push(temp0 + "\r\t\tSpelling: " + temp1 + "\r\t\tHyphenation: " + temp2);

                  }

              }

          if (dataTemp2.length > 0){

              dataTempFinal = dataTemp2.join("\r\t");

              alert(dataTempFinal);

              }

          }

       

      function unik( myDataNum ) {

          for ( var f = 1; f < myDataNum.length; f++ ) {

              if ( myDataNum[f] === myDataNum[ f - 1 ] ) {

                          myDataNum.splice( f--, 1 );

              }

          }

          return myDataNum;

      };

        • 1. Re: Checking spellingVendor and hyphenationVendor
          Jump_Over Level 5

          Hi,

           

          For me it looks like local-names problem.

          Script is using "name" - which is localized already - to refer back to the language. It fails here.

           

          Solution could be to store language.id and refer back using itemByID() method

          OR

          store languages as objects and refer to them directly

           

          Jarek

          • 2. Re: Checking spellingVendor and hyphenationVendor
            Peter Kahrel Adobe Community Professional & MVP

            I don't think it has to do with locality. If you run this script in the ESTK:

             

            lgs = app.documents[0].languages.everyItem().name;
            
            for (i = 1; i < lgs.length; i++) {
                try {
                    $.writeln ('OK: ' + app.documents[0].languages.item(lgs[i]).name);
                } catch (_) {
                    $.writeln ('Problem: ' + lgs[i]);
                }
            }
            
            

             

            Some languages/language names are flagged OK, others not. These flags e.g. are not very clear:

             

            Problem: Norwegian: Bokmål

            OK: Norwegian: Nynorsk

             

            OK: German: Swiss

            Problem: German: Swiss 2006 Reform

            Problem: German: 1996 Reform

            Problem: German: 2006 Reform

            Problem: German: Old Rules

             

            Problem: Spanish

            OK: French

            OK: Italian

             

            Peter

            • 3. Re: Checking spellingVendor and hyphenationVendor
              Paulo Jorge Carvalho Level 1

              Does any one have any idea in how to solve this?

              I've allready tried:

              • itemByName
              • itemByID

              and get allways the same error in Spanish language. I did't tryed with others that .

              • 4. Re: Checking spellingVendor and hyphenationVendor
                Laubender Adobe Community Professional & MVP

                @Jarek – the problem is with itemByName() for language.

                 

                i tested with an empty fresh document of my German InDesign CC 2014.2.

                Only one language in one paragraph style, the one basic paragraph style.


                And that is in my locale named with:

                "Deutsch: 2006 Rechtschreibreform"

                 

                Locale independent that would be:

                "$ID/de_DE_2006"

                 

                You can work with both strings, if you want to apply that language to a text selection in my German version of InDesign like that:

                 

                app.selection[0].appliedLanguage = "$ID/de_DE_2006";
                

                 

                I stripped down Paulo's code a bit, to check at what point it would fail.

                And added a for x in loop to the language object that is called with itemByName()

                 

                var myDocument = app.activeDocument;
                var dataTemp = [];
                var myParStyles=myDocument.allParagraphStyles;
                var myCharStyles=myDocument.allCharacterStyles;
                
                for (var n=1;n<myParStyles.length;n++){
                    dataTemp.push(myParStyles[n].appliedLanguage.name);
                    };
                
                $.writeln(dataTemp);
                //Result:
                //Deutsch: 2006 Rechtschreibreform
                
                var temp1 = myDocument.languages.itemByName(dataTemp[0]);
                //Object is invalid:
                //$.writeln(temp1.spellingVendor);
                
                for(x in temp1){
                    try{
                    $.writeln(x+"\t"+temp1[x]);
                    }catch(e){$.writeln(x+"\t"+e.message);};
                    };
                
                /*
                name    Object is invalid
                singleQuotes    Object is invalid
                doubleQuotes    Object is invalid
                untranslatedName    Object is invalid
                icuLocaleName    Object is invalid
                hyphenationVendor    Object is invalid
                spellingVendor    Object is invalid
                id    Object is invalid
                label    Object is invalid
                isValid    false
                parent    Object is invalid
                index    Object is invalid
                properties    Object is invalid
                events    Object is invalid
                eventListeners    Object is invalid
                isValid    false
                */
                

                 

                So we obviously have an invalid language object stored in variable temp1.

                 

                Instead of that I used the locale independent string and tried again.

                But variable temp1 is still pointing at an invalid language object!

                 

                Somehow, I have a sense of déjà vu…

                 

                Wheras, if I check the appliedLanguage object in my basic paragraph style, the little world of languages seems to be all in order:

                 

                var myDocument = app.activeDocument;
                var dataTemp = [];
                var myParStyles=myDocument.allParagraphStyles;
                var myCharStyles=myDocument.allCharacterStyles;
                
                for (var n=1;n<myParStyles.length;n++){
                    var myAppliedLanguage = myParStyles[n].appliedLanguage;
                    
                    for(x in myAppliedLanguage){
                        $.writeln(x+"\t"+myAppliedLanguage[x]);
                        };
                    
                    };
                
                /*
                name    Deutsch: 2006 Rechtschreibreform
                singleQuotes    ‚‘
                doubleQuotes    „“
                hyphenationVendor    Hunspell
                spellingVendor    Hunspell
                thesaurusVendor    LILO
                untranslatedName    de_DE_2006
                dictionaryPaths    
                icuLocaleName    de_DE
                spellingVendorList    Hunspell,Proximity,User Dictionary Only
                hyphenationVendorList    Hunspell,Proximity,User Dictionary Only
                id    77
                label    
                isValid    true
                parent    [object Application]
                index    18
                properties    [object Object]
                events    [object Events]
                eventListeners    [object EventListeners]
                isValid    true
                */
                

                 

                Jarek, now if we would follow your suggestion and working with the id number alone, maybe there's a bug in itemByName() with the language object?, we could test like that:

                 

                var myDocument = app.activeDocument;
                var dataTemp = [];
                var myParStyles=myDocument.allParagraphStyles;
                var myCharStyles=myDocument.allCharacterStyles;
                
                for (var n=1;n<myParStyles.length;n++){
                    
                    var myAppliedLanguage = myParStyles[n].appliedLanguage;
                    $.writeln("myAppliedLanguage.name:"+"\t"+myAppliedLanguage.name);
                    //Result:
                    //myAppliedLanguage.name:    Deutsch: 2006 Rechtschreibreform
                    
                    var myID = myAppliedLanguage.id;
                    $.writeln("myID:"+"\t"+myID);
                    //Result:
                    //myID:    77
                    
                    //BUT HERE COMES THE PROBLEM:
                    var myLanguageByID = myDocument.languages.itemByID(myID);
                    
                    for(x in myLanguageByID){
                        try{
                        $.writeln(x+"\t"+myLanguageByID[x]);
                        }catch(e){$.writeln(x+"\t"+e.message};
                        };
                    
                    };
                
                /*
                name    Tschechisch
                singleQuotes    ‚‘
                doubleQuotes    „“
                untranslatedName    Czech
                icuLocaleName    cs_CZ
                hyphenationVendor    Hunspell
                spellingVendor    Hunspell
                id    77
                label    
                isValid    true
                parent    [object Document]
                index    13
                properties    [object Object]
                events    [object Events]
                eventListeners    [object EventListeners]
                isValid    true
                */
                

                 

                What? The result i "Czech" instead of German "Deutsch: 2006 Rechtschreibreform" ??!


                paragraphStyle.appliedLanguage

                document.language

                 

                appliedLanguage and language is not from the same collection of languages?

                Hm. Yes… Would be an explanation.

                 

                Please tell me, that there is something dead wrong in my logic…

                 

                Uwe

                 

                PS: To be continued…

                • 5. Re: Checking spellingVendor and hyphenationVendor
                  Laubender Adobe Community Professional & MVP

                  appliedLanguage of a paragraphStyle is a [object LanguageWithVendors]

                  language for the document is a [object Language]

                   

                  Two different collections of objects.

                   

                  Try this and compare:

                   

                  var myLanguagesWithVendors = app.languagesWithVendors.everyItem().getElements(); //length 53
                  var myLanguagesOfDoc = app.documents[0].languages.everyItem().getElements(); //length 53
                  
                  $.writeln("myLanguagesWithVendors");
                  for(var n=0;n<myLanguagesWithVendors.length;n++){
                      $.writeln(n+"\t"+myLanguagesWithVendors[n].id+"\t"+myLanguagesWithVendors[n].name);
                      };
                  
                  $.writeln("myLanguagesOfDoc");
                  for(var n=0;n<myLanguagesOfDoc.length;n++){
                      $.writeln(n+"\t"+myLanguagesOfDoc[n].id+"\t"+myLanguagesOfDoc[n].name);
                      };
                  

                   

                  Uwe

                  • 6. Re: Checking spellingVendor and hyphenationVendor
                    Laubender Adobe Community Professional & MVP

                    So a start for a solution would be to built an array by name (associative array) and for every language name store the ID for both collections.

                    You could later make use of it, if you want to store a third value, maybe usage in your document's paragraph styles or character styles.

                     

                    //Associative Array by name:
                    var myLanguageNameArray = [];
                    
                    var myLanguagesWithVendors = app.languagesWithVendors.everyItem().getElements(); //53
                    var myLanguagesOfDoc = app.documents[0].languages.everyItem().getElements(); //53
                    
                    //$.writeln("myLanguagesWithVendors");
                    for(var n=0;n<myLanguagesWithVendors.length;n++){
                        //$.writeln(n+"\t"+myLanguagesWithVendors[n].id+"\t"+myLanguagesWithVendors[n].name);
                        myLanguageNameArray[myLanguagesWithVendors[n].name] = [ , ];
                        myLanguageNameArray[myLanguagesWithVendors[n].name][0] = myLanguagesWithVendors[n].id;
                        };
                    
                    //$.writeln("myLanguagesOfDoc");
                    for(var n=0;n<myLanguagesOfDoc.length;n++){
                        //$.writeln(n+"\t"+myLanguagesOfDoc[n].id+"\t"+myLanguagesOfDoc[n].name);
                        myLanguageNameArray[myLanguagesOfDoc[n].name][1] = myLanguagesOfDoc[n].id;
                        };
                    
                    for(x in myLanguageNameArray){
                        $.writeln(x+"\t"+myLanguageNameArray[x]);
                        };
                    

                     

                    Uwe

                    • 7. Re: Checking spellingVendor and hyphenationVendor
                      Jump_Over Level 5

                      Hi Uwe,

                       

                      From my side looks like document.languages.everyItem() collects the ones used in doc ==> so there is no need to check para and charStyles, sort() them and unique() them

                      Refering by object (not its name) solution can be:

                       

                      main();
                      function main(){
                          var
                        myDocument = app.activeDocument,
                        mLang = myDocument.languages.everyItem().getElements(),
                        cLang, temp1, temp2,
                        msgString = "";
                      
                        while ( cLang = mLang.pop() ) {
                        temp1 = cLang.spellingVendor;
                        temp2 = cLang.hyphenationVendor;
                        if (temp1 == "Hunspell" || temp2 == "Hunspell")
                        msgString += cLang.name + "\tSpelling: " + temp1 + "\tHyphenation: " + temp2 + "\r";
                        }
                        if (msgString.length)
                        alert ( msgString.slice(0,-1) );
                        else alert ("No languages filtered by 'Hungspell' found");
                        }
                      

                       

                      Jarek

                      • 8. Re: Checking spellingVendor and hyphenationVendor
                        Laubender Adobe Community Professional & MVP

                        Hm, I think this will not work.

                         

                        The length of mLang will be 53 with my InDesign's document.

                        Used is only one single language, the one in my basic paragraph style named "Deutsch: 2006 Rechtschreibreform".

                        (It's just an empty document)

                         

                        Your alert will give me the whole list of 53 entries.

                         

                        Uwe

                        • 9. Re: Checking spellingVendor and hyphenationVendor
                          Jump_Over Level 5

                          Hm,

                           

                          Have got some documents (not every) where it is a useful differences between

                          app.documents[0].languages.everyItem().getElements().length;     ==> 5

                               and

                          app.languagesWithVendors.everyItem().getElements().length;     ==> 40

                           

                          but new doc makes it equal indeed

                          CS5 and Window 7

                           

                          Jarek

                          • 10. Re: Checking spellingVendor and hyphenationVendor
                            Jump_Over Level 5

                            Hm,

                             

                            Is possible walk through IDML format makes it this way?

                            (only used languages in doc collection)

                             

                            Jarek

                            • 11. Re: Checking spellingVendor and hyphenationVendor
                              Laubender Adobe Community Professional & MVP

                              @Jarek – by building an associative array of both collections, you could always fetching missing ones.

                              Check with every arbitrary object that uses the name of a specific language and build a statistic out of it, if you like.

                               

                              To assign a specific language, you can always use its name directly.

                              Or its locale independent name. At least in nearly all cases.

                               

                              For international usage, there are some languages, that have no locale independent names.

                              That's a bug. Here a list of the languages in English:

                               

                              Bengali (India)

                              Gujarati (India)

                              Hindi (India)

                              Kannada (India)

                              Malayalam (India)

                              Marathi (India)

                              Oriya (India)

                              Punjabi (India)

                              Tamil (India)

                              Telugu (India)

                               

                               

                              Uwe

                              • 12. Re: Checking spellingVendor and hyphenationVendor
                                Laubender Adobe Community Professional & MVP

                                @Jarek – yes, I can see now some differences in the length of both collections.

                                 

                                That's for InDesign CS6 and CS5.5.

                                New, empty documents vs. not empty ones (or documents opened from IDML)

                                Not so in CC v9.3.0 and CC 2014.2.

                                 

                                But I'm not sure.

                                I have to test on a bit…

                                 

                                Uwe

                                • 13. Re: Checking spellingVendor and hyphenationVendor
                                  Laubender Adobe Community Professional & MVP

                                  Hm, after a bit of saving, opening and creating new documents with various versions of inDesign…

                                   

                                  Stong assumption: I can see always a difference in both collections, if the document was opened from an IDML file.

                                   

                                  Stong assumption: I can see no difference, if it was a new, empty document.

                                  Weak assumption: I can see no difference, if the document's origin never changed. Never was opened and saved in newer versions, never was opened from IDML or no IDMS files were ever placed.

                                   

                                  So to think, that document.languages is always showing the used languages in the document is simply wrong.

                                  Tested with CS5.5 v7.5.3, CS6 v8.1.0, CC 9.3.0 and CC 2014.2 v10.2.0 on Mac OSX 10.7.5.

                                   

                                  Uwe

                                  • 14. Re: Checking spellingVendor and hyphenationVendor
                                    Jump_Over Level 5

                                    Hi Uwe,

                                     

                                    I agree.

                                    However I would say it is alike oversight (of Adobe).

                                    An empty document has 1 font and plenty of languages.

                                    And value of this property is erased to real numbers after reverting doc by IDML (which keeps complete doc structure, I guess).

                                     

                                    Jarek

                                    • 15. Re: Checking spellingVendor and hyphenationVendor
                                      Peter Kahrel Adobe Community Professional & MVP

                                      By some coincidence I needed to get a valid hook on a language today, and came up with this function:

                                       

                                      function getLanguage (str) {
                                          var lg = app.languagesWithVendors.item(str);
                                          if (!app.languagesWithVendors.item(str).isValid) {
                                              lg = null;
                                              var lgs = app.languagesWithVendors.everyItem().getElements();
                                              for (var i = lgs.length-1; i > 0; i--) {
                                                  if (lgs[i].name === str) {
                                                      lg = lgs[i];
                                                      break;
                                                  }
                                              }
                                          }
                                          return lg;
                                      }
                                      
                                      

                                       

                                      Sample call:

                                       

                                      var language = getLanguahe ('Spanish');

                                       

                                      This is what I'll use to set languages in paragraph styles and what not.

                                       

                                      Peter

                                      • 16. Re: Checking spellingVendor and hyphenationVendor
                                        Paulo Jorge Carvalho Level 1

                                        Thanks Peter,

                                         

                                        This function solved my problem.

                                        • 17. Re: Checking spellingVendor and hyphenationVendor
                                          Paulo Jorge Carvalho Level 1

                                          Just find out that the function was checking the app.languages and not the open document languages.

                                           

                                          Changed the code to:

                                          ...

                                          var temp0 = getLanguage (dataTemp[i], myDocument);

                                          var temp1 = temp0.spellingVendor;

                                          var temp2 = temp0.hyphenationVendor;

                                          ...

                                          function getLanguage (str, myDocument) { 

                                               var lg = myDocument.languages.item(str); 

                                               if (!myDocument.languages.item(str).isValid) { 

                                                    lg = null; 

                                                    var lgs = myDocument.languages.everyItem().getElements(); 

                                                    for (var i = lgs.length-1; i > 0; i--) { 

                                                         if (lgs[i].name === str) { 

                                                              lg = lgs[i]; 

                                                              break; 

                                                         }

                                                    }

                                               } 

                                               return lg; 

                                          }

                                           

                                          Now it's working as i need.

                                           

                                          Thank you all.