Skip to main content
Participating Frequently
November 10, 2016
Answered

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

  • November 10, 2016
  • 5 replies
  • 7029 views

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!

    This topic has been closed for replies.
    Correct answer Laubender

    Spotted two errors that clearly were done by transferring this thread to the new forum.

    Hope I found all. Try the code below; I did not test it:

     

     

     

    #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 (_) {
    	}
    
    }());

     

     

     

    Regards,
    Uwe Laubender

    ( ACP )

    5 replies

    crealibro
    New Participant
    December 18, 2020

    Hello, I've just tried to use the script and it seemed to work, even while I've changed the GREP search. But it doesn't work at all: it creates some phone URL, but that it stops with an error message (code error 79111) that says that the chosen object is yet in use for another hyperlink, or something like that (I'm italian and I'm using the italian version of Indesign, so I don't know how this code error is originally in english).

     

    Could anyone help me? I'm not very experienced with scripts and similar things...

    Known Participant
    January 23, 2024

    Crealibro, if you face this issue again, you may want to make a duplicate copy of your document and remove all existing hyperlinks (you could even just duplicate a portion of the original document), then run it again. What may be happening is that an existing hyperlink is overlapped with the phone number (meaning, a portion of the phone number is part of that hyperlink, perhaps by accident). It could be that it happened due to a search and replace where you might not have known the hyperlink was formed that way. Also, if your hyperlinks all have a character style or paragraph style, you can search for a string like "(\d\d\d)" that has that style applied, and then see which of those is also part of an existing hyperlink. Drop that one into another doc, run the script on it, then paste it back into your original document. Just ideas...good luck!

    Community Expert
    March 4, 2020

    Hi jims85923660,

    what exact version of the code did you run?

     

    Could you post the code in a reply here by using the forum editor's "Insert/Edit code sample" function with Language: JavaScript ? I fear that all the code presented in this thread from 2016 was damaged last year by moving this thread to the new Adobe Forum. This happened to a lot of ExtendScript (JavaScript) code samples all over the place. Not only in the InDesign forum.

     

    Regards,
    Uwe Laubender

    ( ACP )

    jims85923660
    New Participant
    March 4, 2020

    I used the first one at the top, the one chosen as the correct answer. HEre it is:

    #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, {appliedCharacterStyle: cStyle});
    
        destination = app.documents[0].hyperlinkURLDestinations.add ({
    
          destinationURL: 'tel:' + found.contents.replace(/\D/g,'')
    
        });
    
        app.documents[0].hyperlinks.add (source, destination);
    
      }
    
      
    
      try {
    
        w.close();
    
      } catch (_) {
    
      }
    
    }());

     

    LaubenderCommunity ExpertCorrect answer
    Community Expert
    March 4, 2020

    Spotted two errors that clearly were done by transferring this thread to the new forum.

    Hope I found all. Try the code below; I did not test it:

     

     

     

    #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 (_) {
    	}
    
    }());

     

     

     

    Regards,
    Uwe Laubender

    ( ACP )

    jims85923660
    New Participant
    March 4, 2020

    Hello! I'm getting an error when I try this script. I get a message that says this:

    JavaScript Error!

    Error Number: 30477

    Error String: Invalid value for parameter 'source' of method 'add'.

    Expected Text, but received (Paragraph).

    Engine: phone_numbers

    File: C:\Program Files\Adobe\Adobe InDesign 2020\Scripts Panel\numbers to hyperlink.js

    Line: 59

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

     

    If I can get this to work, it would save me so much time! Any help would be greatly appreciated.

    vinny38
    Legend
    November 10, 2016

    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.contents;

        // here's the tricky part for me, specialists please advice

        // myDoc.hyperlinkURLDestinations.add(number, {name:number}); 

         } 

    Community Expert
    November 10, 2016

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

    vinny38
    Legend
    November 10, 2016

    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

    Community Expert
    November 10, 2016

    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

    Participating Frequently
    December 12, 2016

    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.

    Mike Witherell
    Community Expert
    Community Expert
    December 13, 2016

    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?

    Mike Witherell