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")
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
...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
var myHyperlink = app.activeDocument.hyperlinks.add(myHyperlinkSource, myHyperlinkURLDestination, {name: myFound1
count++
}catch(e){}
}
alert(count/2 + " Occurrences Changed")
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
var myHyperlinkTextDestination = app.activeDocument.hyperlinkTextDestinations.add (myFound1
); var myHyperlink = app.activeDocument.hyperlinks.add (myHyperlinkSource, myHyperlinkTextDestination, . . .);
Peter
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!
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
count++
}catch(e){}
}
alert(count/2 + " Hyperlinks Were Created!")
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'?
Copy link to clipboard
Copied
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
count++
}catch(e){}
}
alert(count/2 + " Hyperlink Cross-References Were Created!")
Thanks again Peter!