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

Add same text to each hyperlink in Indesign

Guest
Jul 31, 2015 Jul 31, 2015

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

  }

}

TOPICS
Scripting
692
Translate
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 1 Correct answer

Enthusiast , Aug 04, 2015 Aug 04, 2015

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

Translate
Community Expert ,
Jul 31, 2015 Jul 31, 2015

Change this line:

  var grepForReplace = change.editContents;

to

  var grepForReplace = change.editContents+'1234';

Translate
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
Guest
Aug 03, 2015 Aug 03, 2015

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!

Translate
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 ,
Aug 04, 2015 Aug 04, 2015

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

Translate
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
Guest
Aug 04, 2015 Aug 04, 2015

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

Translate
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
Enthusiast ,
Aug 04, 2015 Aug 04, 2015

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

Translate
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
Guest
Aug 04, 2015 Aug 04, 2015
LATEST

That worked! such a time saver for me! thank you very much!!!

Translate
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