3 Replies Latest reply on Aug 8, 2018 4:45 PM by davidn77786114

    Script for applying hyperlinks to text

    tudor-v Level 2

      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 2

            Thank you! Your script worked very well.

            • 3. Re: Script for applying hyperlinks to text
              davidn77786114

              Thank you for providing Ronald. This was super useful. However, being unfamiliar with coding I'm having trouble tweaking the code so it will operate for my purposes. Instead of hyperlinking LinkedIn Profile: I need to hyperlink #1111 but the digits can be variables and there may also be five digits at times.

               

              Sample below:

              help.PNG

              How would I go about doing this?

               

              After the # is where I'm stuck:

               

               

              var doc = app.activeDocument; 

              // get URL 

              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; 

              app.findGrepPreferences.findWhat = '(?i)(?<=# )(https?|www)\\S+\\>='

              var mURL = doc.findGrep(); 

              // get Texte 

              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing; 

              app.findGrepPreferences.findWhat = '#'

              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)(?<=# )(https?|www)\\S+\\>='

              app.changeGrepPreferences.changeTo = ''

              doc.changeGrep();