Skip to main content
Participant
May 24, 2018 6:50 PM UTC
Answered

Find and Replace in Hyperlink URL?

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 is closed to new replies. Start a new post to keep the conversation going.
brian_p_dts
brian_p_dtsCorrect answer
Community Expert
Jul 28, 2020 3:07 PM UTC

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){}
};

 

17 replies

Inspiring
May 22, 2019 7:07 AM UTC

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

Translate
Participant
Jul 28, 2020 2:34 PM UTC

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

 

 

 

Translate
brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
Jul 28, 2020 3:07 PM UTC

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){}
};

 

Translate
Benreyn
Participating Frequently
May 24, 2018 8:27 PM UTC

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.

Translate
Inspiring
May 21, 2019 9:51 AM UTC

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!

Translate
Jongware
Community Expert
Community Expert
May 21, 2019 10:00 AM UTC

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');
}

Translate