5 Replies Latest reply on Mar 9, 2013 4:52 PM by [Jongware]

    Script to make hyperlinks live


      Below is a script I put together (with many clues from these forums and Adobe's documentation).

      I would appreciate any feedback people would like to provide.


      The aim is to take an InDesign document that was intended only for print, and go through to make all hyperlinks live. One of the problems is that typesetters can insert spaces or soft returns (shift-enter) in the middle of URLs to get them to break in the right spot.


      My main concerns, besides having the script do what it is supposed to do, is that it be stable and simple for anyone to use.


      // --- Assumptions --- //
      // One document open
      // Hyperlinks not necessarily labeled by character style
      // You want all hyperlinks to be live
      // Typesetter sometimes inserts a space or return in middle of URL to force a line break
      // Space or return comes before period or slash per style manual


      var doc=app.activeDocument;


      // Compress stray www .'s
      fn_grepChange (doc, "www(\\s|\\n)\\.", "www.");
      // Normalize to http://
      fn_grepChange (doc, "(http\\:\\/\\/)?(www\\.([\\w\\-\\?\\=\\&]+[\\.\\/]?)+)", "http://$2");
      // Remove ( space or return ) before ( period or slash )
      fn_grepChange (doc, "(http\\:\\/\\/([\\w\\-\\?\\=\\&]+[\\.\\/]?)+)(\\s|\\n)(\\.|\\/)", "$1$4");


      // ---- Main ---- //
      app.changeGrepPreferences = NothingEnum.nothing;
      app.findGrepPreferences = NothingEnum.nothing;
      app.findChangeGrepOptions.includeFootnotes = true;


      // Regardless of character styles, find hyperlinks
      app.findGrepPreferences.findWhat = 'http\\:\\/\\/([\\w\\-\\?\\=\\&]+[\\.\\/]?)+';


      var found = app.findGrep();
      var report = "found and activated "+String(found.length)+" links\n";


      // make hyperlinks


      for(i=0; i<found.length; i++)
      var report = report+found[i].contents+"\n";


      var source = doc.hyperlinkTextSources.add(found[i]);    
      var dest=doc.hyperlinkURLDestinations.add(found[i].contents);
      doc.hyperlinks.add(source,dest, {visible:false, name:found[i].contents+"("+(i+1)+")"});


      // If exporting to PDF set include hyperlinks to true
      PDFExportPreset.includeHyperlinks = true;
      // To export to html, choose File-->Export for Dreamweaver


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


      alert ( report );


      function fn_grepChange (myDocument, regexFind, regexChange) {
          // purpose: execute a grep change, otherwise known as a regex s///;
          app.findGrepPreferences = NothingEnum.nothing;
          app.changeGrepPreferences = NothingEnum.nothing;


          //Set the find options.
          app.findChangeGrepOptions.includeFootnotes = true;
          app.findChangeGrepOptions.includeHiddenLayers = false;
          app.findChangeGrepOptions.includeLockedLayersForFind = false;
          app.findChangeGrepOptions.includeLockedStoriesForFind = false;
          app.findChangeGrepOptions.includeMasterPages = false;


          //Search and change
          app.findGrepPreferences.findWhat = regexFind;
          app.changeGrepPreferences.changeTo = regexChange;


          //Clear the find/change preferences.
          app.findGrepPreferences = NothingEnum.nothing;
          app.changeGrepPreferences = NothingEnum.nothing;

      // --- end of script --- //

        • 1. Re: Script to make hyperlinks live
          [Jongware] Most Valuable Participant

          Congrats! It looks remarkably like the one I'm using (*), except I don't have to account for


          // Typesetter sometimes inserts a space or return in middle of URL to force a line break
          // Space or return comes before period or slash per style manual


          .. because I don't do that, ever! If necessary, I insert one or more Discretionary Line Breaks


          (*) Which I shouldn't brag about (**), 'cause it's one of the main staples of my workflow. Sorry -- definitely not for publication...


          (**) It also adds mailto: links to all e-mail addresses. I think you are able to work that out as well.

          • 2. Re: Script to make hyperlinks live
            ckcosner Level 1

            Thanks for that fabulous indesign javascript resource guide you maintain online, by the way. Like seeing the forest from above.

            • 3. Re: Script to make hyperlinks live
              Colin Flashman Adobe Community Professional

              Love it. At the time the script was written, Twitter wasn't much of a thing... but now it'd be safe to say that twitter hashtags and twitter @s would be something that could be included in this script.

              • 4. Re: Script to make hyperlinks live
                Ellis home Level 4

                What the difference between this script and the Type/Convert URLs to Hyperlinks that Indesign CS6 already has?

                • 5. Re: Script to make hyperlinks live
                  [Jongware] Most Valuable Participant

                  The URL matching in CS6 (I think it actually was new in CS5) is a bit different from this one. It tries to match several things more -- ftb, http, https protocols, mail addresses (without "mailto:" prefix), and even DOS file names. See http://forums.adobe.com/message/4619648 for a longer discussion.


                  The main difference is that GREP is built-in and cannot be modified. The advantage of an external script is that you can tweak and twiddle it to your personal circumstances. See in particular the first few lines in ckcosner's script; these remove stray spaces and returns in a web address. That's not something you can do with the standard function.