22 Replies Latest reply on Dec 22, 2016 1:20 AM by Peter Kahrel

    Converting multiple phone numbers to hyperlinks/URL in InDesign all at once

    stephanieb23791774

      I have a book that contains over 2,500 phone numbers that I update twice a year in InDesign and then export it into PDF. I just discovered a way for recipients of this PDF to save it to their iBooks app to then have the ability to access it from their mobile devices (iPhones, for the most part), where they can just click on a particular phone number and have their device automatically dial out to that number. Since there are SO many numbers provided in my book, is there an easy way to convert/program all of the numbers to essentially do the same function - call when clicked - all in one fell swoop, instead of converting each individual phone number into a hyperlink?  There has to be some code or script to accomplish this - can someone please help? Thanks!

        • 1. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
          Peter Kahrel Adobe Community Professional & MVP

          The hard part is probably identifying the phone numbers. What are the criteria? What distinguishes phone numbers from, say, ISBN numbers or any other numbers? Once you can find the numbers you need a script to create hyperlinks, but that's simple.

           

          Peter

          • 2. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
            vinny38 Level 4

            Hi,

            in addition to @pkahrel comment, you could use a GREP query to apply a character style to your tel numbers. (Or paragraph, depending on how your design is set). Then, as suggested by pkahrel, you'll need a script.

            I personally don't find that simple, especially to set up the hyperlink as "tel:" instead of "http://" but maybe a specialist can help you on this one.

             

            Here's the beginning of a script allowing you to find your numbers, assuming a character style is applied:

             

            var myDoc =app.activeDocument;  
              
            var myLW = 100;  
            var myStyleNames = myDoc.characterStyles.everyItem().name;  
            var myDialog = app.dialogs.add({name:"Create tel: hyperlink", canCancel:true});  
                 with(myDialog.dialogColumns.add()){       
                      with(borderPanels.add()){  
                           with(dialogColumns.add()){staticTexts.add({staticLabel:"Choose your tel number Character Style:   ", minWidth:myLW});}  
                                with(dialogColumns.add()){var myChooseStyle = dropdowns.add({stringList:myStyleNames, selectedIndex:0});}  
                                }  
                           }  
            var myResult = myDialog.show();  
            if(myResult == true){  
                 var mySelStyle = myStyleNames[myChooseStyle.selectedIndex];  
                 myDialog.destroy();  
            
            
            }  
            else{  
                 myDialog.destroy();  
            }  
            
            
            
            
            app.findTextPreferences = NothingEnum.nothing;
            app.changeTextPreferences = NothingEnum.nothing;
            app.findTextPreferences.appliedCharacterStyle = mySelStyle.toString();  
            
            
            var mySearch = app.findText(false);  
            alert ('you have '+mySearch.length + ' tel numbers to convert');
            
            
            for(i=0 ; i<mySearch.length; i++){  
                var number = mySearch[i].contents;
                // here's the tricky part for me, specialists please advice
                // myDoc.hyperlinkURLDestinations.add(number, {name:number});  
                 }  
            
            • 3. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
              Peter Kahrel Adobe Community Professional & MVP

              Vinny -- Finding the phone numbers is the challenge. After that it's not such a problem.

              • 4. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                vinny38 Level 4

                pkahrel -- I'm sure a GREP query can do it easily. Maybe @stephanieb23791774 can display examples of phone numbers format. (would be little bit more tricky if there are different international formats). Besides, it also depends on the design. They can be in a table column for instance... would be very easy then.

                Would you agree on thinking than first step would be applying paragraph or character style? I would be interested to see how you would code what i call "the tricky part". (in javascript, I don't know VBScript)

                I think it's an interesting question. I might use the answer for my projects as well. I'll keep an eye on this thread.

                regards

                • 5. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                  bradya48637260

                  Any follow-up on the "easy" part of this? I have no idea how to make a script in InDesign

                  • 6. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                    stephanieb23791774 Level 1

                    Hi there!  Sorry for the delay, but just so you know, unless you are outside of the U.S., there are no international formatted numbers - the numbers are all based within the U.S.  Right now, we format the numbers as such: (###) ###-####; however, we are open to formatting it differently if it will make writing a script for this easier.

                    I'm familiar with the GREP query, as well as applying a character style - at this point I'd just really like to try to figure out a way to quickly complete this, as well have a process that can be used for going forward: we update this book twice a year.

                    • 7. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                      stephanieb23791774 Level 1

                      Right now, all of our phone numbers in this book are based in the U.S., so currently we use the following formatting for the numbers: (###) ###-####; however, we are open to changing the format if it would make it easier to come up with a script for this.

                      • 8. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                        Michael Witherell Adobe Community Professional

                        Isn't there a built-in GREP search that hunts for all 10-digit phone numbers and sets them to the same typeset condition while you can optionally also applying a character style to it?

                        • 9. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                          Peter Kahrel Adobe Community Professional & MVP

                          Here's a script that creates links from phone numbers in the format you gave. The URL destinations have the form 'tel:' followed by the number stripped of anything non-digit (parentheses and spaces in this case). The script uses the character style 'Hyperlink', which, is created if it doesn't exist. The script closes the Hyperlinks panel to gain some speed. If you've many phone numbers, the script will start to slow down after creating about 800 links -- that's an Inesign thing, it just gets slow with many links.

                           

                          Peter

                           

                          (function () {
                            
                            var i;
                            var cStyle;
                            var found;
                            var source;
                            var destination;
                          
                            try {
                              app.panels.item('$ID/Hyperlinks').visible = false;
                            } catch (_) {
                            }
                          
                            if (!app.documents[0].characterStyles.item ('Hyperlink').isValid) {
                              app.documents[0].characterStyles.add ({name: 'Hyperlink', underline: true});
                            }
                            cStyle = app.documents[0].characterStyles.item ('Hyperlink');
                          
                            app.findGrepPreferences = null;
                            app.findGrepPreferences.findWhat = /\(\d\d\d\)\s\d\d\d\s\d\d\d\d/.source;
                            found = app.documents[0].findGrep();
                            for (var i = 0; i < found.length; i++) {
                              source = app.documents[0].hyperlinkTextSources.add (found[i], {appliedCharacterStyle: cStyle});
                              destination = app.documents[0].hyperlinkURLDestinations.add ({
                                destinationURL: 'tel:' + found[i].contents.replace(/\D/g,'')
                              });
                              app.documents[0].hyperlinks.add (source, destination);
                            }
                          
                          }());
                          
                          2 people found this helpful
                          • 10. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                            vinny38 Level 4

                            Really cool stuff pkharel!!!

                             

                            There just might be a small mistake in the GREP query \(\d\d\d\)\s\d\d\d\d\d\d\d

                            Space should be replaced by dash  \(\d\d\d\)-\d\d\d\d\d\d\d

                             

                            Additional question: why do you close the hyperlink panel? Is there a specific reason for that?

                            • 11. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                              Peter Kahrel Adobe Community Professional & MVP

                              Thanks for spotting that, Vinny. But you corrected it wrong . It should be

                              \(\d\d\d\)\s\d\d\d-\d\d\d\d

                               

                              > Additional question: why do you close the hyperlink panel? Is there a specific reason for that?

                               

                              It speeds up the script because InDesign doesn't have to update the panel.

                               

                              P.

                              • 12. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                                vinny38 Level 4

                                1/Ooops...

                                2/Ok I understand the speed issue!

                                Thanks!

                                • 13. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                                  stephanieb23791774 Level 1

                                  Thank you for providing this information, Peter!  I've entered the script into InDesign; however, it doesn't seem to be doing anything: I have my document open and when I double-click the script to run it, I can't tell if anything is being done.

                                   

                                  Also, it's been over 30 minutes now since I first ran the script - how long should it normally take to create links from phone numbers? 

                                  • 14. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                                    stephanieb23791774 Level 1

                                    One last thing, I took your script and replaced the corrected GREP query, but just wanted to confirm that I had captured it correctly - is this how it should appear:

                                     

                                    (function () {

                                     

                                      var i;

                                      var cStyle;

                                      var found;

                                      var source;

                                      var destination;

                                     

                                      try {

                                      
                                    app.panels.item('$ID/Hyperlinks').visible = false;

                                      } catch (_) {

                                      }

                                     

                                      if
                                    (!app.documents[0].characterStyles.item ('Hyperlink').isValid) {

                                      
                                    app.documents[0].characterStyles.add ({name: 'Hyperlink', underline: true});

                                      }

                                      cStyle =
                                    app.documents[0].characterStyles.item ('Hyperlink');

                                     

                                      app.findGrepPreferences = null;

                                      app.findGrepPreferences.findWhat = /
                                    \(\d\d\d\)
                                    \s\d\d\d-\d\d\d\d/.source;

                                      found = app.documents[0].findGrep();

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

                                        source =
                                    app.documents[0].hyperlinkTextSources.add (found[i], {appliedCharacterStyle:
                                    cStyle});

                                        destination =
                                    app.documents[0].hyperlinkURLDestinations.add ({

                                        
                                    destinationURL: 'tel:' + found[i].contents.replace(/\D/g,'')

                                        });

                                      
                                    app.documents[0].hyperlinks.add (source, destination);

                                      }

                                     

                                    }());

                                    • 15. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                                      Peter Kahrel Adobe Community Professional & MVP

                                      Stephanie -- You changed the GREP correctly, no problem there. You'd better try the script on a small sample: copy some text containing, say, five phone numbers, out of your document into a new document and try the script again.

                                       

                                      Peter

                                      • 16. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                                        Peter Kahrel Adobe Community Professional & MVP

                                        Here is a version of the script that adds a progress bar (which apparently doesn't always show on certain versions of Mac). In a clean new document with just 2,500 phone numbers the script runs almost a minute on my (almost new, well-specced) PC (in CC2015 and 2017). The progress bar shows that the script tears off furiously, then slows down after about 500 links, and crawls to the finish. But do try a test on a document with a few dozen numbers.

                                         

                                        Peter

                                         

                                        #targetengine phone_numbers;
                                        
                                        (function () {
                                          
                                          var i;
                                          var w;
                                          var cStyle;
                                          var found;
                                          var source;
                                          var destination;
                                          
                                          app.scriptPreferences.enableRedraw = true;
                                          
                                          try {
                                            app.panels.item('$ID/Hyperlinks').visible = false;
                                          } catch (_) {
                                          }
                                        
                                          if (!app.documents[0].characterStyles.item ('Hyperlink').isValid) {
                                            app.documents[0].characterStyles.add ({name: 'Hyperlink', underline: true});
                                          }
                                          cStyle = app.documents[0].characterStyles.item ('Hyperlink');
                                        
                                          app.findGrepPreferences = null;
                                          app.findGrepPreferences.findWhat = /\(\d\d\d\)\s\d\d\d-\d\d\d\d/.source;
                                          found = app.documents[0].findGrep();
                                        
                                          w = new Window ('palette {text: "Phone URLs"}');
                                            w.pbar = w.add ('progressbar', undefined, 0, found.length);
                                            w.pbar.preferredSize.width = 300;
                                          w.show();
                                        
                                          for (i = 0; i < found.length; i++) {
                                            w.pbar.value = i+1;
                                            source = app.documents[0].hyperlinkTextSources.add (found[i], {appliedCharacterStyle: cStyle});
                                            destination = app.documents[0].hyperlinkURLDestinations.add ({
                                              destinationURL: 'tel:' + found[i].contents.replace(/\D/g,'')
                                            });
                                            app.documents[0].hyperlinks.add (source, destination);
                                          }
                                          
                                          try {
                                            w.close();
                                          } catch (_) {
                                          }
                                        
                                        }());
                                        
                                        1 person found this helpful
                                        • 17. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                                          stephanieb23791774 Level 1

                                          Peter - this script worked! It did what I needed it to do!  I am SO ecstatic about this. 

                                           

                                          I'm hoping that I could bother you again for one more script: I've noticed that the script fails when there are already hyperlinks included in the document.  Do you have an easy script that will remove any hyperlinks already contained within the document? Sometimes I don't always notice that there are hyperlinks in place (no underline or character style) so rather than scrolling through the whole document to search for these hyperlinks, I thought it would be easier to just remove them all at once with a new script and then run my happy script above.

                                          • 18. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                                            Peter Kahrel Adobe Community Professional & MVP

                                            Between lines 12 and 14, add this single line:

                                             

                                            app.documents[0].hyperlinks.everyItem().remove();

                                             

                                            P.

                                            • 19. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                                              stephanieb23791774 Level 1

                                              Peter - I've added the one line above to the script provided six days ago and I keep getting this popup when I attempt to run the revised script.  Does it make sense to you?

                                              Script error message.jpg

                                              This is exactly how I have the script with the one line added:

                                              (function () { 

                                                 

                                                var i; 

                                                var w; 

                                                var cStyle; 

                                                var found; 

                                                var source; 

                                                var destination; 

                                                 

                                                app.scriptPreferences.enableRedraw = true

                                                 

                                                try

                                                  app.panels.item('$ID/Hyperlinks').visible = false

                                                } catch (_) { 

                                                } 

                                               

                                                if (!app.documents[0].characterStyles.item ('Hyperlink').isValid) { 

                                                  app.documents[0].characterStyles.add ({name: 'Hyperlink', underline: true}); 

                                                } 

                                                cStyle = app.documents[0].characterStyles.item ('Hyperlink'); 

                                               

                                                app.findGrepPreferences = null

                                                app.findGrepPreferences.findWhat = /\(\d\d\d\)\s\d\d\d-\d\d\d\d/.source; 

                                                found = app.documents[0].findGrep(); 

                                               

                                                w = new Window ('palette {text: "Phone URLs"}'); 

                                                  w.pbar = w.add ('progressbar', undefined, 0, found.length); 

                                                  w.pbar.preferredSize.width = 300

                                                w.show(); 

                                               

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

                                                  w.pbar.value = i+1

                                                  source = app.documents[0].hyperlinkTextSources.add (found[i], {appliedCharacterStyle: cStyle}); 

                                                  destination = app.documents[0].hyperlinkURLDestinations.add ({ 

                                                    destinationURL: 'tel:' + found[i].contents.replace(/\D/g,''

                                                  }); 

                                                  app.documents[0].hyperlinks.add (source, destination); 

                                                } 

                                                 

                                                try

                                                  w.close(); 

                                                } catch (_) { 

                                                } 

                                               

                                              }());

                                               

                                              Can this be fixed?

                                              ~Stephanie

                                              • 20. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                                                Peter Kahrel Adobe Community Professional & MVP

                                                I don't see the added line in the script you posted, Stephanie. And when I copy the text of your script, it works fine over here. What happens if you go back to the first version I posted? Does that still work? Don't forget the first line, #targetengine phone_numbers;

                                                 

                                                You could in principle run that additional line as a separate script, by the way, it's just more convenient if everything is in one script.

                                                 

                                                Peter

                                                • 21. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                                                  stephanieb23791774 Level 1

                                                  Darn - I forgot to send the script with the added line.  OK, so this is the revised script that I actually used that is giving me that JavaScript error popup above:

                                                   

                                                  (function () { 

                                                     

                                                    var i; 

                                                    var w; 

                                                    var cStyle; 

                                                    var found; 

                                                    var source; 

                                                    var destination; 

                                                     

                                                    app.scriptPreferences.enableRedraw = true;

                                                    app.documents[0].hyperlinks.everyItem().remove();

                                                    try { 

                                                      app.panels.item('$ID/Hyperlinks').visible = false; 

                                                    } catch (_) { 

                                                    } 

                                                   

                                                    if (!app.documents[0].characterStyles.item ('Hyperlink').isValid) { 

                                                      app.documents[0].characterStyles.add ({name: 'Hyperlink', underline: true}); 

                                                    } 

                                                    cStyle = app.documents[0].characterStyles.item ('Hyperlink'); 

                                                   

                                                    app.findGrepPreferences = null; 

                                                    app.findGrepPreferences.findWhat = /\(\d\d\d\)\s\d\d\d-\d\d\d\d/.source; 

                                                    found = app.documents[0].findGrep(); 

                                                   

                                                    w = new Window ('palette {text: "Phone URLs"}'); 

                                                      w.pbar = w.add ('progressbar', undefined, 0, found.length); 

                                                      w.pbar.preferredSize.width = 300; 

                                                    w.show(); 

                                                   

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

                                                      w.pbar.value = i+1; 

                                                      source = app.documents[0].hyperlinkTextSources.add (found[i], {appliedCharacterStyle: cStyle}); 

                                                      destination = app.documents[0].hyperlinkURLDestinations.add ({ 

                                                        destinationURL: 'tel:' + found[i].contents.replace(/\D/g,'') 

                                                      }); 

                                                      app.documents[0].hyperlinks.add (source, destination); 

                                                    } 

                                                     

                                                    try { 

                                                      w.close(); 

                                                    } catch (_) { 

                                                    } 

                                                   

                                                  }());

                                                   

                                                   

                                                  So then I went ahead and added the first line, #targetengine phone_numbers;, and then got this error message:

                                                  Script error message 2.jpg

                                                  Does this make any sense to you?  Ideally I would LOVE to have everything run in just one script - would definitely be more convenient!

                                                   

                                                  Thanks so much!

                                                  ~Stephanie

                                                  • 22. Re: Converting multiple phone numbers to hyperlinks/URL in InDesign all at once
                                                    Peter Kahrel Adobe Community Professional & MVP

                                                    Looks like you inserted

                                                     

                                                    first line, #targetengine phone_numbers;

                                                     

                                                    that should be

                                                     

                                                    #targetengine phone_numbers;

                                                     

                                                    P.