2 Replies Latest reply on Jan 13, 2016 8:50 AM by tudor-v

    Script for applying hyperlinks to text

    tudor-v Level 1

      Hello,

       

      I made a people directory document in InDesign using data merge. Each person has information about his/her social network URLs (LinkedIn, Twitter).

       

      Is it possible to make a script which searches the InDesign document for all strings looking like this:

      LinkedIn Profile: http://a-very-long-url

      and transforms them into just LinkedIn Profile but with the URL applied as a hyperlink?

       

      If it's easier for the find/replace operation I could mark the string with delimiters before running the script, to avoid complex URL detection scripting. Maybe something like this (Markdown style): [LinkedIn Profile](http://a-very-long-url/)

       

      Thank you!

        • 1. Re: Script for applying hyperlinks to text
          Ronald63 Level 4

          Hi,

           

          Quick snippet, try  ...

          var doc = app.activeDocument;
          // get URL
          app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;   
          app.findGrepPreferences.findWhat = '(?i)(?<=LinkedIn Profile: )(https?|www)\\S+\\>'; 
          var mURL  = doc.findGrep();
          // get Texte
          app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;   
          app.findGrepPreferences.findWhat = 'LinkedIn Profile:'; 
          var mSource  = doc.findGrep();
          
          for (var k = 0; k <mSource.length; k++){
              var mHyperlinkDestination = doc.hyperlinkURLDestinations.add(mURL[k].contents);
              var mHyperlinkTextSource = doc.hyperlinkTextSources.add(mSource[k]);
              mHyperlink = doc.hyperlinks.add(mHyperlinkTextSource,mHyperlinkDestination);
              mHyperlink.name =mURL[k].contents;
              mHyperlink.visible=false;
          }
          //remove URL text
          app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;   
          app.findGrepPreferences.findWhat = '(?i)(?<=LinkedIn Profile: )(https?|www)\\S+\\>'; 
          app.changeGrepPreferences.changeTo = '';  
          doc.changeGrep(); 
          //rename 'LinkedIn Profile: ' to 'LinkedIn Profile'
          app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;   
          app.findGrepPreferences.findWhat = 'LinkedIn Profile: '; 
          app.changeGrepPreferences.changeTo = 'LinkedIn Profile';  
          doc.changeGrep(); 
          
          • 2. Re: Script for applying hyperlinks to text
            tudor-v Level 1

            Thank you! Your script worked very well.