Skip to main content
Participant
May 24, 2018
Answered

Find and Replace in Hyperlink URL?

  • May 24, 2018
  • 2 replies
  • 6228 views

I have a product catalog with many dozens of URLs. I make versions of this with the affiliate link for each salesperson. Is there a way to find and replace their affiliate code in each URL in the document? The URL does not appear in the text itself.

i.e. https://aqsperformance.com/pages/laohs.htm?affiliate=stacieh needs to become https://aqsperformance.com/pages/laohs.htm?affiliate=kristing and there are dozens of similar links throughout the document where stacieh needs to become kristing

This topic has been closed for replies.
Correct answer brian_p_dts

Try this instead. The word boundary (\b) may be throwing things off. Also, some of your Hyperlink Destination objects may not have a URL Destinations assigned to them, so wrapping the code in a try/catch block will skip over any of those. 

var doc = app.activeDocument;
for(var i = 0; i < doc.hyperlinkURLDestinations.length; i++) {
try { 
doc.hyperlinkURLDestinations.item(i).destinationURL = doc.hyperlinkURLDestinations.item(i).destinationURL.replace("MR7", "MR9");
} catch(e){}
};

 

2 replies

Klaas vT
Inspiring
May 22, 2019

Just to end this off well, I think the script below wold do what the OP wants:

marcusg99127037  wrote

I have a product catalog with many dozens of URLs. I make versions of this with the affiliate link for each salesperson. Is there a way to find and replace their affiliate code in each URL in the document? The URL does not appear in the text itself.

i.e. https://aqsperformance.com/pages/laohs.htm?affiliate=stacieh needs to become https://aqsperformance.com/pages/laohs.htm?affiliate=kristing and there are dozens of similar links throughout the document where stacieh needs to become kristing

var doc = app.activeDocument;

for(var i = 0; i < doc.hyperlinkURLDestinations.length; i++) {

    doc.hyperlinkURLDestinations.item(i).destinationURL = doc.hyperlinkURLDestinations.item(i).destinationURL.replace(/\stacieh\b/, 'kristing');

}

Klaas

Participant
July 28, 2020

I'm trying to apply this script for my purposes. I'm trying to replace "MR7" with "MR9" in the this line of hyperlink code:

https://www.amanet.org/2211/?pcode=MR7&utm_source=catalog&utm_medium=digital&utm_campaign=Summer-BOS-2020

My script:

var doc = app.activeDocument;

for(var i = 0; i < doc.hyperlinkURLDestinations.length; i++) {

doc.hyperlinkURLDestinations.item(i).destinationURL = doc.hyperlinkURLDestinations.item(i).destinationURL.replace(/\MR7\b/, 'MR9');

};

 

I get an error message:

I don't know scripting and I don't know what this means.

Thank you,

Tony

 

 

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
July 28, 2020

Try this instead. The word boundary (\b) may be throwing things off. Also, some of your Hyperlink Destination objects may not have a URL Destinations assigned to them, so wrapping the code in a try/catch block will skip over any of those. 

var doc = app.activeDocument;
for(var i = 0; i < doc.hyperlinkURLDestinations.length; i++) {
try { 
doc.hyperlinkURLDestinations.item(i).destinationURL = doc.hyperlinkURLDestinations.item(i).destinationURL.replace("MR7", "MR9");
} catch(e){}
};

 

Benreyn
Participating Frequently
May 24, 2018

Hi Marcus,

Here is a simple script that should find and replace all the hyperlinks:

var doc = app.activeDocument;

for(var i = 0; i < doc.hyperlinks.length; i++) {

    if(doc.hyperlinks.item(i).destination.name === "http://www.mysite.com?=sometext") {

        doc.hyperlinks.item(i).destination.name = "http://www.mysite.com?=newValue";

    }

}

Hope it helps!

EDIT: I changed the .item(0) to .item(i), that was my error. Now it should loop properly.

Klaas vT
Inspiring
May 21, 2019

I've got a similar problem, but I need to replace the .com in all the hyperlink to .de. Can I do that with GREP or how would you tweak the script above to make that work? The document has a couple hundred hyperlinks... I guess I could get jiggy with the XML of the file itself but this might be easier...

Thanks!

Jongware
Community Expert
Community Expert
May 21, 2019

An untested adjustment of Benreyn's code above (which totally undeserved did not get the Correct Answer badge of honour, or even an acknowledgement that it worked...):

var doc = app.activeDocument;

for(var i = 0; i < doc.hyperlinks.length; i++) {
    doc.hyperlinks.item(i).destination.name = doc.hyperlinks.item(i).destination.name.replace(/\.com\b/, '.de');
}