Script - Appending page numbers to text anchor names
Hello all!
I have this script that automatically makes text anchors of all words in certain character style and names them with "that word + incremental number". Is there a way to make it also append a page number the word was found on to the text anchor name?
Any help would be greatly appreciated!
Here is the script (created by Kasyan Servetsky):
if (app.documents.length == 0) ErrorExit("Please open a document and try again.");
const gScriptName = "Create Text Anchors";
const gScriptVersion = "2.0";
var gDoc = app.activeDocument;
if (!gDoc.characterStyles.item("Anchor").isValid) ErrorExit("Character style \"Anchor\" doesn't exist.");
CreateDestinations();
//======================================================== FUNCTIONS =====================================================
function CreateDestinations() {
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.findWhat = ".+";
app.findGrepPreferences.appliedCharacterStyle = gDoc.characterStyles.item("Anchor");
var finds = gDoc.findGrep();
var destCounter = 0;
for ( var j = finds.length-1; j >= 0; j-- ) {
var found = finds[j];
var name = found.contents;
try {
if (!gDoc.hyperlinkTextDestinations.itemByName(name).isValid) {
var hypTextDest = gDoc.hyperlinkTextDestinations.add(found);
hypTextDest.name = name;
destCounter++;
}
else {
var increment = 1;
while (gDoc.hyperlinkTextDestinations.itemByName(name).isValid) {
name = name.replace(/\-\d+$/, "") + "-" + increment++;
}
var hypTextDest = gDoc.hyperlinkTextDestinations.add(found);
hypTextDest.name = name;
destCounter++;
}
}
catch(e) {}
}
if (destCounter == 0) {
alert("No text anchors have been created.", gScriptName + " - " + gScriptVersion);
}
else if (destCounter == 1) {
alert("One text anchor has been created.", gScriptName + " - " + gScriptVersion);
}
else if (destCounter > 1) {
alert(destCounter + " text anchors have been created.", gScriptName + " - " + gScriptVersion);
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ErrorExit(error, icon) {
alert(error, gScriptName + " - " + gScriptVersion, icon);
exit();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------