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

Find and Replace in Hyperlink URL?

New Here ,
May 24, 2018 May 24, 2018

Copy link to clipboard

Copied

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

TOPICS
Scripting

Views

4.1K

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 2 Correct answers

Participant , May 24, 2018 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.

Votes

Translate

Translate
Community Expert , Jul 28, 2020 Jul 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){}
};

 

Votes

Translate

Translate
Participant ,
May 24, 2018 May 24, 2018

Copy link to clipboard

Copied

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.

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 ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

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!

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 ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

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

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 ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

https://forums.adobe.com/people/%5BJongware%5D  wrote

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

Hi Jongware,

Thanks for the reply, but nothing happens yet... Here are the actual hyperlinks:

https://www.communityplaythings.co.uk/products/outdoor-play/outlast-sets/w389

should become

https://www.communityplaythings.de/products/outdoor-play/outlast-sets/w389

Does that make a difference?

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 ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

In your previous query you asked to replace .com with .de. However i see that the url's that you shared have .co.uk instead of .com hence its not working. To make it work with this, change /\.com\b/, '.de' to /\.co.uk\b/, '.de' in the code that [Jongware]​ shared.

-Manan

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 ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

Yes, I'd gotten that far.

Turns out it did work for the normal hyperlinks, what's not working is that the document often has the hyperlink on the text boxes, rather than on the text itself. Maybe those need to be called differently?

This is the script I ran:

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(/\.co.uk\b/, '.de');

}

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
Participant ,
May 21, 2019 May 21, 2019

Copy link to clipboard

Copied

Hi Klass,

I just tested my original code along with Jong's edits and it does not work properly. The name is just the internal name and not the actual URL destination.

Use these lines of code instead (works on both copy and object hyperlinks), you will see we are now calling hyperlinkURLDestinations:

var doc = app.activeDocument;

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

    doc.hyperlinkURLDestinations.item(i).destinationURL = doc.hyperlinkURLDestinations.item(i).destinationURL.replace(/\.co.uk\b/, '.de');

}

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 ,
May 22, 2019 May 22, 2019

Copy link to clipboard

Copied

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

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 Beginner ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

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

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:

Screen Shot 2020-07-28 at 10.29.25 AM.png

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

Thank you,

Tony

 

 

 

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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

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

 

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 Beginner ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

Wow! amazing, it works and thank you for responding so quickly.

I have a screen shot of the hyperlinks pallete where you can see the new code in the actual link but the code name in the listing has the old code MR7, not critical but would I that need another line to change that? Thank you so much Brian!

 

Screen Shot 2020-07-28 at 12.13.00 PM.png

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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

I think you want to change the "name" property of the URLDestination object, too: 

try { 
doc.hyperlinkURLDestinations.item(i).destinationURL = doc.hyperlinkURLDestinations.item(i).destinationURL.replace("MR7", "MR9");
doc.hyperlinkURLDestinations.item(i).name = doc.hyperlinkURLDestinations.item(i).name.replace("MR7", "MR9");
} catch(e){}
};

 

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 Beginner ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

Yep, that worked as well! I'll run it separately for now until I can figure out how to combine them.

Thank you again Brian. I don't know how I can repay you. This is very valuable!

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 ,
Jul 28, 2020 Jul 28, 2020

Copy link to clipboard

Copied

You can run it all as one script. Just add replace that snippet above into the loop: 

 

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");
doc.hyperlinkURLDestinations.item(i).name = doc.hyperlinkURLDestinations.item(i).name.replace("MR7", "MR9");
} catch(e){}
};

 

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
New Here ,
Sep 23, 2020 Sep 23, 2020

Copy link to clipboard

Copied

Thank you! It is very helpful for me to change all hyperlinks at the same time.

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
New Here ,
May 18, 2022 May 18, 2022

Copy link to clipboard

Copied

LATEST

Thank you so much @brianp311 - that was exactly what I needed for a similar use-case.

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
New Here ,
Nov 13, 2020 Nov 13, 2020

Copy link to clipboard

Copied

Thank you so much for this! It saved me a lot of time and embarrassment!

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