Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Index from Character Styles Script Error...

Guest
Apr 24, 2009 Apr 24, 2009

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

http://www.kahrel.plus.com/indesign/index_charstyles.html

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;
          displ.text = doc.name;
          if (doc.indexes.length == 0)
               doc.indexes.add ();
          else
               doc.indexes[0].topics.everyItem().remove();
          for (var j = 0; j < character_styles.length; j++)
               {
               app.findGrepPreferences.appliedCharacterStyle = character_styles;
               var found = doc.findGrep();
               for (k = found.length-1; k > -1; k--)
                    {
                    try
                         {
                         var new_topic = doc.indexes[0].topics.add (found.contents);
                         new_topic.pageReferences.add (found, PageReferenceType.currentPage);
                         }
                    catch(_) {/* not interested in errors */}
                    }
               }
          }
     }


function errorM (m)
     {
     alert (m, 'Error', true);
     exit()
     }


function init_progress ()
     {
     index_progress = new Window ('palette', 'Concordance' );
     index_progress.alignChildren = ['left', 'top'];
     fname = index_progress.add ('statictext', undefined, '------');
     fname.characters = 40;
     index_progress.show();
     return fname;
     }
TOPICS
Scripting
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 25, 2009 Apr 25, 2009

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.

//IndexFromCharStyle.jsx

var myDocument = app.activeDocument;

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

with(myDialog){

with(dialogColumns.add()){

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

}

}

var myResult = myDialog.show();

if(myResult == true){

var myCharacterStyle = myTextEditField.editContents;

myDialog.destroy();

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

myDocument.indexes.add();

}

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.contents);

myTopic.pageReferences.add (myFinds,PageReferenceType.CURRENT_PAGE);

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 25, 2009 Apr 25, 2009

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 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Apr 25, 2009 Apr 25, 2009

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 25, 2009 Apr 25, 2009
LATEST

I've fixed that script.

Peter

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines