6 Replies Latest reply on Aug 4, 2015 7:04 AM by DawidRuzica

    Add same text to each hyperlink in Indesign

    DawidRuzica

      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[i].destinationURL = dests[i].destinationURL.replace(grepForFind,grepForReplace);

        }

      }