is now part of CreativePro.com!

Free Script to Automate Adding Visible URLs

9

I do a lot of writing for InDesign Magazine, Adobe, and other print and PDF publications. I do most of my writing right in InDesign. The editorial style of print and PDF publications usually dictates that all hyperlinks be displayed as visible URLs, often using a URL shortener like Bitly.

I got really tired of visiting a web site, generating a shortened URL, copying the URL, pasting it into InDesign, removing the https:// from the front of the URL, selecting the remaining URL, creating a URL hyperlink with the correct settings, and moving the cursor to the end so I could continue typing.

After repeating this process several hundred times over the last few months, a thought slowly wormed its way into my thick skull: “hey, I should write a script to do this!” You’d think this would have occurred to me earlier. After all, I’ve written a title called InDesign Scripting Made Easy for Lynda.com, and have written countless custom scripts for clients.

I finally got around to hammering out the script, and you can download it here. To learn how to install a script, watch this video. Then, display your scripts panel on the screen (Window > Utilities > Scripts). Then, to use the script, visit a website, and copy the URL to your clipboard. Then, choose an insertion point and double-click on the script in the Scripts panel. The script will:

  1. Paste the URL at the insertion point
  2. Remove the prefix (http, https, etc) before and including the // at the start of the URL
  3. Check to see if there is a swatch called “Hyperlink” in the document. If not, it creates one.
  4. Check to see if there is a character style called “Hyperlink” in the document. If not, it creates one.
  5. Create a Hyperlink out of the URL
  6. Position the cursor at the end of the Hyperlink text
  7. Set the character style to “none” so you can continue to type after the hyperlink text.

I hope that you find this useful. I sure saves me time and aggravation each day!

Keith Gilbert is a design consultant, developer, educator, speaker, and author. His work has taken him throughout North America, Africa, Europe, and Asia. During his 35+ year career his clients have included Adobe, Apple, Target, Oracle, and the United Nations. He is the author of several popular titles for LinkedIn Learning, Adobe Press, and CreativePro. Find him at gilbertconsulting.com and on Twitter @gilbertconsult
  • Uwe Laubender says:

    Hi Keith,
    thank you for sharing this.

    I tested the script and there was an error on line 78.
    You are using an English string for the “[None]” character style there.
    I’m using a German version, that couldn’t understand that string.

    I think, it’s better to address that character style by its index number.
    That should run in all versions of InDesign all over the world:

    myLastIP.appliedCharacterStyle = myDoc.characterStyles[0];

    Uwe

    • Thanks for the helpful suggestion, Uwe. I apologize for forgetting about all users of non-English versions of InDesign! I’ve made the change you suggested to the script, and updated the script on the Web site.

  • Adam Pócs says:

    Hi Keith,

    thanks for this.

    Nonetheless I’d like to share a more automated (but less feature-rich) solution which is based on GREP and Character Styles (and a small “hack” if I can call it this).

    The proof-of-concept can be downloaded from this link:
    https://drive.google.com/file/d/0B_pLmZ05_3rcQW9pQWd2ZE5pc1U/view?usp=sharing

    It works the following way:
    – GREP checks if the text contains any substrings containing “http”. If yes, applies URL Character Style (blue).
    – At the same time it “removes” the http-part from the beginning (see URL Character Style how it goes).
    – One has to make sure having hyperlinks included upon PDF export, since this procedure does not create hyperlinks (but InDesign can of course create them when exporting to PDF).

    • Hi Adam. Thanks for sharing this. Very clever use of GREP styles. It works great. One small thing…

      Keep in mind that checking the Include: Hyperlinks option when you export a print PDF from InDesign doesn’t actually convert hyperlink text to live hyperlinks in the PDF. This option only includes hyperlinks that are created as hyperlinks in InDesign. It may appear to be converting hyperlink text to live hyperlinks, because Acrobat itself recognizes text that “looks” like hyperlinks and creates a hyperlink on-the-fly, but ONLY if you have the “Create Links from URLs” option selected in Acrobat’s Preferences > General.

      So, to guarantee that hyperlinks will always work for everyone in all Acrobat readers, you should create actual hyperlinks in InDesign’s Hyperlinks panel, or by choosing “Type > Hyperlinks & Cross-References > Convert URLs to Hyperlinks” in InDesign.

      • Adam Pócs says:

        Oh, yeah, thanks, for mentioning, you are right. :)

        I am still missing a GREP-based replacement feature from the app (without the used hack). So to be clear: I need something like the GREP styles, but instead of applying a style, it’d replace a string with another one dynamically.

  • Masood Ahmad says:

    Hi Keith,

    I have watched your tutorial “InDesign Scripting Made Easy”on Lynda.com. It was me who requested Lynda for an Article on Indesign scripting, couple of months ago. I appreciate your effort in creating such a nice Tutorial. I can say it is a good start for beginners like me. However, I feel like that it contains the basics only. Just taking the liberty to ask if you can put more Tutorials on Lynda.com, moving from Beginner to Advance and Master levels.

    For example:
    Exporting a PDF/IDML/Package/JPG etc with options on particular locations.
    Renaming the folder/file after performing some task.
    Getting user input and performing actions.
    Getting the location of the file, creating folders and sub-folders at that location etc etc.

    Hope you can help us to learn scripting…

  • Wallace says:

    Hi Keith,

    I found this awesome script that turns this:
    Linkedin Profile: https://secure.yourirvine.org/CA/city-of-irvine/catalog?filter=c2VhcmNoPTE0MjA5

    into this:
    Linkedin Profile (but as a hyperlink from the link that follows it.

    The script below is what I found…

    ——————————–
    var doc = app.activeDocument;
    // get URL
    app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
    app.findGrepPreferences.findWhat = ‘(?i)(?’;
    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)(?’;
    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();
    ——————————–

    What I need is for it to do the same thing to this:
    #14209 https://secure.yourirvine.org/CA/city-of-irvine/catalog?filter=c2VhcmNoPTE0MjA5

    to this:
    #14209 (but with the hyperlink applied as it did to the first one.

    Please help, I am super close to being the hero and completing this script for my team.
    What can I do?

  • Eugenio Perez says:

    Thanks so much for this script Keith. I’ve been using it for about a year. It recently stopped working though, guessing because of an INDD update. I’m using INDD 18.2.1 on Mac OS 13.2.1 and receive the following error when I try to use it:

    JavaScript Error!
    Error Number: 55
    Error String: Object does not support the property or method
    ‘sourceText’
    Engine: main
    File: /Users / eugenio/Library/Preferences /Adobe InDesign /Version
    18.0/en_US/Scripts/Scripts Panel/Add hyperlink to selected text from clipboard.jsx
    Line: 31
    Source:
    app.doScript(Main, undefined, undefined,
    UndoModes.ENTIRE SCRIPT. “Run Script”;

    If you have time, could you please look into it. Much appreciated.

    • Mike Rankin says:

      Hi Eugenio- I just tried the script in InDesign 2023 and it works as expected. First, be sure you have copied a full URL to your clipboard, then put your cursor in a text frame and run the script.

  • >