2 Replies Latest reply on Dec 12, 2016 3:47 AM by Obi-wan Kenobi

    Code to be evaluated! [010] Hyperlinks! …

    Obi-wan Kenobi Adobe Community Professional

      Hi Scripters!

       

      A code with a very strange behavior! [for me, of course!!  ]

       

      It's about a glossary!

       

      On the double page below, the glossary text is on the 2nd page, the current text on the 1rst one.

      The deal is to find in the current text all the occurrences of each word or words group we have in red in the glossary [grep style // the word(s), a " – " and its definition/explanation], see paras in blue. And finally create hyperlinks on all these words with the glossary as target!

       

      Capture d’écran 2016-12-12 à 00.24.45.png

       

      When I launch the script, all seems to go right: the script finds the 4 words groups in the current text and mark them! …

      But, as indicated in the code, the alert "alert(mySources.length)" gives "zero" at each loop, even if the script finds and treats something!

       

      Capture d’écran 2016-12-12 à 00.24.12.png

       

      Really no idea! 

       

      As usual, many thanks for your help!

       

       

      app.doScript("main()", ScriptLanguage.javascript, undefined, UndoModes.FAST_ENTIRE_SCRIPT, "Glossary! …");  
        
      function main()  
      { 
      
      
      var myDoc = app.activeDocument;
      var myCondition = myDoc.conditions.item("GlossaryDone");
      if (!myCondition.isValid)  myCondition = myDoc.conditions.add ({name: "GlossaryDone", indicatorMethod: ConditionIndicatorMethod.USE_HIGHLIGHT});
      
      
      app.findGrepPreferences = app.changeGrepPreferences = null;
      app.findGrepPreferences.appliedCharacterStyle = "Glossary";
      app.changeGrepPreferences.appliedConditions = [myCondition];
      myDoc.changeGrep();
      
      
      var myAnchors = myDoc.findGrep();
      
      
      for (var A = 0; A < myAnchors.length; A++)
          {
              var myAnchor = myAnchors[A];
      
      
              myDestination = myDoc.hyperlinkTextDestinations.add(myAnchor, {name: myAnchor.contents});
      
      
              app.findGrepPreferences = app.changeGrepPreferences = null;
              app.findGrepPreferences.findWhat = "(?i)\\b" + myAnchor.contents + "s?\\b";
              app.findGrepPreferences.appliedConditions = [];
              app.changeGrepPreferences.appliedConditions = [myCondition];
              myDoc.changeGrep();
              
              var mySources = myDoc.findGrep();
              
              alert(mySources.length) // -----------------------------------  VERY WEIRD!!!  mySources.length systematically = 0!  :(  So, no hyperlinks created!!!
              
              for (var S = 0; S < mySources.length; S++)
                  {
                      var mySource = myDoc.hyperlinkTextSources.add(mySources[S], {name: "Source_" + S});  
                      var myHyperlink = myDoc.hyperlinks.add(mySource, myDestination, {name: "Hyperlink_" + myDestination.name + "_" + S});  
                  }  
          }
      
      
      }
      

       

       

      (^/)  [ … a Jedi very tired tonight! ]