Skip to main content
fabula_rasa
Participant
October 10, 2021
Answered

Script - Appending page numbers to text anchor names

  • October 10, 2021
  • 1 reply
  • 280 views

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();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------

This topic has been closed for replies.
Correct answer brian_p_dts

Gave it a shot here. It should skip adding the page name if the found text exists on the pasteboard.

 

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;
//____________NEW ADDITIONS__________________
var foundPage = finds[j].parentTextFrames[0].parentPage;
var fpName = "";
if (foundPage && foundPage.isValid) { 
    fpName = "-" + foundPage.name;
}
//____________END NEW ADDITION________________

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+$/, "") + /*____NEW ADD___*/fpName/*___END___*/ + "-" + 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();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------

 

1 reply

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
October 10, 2021

Gave it a shot here. It should skip adding the page name if the found text exists on the pasteboard.

 

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;
//____________NEW ADDITIONS__________________
var foundPage = finds[j].parentTextFrames[0].parentPage;
var fpName = "";
if (foundPage && foundPage.isValid) { 
    fpName = "-" + foundPage.name;
}
//____________END NEW ADDITION________________

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+$/, "") + /*____NEW ADD___*/fpName/*___END___*/ + "-" + 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();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------

 

fabula_rasa
Participant
October 10, 2021

Works like a charm! Awesome!

Can't thank you enough 🙂