3 Replies Latest reply on Jun 11, 2013 12:27 PM by -hans-

    Make hyperlink text actual hyperlinks

    xmlapi

      We have a couple of CSV files we use for datamerging with InDesign. Some of the text in the CSV has hyperlinks in the paragraph.

       

      Since InDesign does not automatically make hyperlinks in the CSV text actual hyperlinks and there is no special syntax to indicate it is a hyperlink (such as using @ for image paths).....we have decided to wrap any hyperlink text with the <link></link> tag.

       

      InDesign, we want to find all of these <link> tags and replace it with an actual hyperlink. The link maybe an http link, mailto link, or a tel link.

       

      How would I do this with InDesign scripting (prefably javascript or c#)?

       

      I have the following so far in C# (similiar to javascript):

      app.FindTextPreferences.FindWhat = "<link>";

       

       

      How do I replace it with a hyperlink?

        • 1. Re: Make hyperlink text actual hyperlinks
          -hans- Level 4

          Hi,

           

          first: you'll need a grepsearch (something like: (?<=<link>).+(?=</link>) which should only find the link itsself)

          second: to create a hyperlink you'll need  a source and a destination (dest will be the result of the search above ...). -> http://jongware.mit.edu/idcs5/pc_Hyperlinks.html#add

          • 2. Re: Make hyperlink text actual hyperlinks
            xmlapi Level 1

            I have the following so far:

            var doc = app.activeDocument;

            app.findGrepPreferences.findWhat = '(?<=<link>).+(?=</link>)';

            var objs = doc.findGrep();

             

            for(var i=0; i < objs.length; i++){

                var txtFrame = doc.textFrames[0];

                var para = txtFrame.paragraphs.item(0);

                var lnkDest = doc.hyperlinkURLDestinations.add(objs[i].contents);

             

                var lnkSrc = doc.hyperlinkTextSources.add(objs[i]);

                var lnk = doc.hyperlinks.add(lnkSrc, lnkDest);

                objs[i].contents = objs[i].contents.replace("<link>","");

                objs[i].contents = objs[i].contents.replace("</link>","");

            }

             

             

            This links the text but how do I remove the <link> text tag after linking it? Also, this has to run about 300 pages, is this the fastest way to do it?

            • 3. Re: Make hyperlink text actual hyperlinks
              -hans- Level 4

              Hi,

              as you want to use the found link itsself as textsource ... have you thought of calling the command 'convert Urls to hyperlinks in the GUI'¿

              The link-tags wouldn't be needed then ...

               

              A script could be like this:

               

              app.findGrepPreferences = app.changeGrepPreferences = null;
              var doc = app.activeDocument;
              app.findGrepPreferences.findWhat = '(?<=<link>)[^<link>]+(?=</link>)';
              var objs = doc.findGrep();
               l = objs.length;
              
               while(l--)
               {
                   var currTarget  = objs[l];
                  var lnkDest = doc.hyperlinkURLDestinations.add(currTarget.texts[0].contents);
                  var lnkSrc = doc.hyperlinkTextSources.add(currTarget);
                  var lnk = doc.hyperlinks.add(lnkSrc, lnkDest);
                  /*
                  var leftIndex = currTarget.index;
                  var rigthIndex = currTarget.index + objs[l].length;
                  var pStory = currTarget.parentStory;
                  pStory.characters.itemByRange(rigthIndex, rigthIndex+6).remove();
                    pStory.characters.itemByRange(leftIndex-6, leftIndex-1).remove();
                    */    
                 }
              
              app.findGrepPreferences = app.changeGrepPreferences = null;
              app.findGrepPreferences.findWhat = '<link>|</link>';
              app.changeGrepPreferences.changeTo = '';
              doc.changeGrep();
              

               

              The commented stuff would delete the link-Tags during the repeatloop instead of the actually used second search ...

              Didn't test what'll be faster ...