3 Replies Latest reply on Aug 6, 2014 7:03 AM by Colin Flashman

    Scripting hyperlinks

    jeslaine

      I'm looking for a very simple java or applescript for InDesign CS5 that will change multiple items into anchors. I found some in other places but I don't know enough about scripting to be able to change them to be what I need.

       

      I'd like to use scripting to make everything that's formatted in paragraph style "Recipe Title-H2" into a text anchor. (I can change the name if spaces in the title are an issue). Or a bookmark with text anchor would work too. From there I can link all the cross references that mention the recipe back to the anchor. Can someone please help me out with the script? I need the text anchors to be named something, preferrably the original text they are highlighting.

       

      Background: I'm trying to teach myself how to encode epubs, and with that I'm trying to make better InDesign files for epub output. One of the things I'd like to do is use scripting to make linking a little easier.

       

      Thanks so much.

       

      p.s. please be nice. I am n00b so if you need more info, or I'm in the wrong place I'll kindly try to fix my error.

        • 1. Re: Scripting hyperlinks
          Kasyan Servetsky Level 5

          Here is a simple example -- the script looks for the text formatted with "Anchor" character style and creates anchors from it. The name of the anchor is the same as found text.

           

          // Written by Kasyan Servetsky
          // June 30, 2011
          // http://www.kasyan.ho.com.ua
          // e-mail: askoldich@yahoo.com
          //=========================================================================================================================
          if (app.documents.length == 0) ErrorExit("Please open a document and try again.");
          
          const gScriptName = "Create Text Anchors"; // Name of the script
          const gScriptVersion = "1.0"; // Version
          var gDoc = app.activeDocument;
          if (!gDoc.characterStyles.item("Anchor").isValid) ErrorExit("Character style \"Anchor\" doesn't exist.");
          
          CreateDestinations();
          
          //======================================================== FUNCTIONS  =====================================================
          function CreateDestinations() {
              app.findGrepPreferences = app.changeGrepPreferences = NothingEnum.nothing;
              app.findGrepPreferences.findWhat = ".+";
              app.findGrepPreferences.appliedCharacterStyle = gDoc.characterStyles.item("Anchor");
              var finds = gDoc.findGrep();
              
              var destCounter = 0;
          
              for ( var j = finds.length-1; j >= 0; j-- ) {
                  var found = finds[j];
                  try {
                      if (!gDoc.hyperlinkTextDestinations.itemByName(found.contents).isValid) {
                          var hypTextDest = gDoc.hyperlinkTextDestinations.add(found);
                          hypTextDest.name = found.contents;
                          destCounter++;
                      }
                  }
                  catch(e) {
                      //$.writeln("Error: in CreateDestinations - " + e);
                  }
          
              }
          
              if (destCounter == 0) {
                  alert("No text anchors have been created.", gScriptName + " - " + gScriptVersion);
              }
              else if (destCounter == 1) {
                  alert("One text anchor has been created.", gScriptName + " - " + gScriptVersion);
              }
              else if (destCounter > 1) {
                  alert(destCounter  + " text anchors have been created.", gScriptName + " - " + gScriptVersion);
              }
          }
          //--------------------------------------------------------------------------------------------------------------------------------------------------------
          function ErrorExit(error, icon) {
              alert(error, gScriptName + " - " + gScriptVersion, icon);
              exit();
          }
          //--------------------------------------------------------------------------------------------------------------------------------------------------------
          

           

          Here is a much more complex example -- a script that batch processes InDesign documents creating x-refs.

           

          Hope this helps.
          Kas

          • 2. Re: Scripting hyperlinks
            jeslaine Level 1

            Thanks so much Kasyan!

             

            My style was a paragraph style rather than a character style but I was easily able to change that in the script.

             

            You've saved me hours of frustration trying to figure out how to make it work! Thank you

            • 3. Re: Scripting hyperlinks
              Colin Flashman Adobe Community Professional

              Hello all.

               

              Sorry to resurrect an old post, but I am not having as much luck as the OP.

               

              Unlike the OP, I am using IDCS6 running on Mac OSX 10.9.4.

               

              I have tried the script on a 200pp file that I have created via a data merge of 100 records (each record is 2pp long, yielding a 200pp indesign file). The script looks like it is working (i.e. a dialog box tells me that there are 100 anchors made):

               

              1.png

              The story editor shows me that there is indeed an anchor:

              3.png

              And indeed, if I select new hyperlink from the hyperlinks palette, my bookmarks do appear:

              2_5.png

              However, no bookmarks appear in the bookmark list:

              2.png

              If I make a bookmark by highlighting some text elsewhere and using the "new bookmark" from the flyout tool, the bookmark appears in the bookmark palette; and in the story editor:

              4.png

              Is the script intended to just make a text anchor destination BUT not necessarily make it a bookmark? Is there a way using the script to make the text anchor destinations appear in the bookmarks palette?

               

              As a workaround, I have tried running the scripts subfolders of the Javascript Folder in InDesign labelled "Version 6.0 Scripts" (as well as for versions 7,5,4 & 3) and is still no better.

               

              Colly