Script function trying to link terms to text anchors isn't working as intended
A function that is part of a larger script isn't working as intended. The purpose of the function is to identify all paragraphs on a parent page with a specified style and link each paragraphs text via a hyperlink to a previously created text anchor with a name identical to the paragraphs content. I've used the hyperlink.add() method successfully before, but the source was some user selected text and the destination was a url composed of a bit of javascript. This time, i'm having trouble getting the hyperlinkTextSource and hyperlinkTextDestination to work appropriately. the code below is the function in question with plenty of notes for each line.
linkPopupBtn.onClick = function () {
var glossPage;
var textAnchors = doc.hyperlinkTextDestinations; //text anchors created in a previous, not shown, step.
for (var i = 0; i < app.activeDocument.masterSpreads.length; i++) { //find my glossary parent page
if (app.activeDocument.masterSpreads[i].name == "L-Glossary") {
glossPage = app.activeDocument.masterSpreads[i].pages[0];
break;
}
}
var glossFrame = glossPage.textFrames[0]; //only the glossary text frame exists on this page
app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
app.findGrepPreferences.appliedParagraphStyle = doc.paragraphStyleGroups.item("Glossary").paragraphStyles.item("Glossary Term Word"); //find all paragraphs with the specified paragragh style that denotes the term as opposed to defintion
var paras = glossFrame.findGrep();
var alertText = ""; //start a blank alert
for (i = 0; i < paras.length; i++) { //loop through all identified paragraphs
for (j = 0; j < textAnchors.length; j++) { //loop through all text anchors
var testAnchor = textAnchors[j].name + "\r"; //add a carriage return for the alert, and previously used in the comparator
try{ //the alert below will not trigger without the try/catch, but no error ever appears
if(paras[i].contents.split("\r")[0] == textAnchors[j].name) { //the identified paragraphs all end in a carriage return, remove it to compare to the anchor names
alertText += paras[i].contents.split("\r")[0] + ":" + testAnchor; //this works and the alert shows all the glossary terms have matched to text anchor created in a previous, not shown, step
var glossPopTerm = doc.hyperlinkTextSources.add(paras[i]); //this should make the identified paragraph containing the glossary term as the hyperlink source, so you can click on it, but doesn't work
var glossPopDest = doc.hyperlinkTextDestinations.item(textAnchors[j]); //this should make the positive matched text anchor the destination of the glossary term above, but doesn't work
doc.hyperlinks.add(glossPopTerm, glossPopDest, {name:paras[i].contents.split("\r")[0]}); //this should create the actual hyperlink, but doesn't work
var style = doc.characterStyles.itemByName("Underline"); //find the underline character style
paras[i].applyCharacterStyle(style); //apply an underline to the glossary term to show the link has worked appropriately. works if the hyperlink steps above are omitted but not if they are included
}
else {
alertText += para[i].contents + ":No Matches Found." //never triggers, as there should be a match for every work if previous, not shown, steps are followed appropriately
}
}
catch(error){} //never triigers
}
}
alert(alertText); //creates an alert showing all the matched pairings and unmatched terms. triggers as long as the try/catch is there or the hyperlink steps are omitted
}
