Copy link to clipboard
Copied
Hello.
I have a simple JavaScript finding and replacing text in hyperlinks. I was wondering if this can be re-written to just add additional text to each hyperlink in Indesign document.
for example "1234" will add 1234 to the end of each hyperlink. google.com will become google.com1234
thank you very much!
main();
function main(){
var d = app.dialogs.add({name:"Replace Hyperlink URL Values"});
var col1 = d.dialogColumns.add();
var col2 = d.dialogColumns.add();
col1.staticTexts.add({staticLabel:"Find (GREP):"});
col1.staticTexts.add({staticLabel:"Replace:"});
var find = col2.textEditboxes.add({minWidth:100});
var change = col2.textEditboxes.add({minWidth:100});
var result = d.show();
if(!result){
d.destroy();
return;
}
var grepForFind = RegExp(find.editContents,"g");
var grepForReplace = change.editContents;
d.destroy();
var dests = app.documents[0].hyperlinkURLDestinations.everyItem().getElements();
for(var i=0;i<dests.length;i++){
dests.destinationURL = dests.destinationURL.replace(grepForFind,grepForReplace);
}
}
Hi,
Try this ...
hlinks = app.documents[0].hyperlinks.everyItem().getElements();
for (i = hlinks.length-1; i >= 0; i--) {
if (hlinks.destination instanceof HyperlinkURLDestination){
hlinks.destination.destinationURL+= '1234';
}
}
Regards
Copy link to clipboard
Copied
Change this line:
var grepForReplace = change.editContents;
to
var grepForReplace = change.editContents+'1234';
Copy link to clipboard
Copied
hmm is there a way to get rid of search and replace function ? Just run the script it will find all hyperlinks in documents and add 1234 to all hyperlinks ?
thank you!
Copy link to clipboard
Copied
That wasn't entirely clear from your post. Anyway, this will do what you want:
hlinks = app.documents[0].hyperlinks.everyItem().getElements();
for (i = hlinks.length-1; i >= 0; i--) {
hlinks.name += '1234';
}
Peter
Copy link to clipboard
Copied
Hi Peter,
I am sorry . english is not my strong suit. Your script works but it renames hyperlink names in Indesign. I would like to update urls. for example Hyperlink1 url "google.com" change to "google.com1234". Hyperlink2 url "yahoo.com" change to "yahoo.com1234" and so on. Right now it is updating names Hyperlink1 becomes Hyperlink11234. Hyperlink2 become Hyperlink212134.
I tried changing hlinks.name to hlinks.url but that did not work. I am not that good in scripting ![]()
Copy link to clipboard
Copied
Hi,
Try this ...
hlinks = app.documents[0].hyperlinks.everyItem().getElements();
for (i = hlinks.length-1; i >= 0; i--) {
if (hlinks.destination instanceof HyperlinkURLDestination){
hlinks.destination.destinationURL+= '1234';
}
}
Regards
Copy link to clipboard
Copied
That worked! such a time saver for me! thank you very much!!!
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more