4 Replies Latest reply on Apr 25, 2009 2:25 PM by Peter Kahrel

    Index from Character Styles Script Error...


      I am trying to use Peter Kahrel's script to generate Index entries from character styles.  The script is located here:




      However, when I run it, I am getting the following error:

      JavaScript  Error!
      Error Number:  21
      Error String:  undefined is not an object
      File:/Applications/Adobe InDesign CS4/Scripts/Scripts  Panel/Chris/index-char-styles.jsx
      Line:  33
      Source:    for (var  j = 0;j < character_styles.length; j++)

      As I am new to scriptin in InDesign, I am not familiar with what this error is trying to tell me.


      I tried it with redefining in the script the character styles I want it to use, and keeping it exactly how it came, but both generate the same error.



      The full code is also listed below:

      #target indesign
      // List the character styles to be included:
      character_styles = ["bold", "index"];
      // Only Indesign 5 (CS3) and higher
      if (parseInt (app.version) < 5)
           exit ();
      index_from_character_styles (character_styles);
      function index_from_character_styles (character_styles)
           app.findGrepPreferences = app.changeGrepPreferences = null;
           app.findChangeGrepOptions.includeFootnotes = true;
           displ = init_progress ();
           index_documents (displ);
      function index_documents (displ, character_styles)
           for (var i = 0; i < app.documents.length; i++)
                var doc = app.documents[i];
                displ.text = doc.name;
                if (doc.indexes.length == 0)
                     doc.indexes.add ();
                for (var j = 0; j < character_styles.length; j++)
                     app.findGrepPreferences.appliedCharacterStyle = character_styles[j];
                     var found = doc.findGrep();
                     for (k = found.length-1; k > -1; k--)
                               var new_topic = doc.indexes[0].topics.add (found[k].contents);
                               new_topic.pageReferences.add (found[k], PageReferenceType.currentPage);
                          catch(_) {/* not interested in errors */}
      function errorM (m)
           alert (m, 'Error', true);
      function init_progress ()
           index_progress = new Window ('palette', 'Concordance' );
           index_progress.alignChildren = ['left', 'top'];
           fname = index_progress.add ('statictext', undefined, '------');
           fname.characters = 40;
           return fname;
        • 1. Re: Index from Character Styles Script Error...
          Jeremy bowmangraphics-DQuh1B Level 3

          You might try my own version of this script, which is basically Peter's main idea applied in a simpler, less elegant, less polished way. It differs from his script in that it presents a dialog box which asks you to write in the name of the character style to be added to the index. It's pretty basic -- don't try anything too ambitious -- and it will only add first-level index entries for "current page". If you don't have an index already, it makes one, and successive runs add entires to the index rather than making a new index. It's just for simple alphabetical directories, not for "real" back-of-book indexes.





          var myDocument = app.activeDocument;

          var myDialog = app.dialogs.add({name:"Enter char style name to be added to index",canCancel:true});



          var myTextEditField = textEditboxes.add({editContents:"Character Style name", minWidth:200});



          var myResult = myDialog.show();


          if(myResult == true){

          var myCharacterStyle = myTextEditField.editContents;


          if (myDocument.indexes.length == 0){



          app.findGrepPreferences = null;

          app.changeGrepPreferences = null;

          app.findGrepPreferences.appliedCharacterStyle = myCharacterStyle;

          var myFinds = myDocument.findGrep();

          var myHowManyEntries = myFinds.length;

          for (i = myHowManyEntries-1; i >= 0; i--){

          var myTopic = myDocument.indexes[0].topics.add(myFinds[i].contents);

          myTopic.pageReferences.add (myFinds[i],PageReferenceType.CURRENT_PAGE);



          • 2. Re: Index from Character Styles Script Error...
            [Jongware] Most Valuable Participant

            A warning to copiers of the script: Those   entries should not be there! The original file at Peter's site just have tabs there.


            I'd hazard this for the 'undefined' warning:


            It occurs when first referencing the array 'character_styles' inside the function index_documents. This is a local variable (because it is mentioned by name in the function header), and not the global variable of the same name that gets defined on the 5th line. This local variable is undefined because the function is called on line 20 with just 1 argument, not 2 -- any unmentioned arguments apparently are 'undefined' by default.

            Changing line 20 to


            index_documents (displ, character_styles);


            might solve the problem. (It 'might' because I'm not behind a CS3 machine now :-)

            • 3. Re: Index from Character Styles Script Error...
              Jeremy bowmangraphics-DQuh1B Level 3

              (Although my simple-minded version of it should work OK if you just copy and paste from the posting above. -- Jeremy)

              • 4. Re: Index from Character Styles Script Error...
                Peter Kahrel Adobe Community Professional & MVP

                I've fixed that script.