// Adjustendnotenumbers.jsx // Increases or decreases numbers of endnotes // Presuppositions: the endnote numbers in the text must have the // character style chosen in the dialog displayed at starting the script. // The Endnote text must have the paragraph style chosen in // the dialog displayed at starting the script. // The endnote separator must be the character specified in // the dialog displayed at starting the script and must not // be used in paragraphs in endnotes that have no endnote number. // Made by Teus de Jong; last version August 29, 2005 // Made compatible with CS3 April 2007 // Updated by Anders Skogheim Liane july 2009 // Thanks Anders!! if (app.documents.length == 0) errorExit('No documents are open'); var doendnotes = true; var startnumber, endnumber, increase; endnotereferencestyle = 'Endnote Reference'; endnotetextstyle = 'Endnote Text'; endnoteseparator = '^t'; // tab appversion = Number(app.version.substr(0,1)); app.scriptPreferences.version = appversion; if (!doDisplayDialog()) {exit()} resetFind(); notes = findNotes(endnotereferencestyle); if (notes.length == 0) errorExit('No note references found'); for (i = notes.length-1; i > -1; i--){ str = notes[i].contents; newstr = increasenumber(str); if (newstr != str){ notes[i].contents = newstr; } } resetFind(); if (!doendnotes){exit()} notetexts = findNoteTexts(endnotetextstyle); for (t = notetexts.length -1; t > -1;t--){ for (i = notetexts[t].paragraphs.length-1; i > -1; i--){ str = notetexts[t].paragraphs[i].contents; if (str.indexOf(endnoteseparator) == -1) {continue} try{ str = notetexts[t].paragraphs[i].words[0].contents; newstr = increasenumber(str); if (newstr != str){ notetexts[t].paragraphs[i].words[0].contents = newstr; } } catch (e) {} } } resetFind(); function increasenumber(s){ var x; var havepoint = false; // we check if there is a point at the end of the note number if (s.indexOf('.') == s.length - 1){ s = s.split('.')[0]; havepoint = true; } x = Number(s); if ((x >= startnumber) && (x <= endnumber)){ x = x + increase; } s = x.toString(); if (havepoint){ s = s+'.'; } return(s); } function doDisplayDialog(){ with (theDialog = app.dialogs.add({name:'Increase, decrease endnote numbers'})){ //Add a dialog column. DialogColumn1 = dialogColumns.add(); with (DialogColumn1){ staticTexts.add({staticLabel:'Start number:'}); staticTexts.add({staticLabel:'End Number:'}); staticTexts.add({staticLabel:'Increase with:'}); staticTexts.add({staticLabel:'Endnote reference:'}); staticTexts.add({staticLabel:'Endnote text:'}); staticTexts.add({staticLabel:'Handle numbers in notes:'}); staticTexts.add({staticLabel:'Endnote separator:'}); } DialogColumn2 = dialogColumns.add(); with (DialogColumn2){ with (editstartnumber = integerEditboxes.add()){ editValue = 1; } with (editendnumber = integerEditboxes.add()){ editValue = 100; } with (editincrease = integerEditboxes.add()){ editValue = 2; } with (endnotereferencebox = dropdowns.add()){ stringList = app.activeDocument.characterStyles.everyItem().name; for (i = 0; i < stringList.length; i++){ if (stringList[i] == endnotereferencestyle){ selectedIndex = i; break; } } } with (endnotetextbox = dropdowns.add()){ stringList = app.activeDocument.paragraphStyles.everyItem().name; for (i = 0; i < stringList.length; i++){ if (stringList[i] == endnotetextstyle){ selectedIndex = i; break; } } } with (doends = checkboxControls.add()){ checkedState = true; } with (endnotesepbox = textEditboxes.add()){ editContents = endnoteseparator; } } if (theDialog.show()) { startnumber = editstartnumber.editValue; endnumber = editendnumber.editValue; increase = editincrease.editValue; endnotereferencestyle = endnotereferencebox.stringList[endnotereferencebox.selectedIndex]; endnotetextstyle = endnotetextbox.stringList[endnotetextbox.selectedIndex]; doendnotes = doends.checkedState; endnoteseparator = endnotesepbox.editContents; switch (endnoteseparator){ case '^t': case '\t': {endnoteseparator = '\u0009'; break} case '^>': {endnoteseparator = '\u2002'; break;} case '^m': {endnoteseparator = '\u2003'; break;} case '^s': {endnoteseparator = '\u00A0'; break;} } theDialog.destroy; return(true); } else{ theDialog.destroy(); return(false); } } } function findNotes(x){ resetFind(); var u = undefined; if (appversion == 4) return(app.activeDocument.search(u, u, u, u, {appliedCharacterStyle: x})); else { app.findTextPreferences.appliedCharacterStyle = x; return(app.activeDocument.findText()); } } function findNoteTexts(x){ resetFind(); var u = undefined; if (appversion == 4) return(app.activeDocument.search(u, u, u, u, {appliedParagraphStyle: x})); else { app.findTextPreferences.appliedParagraphStyle = x; return(app.activeDocument.findText()); } } function resetFind(){ if (appversion == 4) app.findPreferences = null; else app.findTextPreferences = null; }