5 Replies Latest reply on Jul 23, 2009 5:58 PM by jay fresno

    a script to make all page references into hyperlinks

    Ola K
      Hi,

      I have a customer request to make all pages references into hyperlinks.
      For example: when you see in the text of the PDF "page 4", you can click on it and move to page 4. This of course can be done manually with InDesign. The problem starts when you have whole load of them. In addition to that, I have this project as a book, with several different documents...

      I used the script "Automatic hyperlink of each word in a file" from http://www.nobrainer.dk/automatic-hyperlink-of-each-word-in-a-file/ as a base, and modified it for my porpuse. Currently it still produces errors. Any idea what's the bug?

      if (app.documents.length > 0) {
      var myDocument = app.activeDocument;
      var myHyperlinkStyle = myDocument.characterStyles.item("hyperlink"); // replace hyperlink with name of your char style for links
      //alert(myHyperlinkStyle);
      main();
      } else {
      alert("Please open a document");
      }

      function main() {
      app.findGrepPreferences = NothingEnum.nothing;
      app.changeGrepPreferences = NothingEnum.nothing;

      //the value to find
      myFindVal = "page (\\d+)";
      doSearchAndReplace(myFindVal, app.activeDocument);
      // reset search
      app.findGrepPreferences = NothingEnum.nothing;
      app.changeGrepPreferences = NothingEnum.nothing;
      }

      function doSearchAndReplace(stringfind, searchin) {
      app.findGrepPreferences.findWhat = stringfind;

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

      var myFoundItems = searchin.findGrep();

      for (i = 0; i < myFoundItems.length; i++) {
      var item = myFoundItems[i].contents;
      //the destination page
      var destPage = item.slice(5);
      var myHyperlinkDestination = myMakeHyperlinkDestination(destPage);
      myMakeHyperlink(myFoundItems[i], myHyperlinkDestination);
      myFoundItems[i].applyCharacterStyle(myHyperlinkStyle, false);
      }
      }

      function myMakeHyperlink(myFoundItem, myHyperlinkDestination){
      try {
      var myHyperlinkTextSource = myDocument.hyperlinkTextSources.add(myFoundItem);
      var myHyperlink = myDocument.hyperlinks.add(myHyperlinkTextSource, myHyperlinkDestination);
      myHyperlink.visible = false;
      }
      catch(myError){
      }
      }

      function myMakeHyperlinkDestination(myDestPage){
      //If the hyperlink destination already exists, use it;
      //if it doesn't, then create it.
      try{
      var myHyperlinkDestination = myDocument.hyperlinkDestinations.item(myDestPage);
      myHyperlinkDestination.name;
      }
      catch(myError){
      myHyperlinkDestination = myDocument.hyperlinkPageDestinations.add(myDestPage);
      myHyperlinkDestination.destinationPage = myDestPage;
      }
      myHyperlinkDestination.name = myDestPage;

      //Set other hyperlink properties here, if necessary.
      return myHyperlinkDestination;
      }
        • 1. Re: a script to make all page references into hyperlinks
          Ola K Level 1
          well, after some more digging into it, I found the bugs and corrected, here is the corrected version (now I only need to figure out how to do it on an entire book...):

          if (app.documents.length > 0) {
          var myDocument = app.activeDocument;
          var myHyperlinkStyle = myDocument.characterStyles.item("hyperlink"); // replace hyperlink with name of your char style for links
          //alert(myHyperlinkStyle);
          main();
          } else {
          alert("Please open a document");
          }

          function main() {
          app.findGrepPreferences = NothingEnum.nothing;
          app.changeGrepPreferences = NothingEnum.nothing;

          //the value to find
          myFindVal = "page (\\d+)";
          doSearchAndReplace(myFindVal, app.activeDocument);
          // reset search
          app.findGrepPreferences = NothingEnum.nothing;
          app.changeGrepPreferences = NothingEnum.nothing;
          }

          function doSearchAndReplace(stringfind, searchin) {
          app.findGrepPreferences.findWhat = stringfind;

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

          var myFoundItems = searchin.findGrep();

          for (i = 0; i < myFoundItems.length; i++) {
          var item = myFoundItems[i].contents;
          //the destination page
          var destPageNumber = item.slice(5);

          var destPage = searchin.pages.itemByName(destPageNumber);
          var myHyperlinkDestination = myMakeHyperlinkDestination(destPage);
          myMakeHyperlink(myFoundItems[i], myHyperlinkDestination);
          myFoundItems[i].applyCharacterStyle(myHyperlinkStyle, false);
          }
          }

          function myMakeHyperlink(myFoundItem, myHyperlinkDestination){
          try {
          var myHyperlinkTextSource = myDocument.hyperlinkTextSources.add(myFoundItem);
          var myHyperlink = myDocument.hyperlinks.add(myHyperlinkTextSource, myHyperlinkDestination);
          myHyperlink.visible = false;
          }
          catch(myError){
          }
          }

          function myMakeHyperlinkDestination(myDestPage){
          //If the hyperlink destination already exists, use it;
          //if it doesn't, then create it.
          try{
          var myHyperlinkDestination = myDocument.hyperlinkDestinations.item(myDestPage);
          myHyperlinkDestination.name;
          }
          catch(myError){
          myHyperlinkDestination = myDocument.hyperlinkPageDestinations.add(myDestPage);
          myHyperlinkDestination.destinationPage = myDestPage;
          }
          //myHyperlinkDestination.name = myDestPage.name;

          //Set other hyperlink properties here, if necessary.
          return myHyperlinkDestination;
          }
          • 2. Re: a script to make all page references into hyperlinks
            jay fresno Level 1

            Ola,

             

            Thank you for sharing your script! The script could be very useful for me (and I'm sure to others as well). However, I'm getting the following error when I run the script. Can you help with a suggestion?

             

            scripterror.jpg

             

            • 3. Re: a script to make all page references into hyperlinks
              Loic.Aigon Adobe Community Professional

              Hi,

               

              Do you have any character style named"hyperlink" ?

              Because it seems to me that Olav's script try to apply one named like this

              var myHyperlinkStyle = myDocument.characterStyles.item("hyperlink");

               

              Bye

              • 4. Re: a script to make all page references into hyperlinks
                [Jongware] Most Valuable Participant

                Jay,

                 

                At first glance, you might want to check the name of your hyperlink character style. The script sez it's called "hyperlink" and suggests you to change it to the one you are using

                 

                I find myself wondering about the 'true' (or was it 'false'? Read it ten minutes ago. Waited for the editor. Forgot.) after "applyCharacterStyle (name, false (or true))". According to the documentation, applyCharacterStyle doesn't take 2 arguments, it just needs the style name. applyParagraphStyle, on the other hand, needs two. Perhaps it accidentally worked for CS3 and it was "fixed" for CS4?

                • 5. Re: a script to make all page references into hyperlinks
                  jay fresno Level 1

                  Yes, Loic and Jongware. Thanks to you both! That was it -- after adding a character style named "hyperlink" script works fine.