• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help Creating Cross-References Linked to Text Anchors

Explorer ,
Jan 02, 2017 Jan 02, 2017

Copy link to clipboard

Copied

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")

TOPICS
Scripting

Views

919

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Jan 04, 2017 Jan 04, 2017

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.paragra

...

Votes

Translate

Translate
Explorer ,
Jan 02, 2017 Jan 02, 2017

Copy link to clipboard

Copied

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")

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2017 Jan 03, 2017

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 03, 2017 Jan 03, 2017

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 03, 2017 Jan 03, 2017

Copy link to clipboard

Copied

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 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!") 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 03, 2017 Jan 03, 2017

Copy link to clipboard

Copied

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'?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 04, 2017 Jan 04, 2017

Copy link to clipboard

Copied

LATEST

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines