Highlighted

Find and Replace in Hyperlink URL?

New Here ,
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

Adobe Community Professional
Correct answer by brianp311 | Adobe Community Professional

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

 

TOPICS
Scripting

Views

940

Likes

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

Find and Replace in Hyperlink URL?

New Here ,
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

Adobe Community Professional
Correct answer by brianp311 | Adobe Community Professional

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

 

TOPICS
Scripting

Views

941

Likes

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

Likes

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
Reply
Loading...
May 24, 2018 1
Explorer ,
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!

Likes

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
Reply
Loading...
May 21, 2019 0
Most Valuable Participant ,
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');
}

Likes

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
Reply
Loading...
May 21, 2019 1
Explorer ,
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?

Likes

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
Reply
Loading...
May 21, 2019 0
Adobe Community Professional ,
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

Likes

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
Reply
Loading...
May 21, 2019 2
Explorer ,
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');

}

Likes

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
Reply
Loading...
May 21, 2019 0
Participant ,
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');

}

Likes

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
Reply
Loading...
May 21, 2019 3
Explorer ,
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

Likes

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
Reply
Loading...
May 22, 2019 1
Community Beginner ,
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

 

 

Likes

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
Reply
Loading...
Jul 28, 2020 0
Adobe Community Professional ,
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){}
};

 

Likes

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
Reply
Loading...
Jul 28, 2020 2
Community Beginner ,
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

Likes

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
Reply
Loading...
Jul 28, 2020 0
Adobe Community Professional ,
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){}
};

 

Likes

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
Reply
Loading...
Jul 28, 2020 1
Community Beginner ,
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!

Likes

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
Reply
Loading...
Jul 28, 2020 0
Adobe Community Professional ,
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){}
};

 

Likes

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
Reply
Loading...
Jul 28, 2020 0
New Here ,
Sep 23, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Sep 23, 2020 0
New Here ,
Nov 13, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Nov 13, 2020 0