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
2 Correct answers
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.
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){}
};
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.
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!
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');
}
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?
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
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');
}
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');
}
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
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:
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
 
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){}
};
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!
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){}
};
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!
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){}
};
Copy link to clipboard
Copied
Thank you! It is very helpful for me to change all hyperlinks at the same time.
Copy link to clipboard
Copied
Thank you so much @brian_p_dts - that was exactly what I needed for a similar use-case.
Copy link to clipboard
Copied
Thank you so much for this! It saved me a lot of time and embarrassment!

