6 Replies Latest reply on Dec 10, 2018 3:32 PM by wallacea3343183

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

              • 4. Re: Script for applying hyperlinks to text
                wallacea3343183

                Greetings,

                 

                That script worked well.

                How can I tweak it to do the same for sets of digits

                 

                Sample below:

                #10476 https://thiswebsite...

                #10536 https://thiswebsite...

                #12784 https://thiswebsite...

                 

                Here is what I tried:

                var doc = app.activeDocument; 

                // get URL 

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

                app.findGrepPreferences.findWhat = '(?i)(?<=\\d{5})(https?|www)\\S+\\>';  

                var mURL  = doc.findGrep(); 

                // get Texte 

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

                app.findGrepPreferences.findWhat = '\\d{5}';  

                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)(?<=\\d{5} )(https?|www)\\S+\\>';  

                app.changeGrepPreferences.changeTo = '';   

                doc.changeGrep();  

                //rename '#\d\d\d\d\d ' to '#\d\d\d\d\d ' 

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

                app.findGrepPreferences.findWhat = '\\d{5} ';  

                app.changeGrepPreferences.changeTo = '\\d{5}';   

                doc.changeGrep();

                ------------------------------------

                 

                What am I doing wrong?

                It keeps saying the error is in LINE 12

                ...Please and thanks

                • 5. Re: Script for applying hyperlinks to text
                  fragmichnicht Level 1

                  The reason might be a missing Space in the first GREP query (line 4)

                   

                  original:

                  app.findGrepPreferences.findWhat = '(?i)(?<=\\d{5})(https?|www)\\S+\\>';


                  how it should be:

                  app.findGrepPreferences.findWhat = '(?i)(?<=\\d{5} )(https?|www)\\S+\\>'; 

                   

                  Greetings

                  • 6. Re: Script for applying hyperlinks to text
                    wallacea3343183 Level 1

                    Thanks for your help,

                     

                    It was not the space.

                    Note: The first script, at the top of the page, with the LINKEDIN: verbiage, worked perfect.

                    I would need to replace LINKEDIN with the numbers like: '#10429'

                     

                    Again, thanks for you reply and help.