2 Replies Latest reply on Apr 5, 2014 12:27 AM by gaphic2

    CS5: Hyperlink to text anchor from character style?

    gaphic2 Level 1

      Is it possible to script the following:

       

      A text has several occurances of the same string, e.g. 'A10'. One is marked with a character style 'Anchor' and the others are marked with a character style 'Link'. The idea is to create text anchors from the 'Anchor' style and then find every occurrence of the character style 'Link' and create a hyperlink to the matching anchor.

       

      I've managed to find a script that creates text anchors from a character style 'Anchor'. Can someone offer some suggestions on how to do script creating the links?

       

      // Written by Kasyan Servetsky

      // March 13, 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";

      const gScriptVersion = "1.0";

      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) {}

          }

       

          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();

      }

      //---------------------------------------------------------------------------------------- ----------------------------------------------------------------

        • 1. Re: CS5: Hyperlink to text anchor from character style?
          Jump_Over Level 5

          Hi,

           

          The way is:

          create hyperlinkTextDestination;

          create hyperlinkTextSources;

          create hyperlinks using one destination and various sources;

           

          so:

          // to create hyperlinks alike:    one destination==>many sources
          // destination is the 1st occurrence of text with charStyle "anchor" applied
          // sources are each occurrences of text with charStyle "link" applied
          
          var mDoc = app.activeDocument;
          
          app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
          app.findTextPreferences.appliedCharacterStyle = mDoc.characterStyles.item("anchor");
          
          var 
               mAnchor = mDoc.findText()[0],
               mDest;
          
          if (mAnchor) mDest = mDoc.hyperlinkTextDestinations.add(mAnchor,{name: mAnchor.words[0].contents});
          else {alert ("no anchor found"); exit(); }
          
          app.findTextPreferences.appliedCharacterStyle = mDoc.characterStyles.item("link");
          
          var 
               mSource = mDoc.findText(),
               len = mSource.length,
               currSource, currHyper;
          
          if (!len) {alert ("no link found"); exit(); }
          
          while (len-->0) {
               currSource = mDoc.hyperlinkTextSources.add(mSource[len],{name: "sourceLink_" + len});
               currHyper = mDoc.hyperlinks.add(currSource, mDest, {name: "mHyperlink_" + mDest.name + "_" + len});
               }
          
          app.findTextPreferences = app.changeTextPreferences = NothingEnum.nothing;
          

           

          Jarek

          • 2. Re: CS5: Hyperlink to text anchor from character style?
            gaphic2 Level 1

            Many thanks, Jarek. You make it all look so simple, awesome script.