Index from Character Styles Script Error...

Copy link to clipboard
Copied
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:
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;
}
Copy link to clipboard
Copied
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);
}
}
Copy link to clipboard
Copied
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 🙂
Copy link to clipboard
Copied
(Although my simple-minded version of it should work OK if you just copy and paste from the posting above. -- Jeremy)
Copy link to clipboard
Copied
I've fixed that script.
Peter

