Skip to main content
johnpredmore
Inspiring
January 3, 2017
Answered

Help Creating Cross-References Linked to Text Anchors

  • January 3, 2017
  • 1 reply
  • 1118 views

Hi I'm fairly new to scripting so I'm having trouble modifying the following script. I've gotten it to do all I need to do except that I need to change the link type from URL to Text Anchor. Can you please help?

var myDoc = app.activeDocument; 

app.findGrepPreferences = app.changeGrepPreferences = null; 

 

app.findGrepPreferences.findWhat = "a"; 

app.findGrepPreferences.appliedCharacterStyle = myDoc.characterStyles.item("Superscript");

app.findGrepPreferences.appliedParagraphStyle = myDoc.paragraphStyleGroups.item("Table").paragraphStyles.item("Table Title");

var myFound1 = myDoc.findGrep(); 

 

count = 0 

for(k=0; k<myFound1.length; k++) 

try{ 

    var myFind = myFound1

    var myHyperlinkSource = app.activeDocument.hyperlinkTextSources.add(myFound1

    var myHyperlinkURLDestination = app.activeDocument.hyperlinkURLDestinations.add(myFound1.contents) 

    var myHyperlink = app.activeDocument.hyperlinks.add(myHyperlinkSource, myHyperlinkURLDestination, {name: myFound1.contents+count++}) 

    count++ 

}catch(e){} 

 

alert(count/2 + " Ocurrences is changed")

This topic has been closed for replies.
Correct answer johnpredmore

It should be app.activeDocument.hyperlinkTextDestinations.add (. . .) (mind the 's')

And you need to specify the source: links are from sources to destinations. What are you trying to link? You look for 'a' in a particular character and paragraph style, then you create an anchor at the found a's, then you create a link from somewhere to that a. Where is that 'somewhere'?


Thanks for your explanation Peter; it really helped it click for me. I was trying to link to a "somewhere" (anchor tag) called "a".

Here's the code that worked for me based on your tip:

var myDoc = app.activeDocument;

app.findGrepPreferences = app.changeGrepPreferences = null;

app.findGrepPreferences.findWhat = "a"; // Grep for what you need to find

app.findGrepPreferences.appliedCharacterStyle = myDoc.characterStyles.item("Superscript");

app.findGrepPreferences.appliedParagraphStyle = myDoc.paragraphStyleGroups.item("Table").paragraphStyles.item("Table Title");

var myFound1 = myDoc.findGrep();

count = 0

for(k=0; k<myFound1.length; k++)

{

try{

    var myFind = myFound1;

    var myHyperlinkSource = app.activeDocument.hyperlinkTextSources.add(myFound1

    var myHyperlinkTextDestination = app.activeDocument.hyperlinkTextDestinations.item("a"); // Name of anchor to link to

    var myHyperlink = app.activeDocument.hyperlinks.add (myHyperlinkSource, myHyperlinkTextDestination, {name: myFound1.contents+count++});

    count++

}catch(e){}

}

alert(count/2 + " Hyperlink Cross-References Were Created!")

Thanks again Peter!

1 reply

johnpredmore
Inspiring
January 3, 2017

Here's the script if it didn't show up above for you

var myDoc = app.activeDocument; 

app.findGrepPreferences = app.changeGrepPreferences = null; 

 

app.findGrepPreferences.findWhat = "a"; 

app.findGrepPreferences.appliedCharacterStyle = myDoc.characterStyles.item("Superscript");

app.findGrepPreferences.appliedParagraphStyle = myDoc.paragraphStyleGroups.item("Table").paragraphStyles.item("Table Title");

var myFound1 = myDoc.findGrep(); 

 

count = 0 

for(k=0; k<myFound1.length; k++) 

try{ 

    var myFind = myFound1

    var myHyperlinkSource = app.activeDocument.hyperlinkTextSources.add(myFound1

    var myHyperlinkURLDestination = app.activeDocument.hyperlinkURLDestinations.add(myFound1.contents) 

    var myHyperlink = app.activeDocument.hyperlinks.add(myHyperlinkSource, myHyperlinkURLDestination, {name: myFound1.contents+count++}) 

    count++ 

}catch(e){} 

 

alert(count/2 + " Occurrences Changed")

Peter Kahrel
Community Expert
Community Expert
January 3, 2017

It's indeed confusing that InDesign uses two different terms for the same object: text anchor and text destination.

Change "myHyperlinkURLDestination" to "myHyperlinkTextDestination" three times, and change "myFound1.contents" to "myFound1"

var myHyperlinkTextDestination = app.activeDocument.hyperlinkTextDestinations.add (myFound1);

var myHyperlink = app.activeDocument.hyperlinks.add (myHyperlinkSource, myHyperlinkTextDestination, . . .);

Peter

johnpredmore
Inspiring
January 3, 2017

Thanks for your response Peter, is the following the code you are proposing? I'm still not getting it to work; perhaps you can see where I'm messing up.

var myDoc = app.activeDocument; 

app.findGrepPreferences = app.changeGrepPreferences = null; 

 

app.findGrepPreferences.findWhat = "a"; 

app.findGrepPreferences.appliedCharacterStyle = myDoc.characterStyles.item("Superscript");

app.findGrepPreferences.appliedParagraphStyle = myDoc.paragraphStyleGroups.item("Table").paragraphStyles.item("Table Title");

var myFound1 = myDoc.findGrep(); 

 

count = 0 

for(k=0; k<myFound1.length; k++) 

try{ 

    var myFind = myFound1

    var myHyperlinkTextDestination = app.activeDocument.hyperlinkTextDestination.add(myFound1);

    var myHyperlink = app.activeDocument.hyperlinks.add (myHyperlinkSource, myHyperlinkTextDestination, {name: myFound1.contents+count++}); 

    count++ 

}catch(e){} 

 

alert(count/2 + " Hyperlinks Were Created!") 

Thanks again!