// Cleanindex.jsx // This script is meant to clean an imported index // e.g. from Word for use in InDesign. // The script does two things: // 1. When there are several instances of an entry // on the same page, all except the first are set to non pronting. // 2. If there are more than 'minconsecs' instances of the same entry // on consecutive pages, the first is set to ForNextNPages // and the rest to non printing (the default of minconsecs is 3, // you can change this to 2 in the startup window). // If a book is open, all files in the book will be handled, // if no book is open, the active document will be handled. // Made by Teus de Jong, last version August 28, 2005 if (!doDisplayDialog()){exit()} if (app.books.length == 0){ if (app.documents.length == 0){ errorExit('No documents are open'); } else { nrofdocs = 1; } } else { nrofdocs = app.books[0].bookContents.length; for (i = nrofdocs - 1; i > -1; i--){ app.open(File(app.books[0].bookContents[i].fullName)); } } doc = app.activeDocument; for (d = 1; d <= nrofdocs; d++){ IndIndex = doc.indexes[0]; IndIndex.update(); alltops = IndIndex.allTopics; nroftopics = alltops.length; if (nroftopics == 0){continue} for (i = 0; i < nroftopics; i++){ DoTopic(alltops[i]); } if (d >= nrofdocs){ break; } doc = app.open(app.books[0].bookContents[d].fullName); } function DoTopic(IndTopic){ var i, s; var pags = []; var indices = []; with (IndTopic){ nrofprefs = pageReferences.length; if (nrofprefs < 2){return} // make double topics non printing for (i = 0; i < nrofprefs - 1; i++){ pagref1 = pageReferences[i]; pag1 = GetPageNr(pagref1.sourceText); if (i == 0){ pags.push(pag1); indices.push(0); } pagref2 = pageReferences[i+1]; pag2 = GetPageNr(pagref2.sourceText); if (pag1 == pag2){ pagref2.pageReferenceType = PageReferenceType.suppressPageNumbers; } else { indices.push(i+1); pags.push(pag2); } } if (nrofprefs < minconsecs){return} // make ranges of more than minconsec consecutive pages consecs = 0; i = 0; b = i; while (i < pags.length - 1){ if (pags[i+1] == pags[i]+1){ consecs++; i++; if (i < pags.length - 1){ continue; } } if (consecs >= minconsecs){ for (j = b + 1; j <= i; j++){ pageReferences[indices[j]].pageReferenceType = PageReferenceType.suppressPageNumbers; } pageReferences[indices[b]].pageReferenceType = PageReferenceType.forNextNPages; pageReferences[indices[b]].pageReferenceLimit = pags[i] - pags[b] + 1; } i++; consecs = 0; b = i; } } } function GetPageNr(txt){ var pg = txt.parentTextFrames[0].parent; while (pg.constructor.name != 'Page'){ if (pg.constructor.name == 'Character'){ pg = pg.parentTextFrames[0]; } pg = pg.parent; if (pg == null){break} } if (pg != null){ return(pg.documentOffset) } else { alert('Could not get page number; exiting...'); exit() } } function errorExit(s){ alert(s); exit(); } function doDisplayDialog(){ var i; with (theDialog = app.dialogs.add({name:'Minimum number'})){ with (DialogColumn1 = dialogColumns.add()){ staticTexts.add({staticLabel:'Minimum number of entries to be combined'}); with (edmin = radiobuttonGroups.add()){ radiobuttonControls.add({staticLabel:'2'}); radiobuttonControls.add({staticLabel:'3', checkedState: true}); } } if (show()) { minconsecs = edmin.selectedButton + 1; destroy; return(true); } else{ destroy(); return(false); } } }