3 Replies Latest reply on Dec 13, 2016 3:16 AM by Obi-wan Kenobi

    Code to be evaluated! [011] Hyperlinks to be removed! …

    Obi-wan Kenobi Adobe Community Professional

      Hi Scripters,

       

      Another issue about hyperlinks!

       

      As shown in the screenshot below, destinations are missing! So, the deal is to remove the concerned hyperlinks!

       

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

       

      I wrote this code but it doesn't work! 

       

      var myDoc = app.activeDocument;
      var myHyperlinks = myDoc.hyperlinks.everyItem().getElements();
      for ( H = 0; H < myHyperlinks.length; H++ ) 
          {
              var myDestination = myHyperlinks[H].HyperlinkTextDestination.destinationText;
              if ( !myDestination.isvalid )  myHyperlinks[H].remove();
          }
      

       

       

      Thanks in advance for your help!

       

      (^/)

        • 1. Re: Code to be evaluated! [011] Hyperlinks to be removed! …
          Obi-wan Kenobi Adobe Community Professional

          Fixed! 

           

          var myDoc = app.activeDocument;
          var myHyperlinks = myDoc.hyperlinks.everyItem().getElements();
          for ( H = 0; H < myHyperlinks.length; H++ ) 
              {
                  var myDestination = myHyperlinks[H].destination;
                  if ( myDestination == null )  myHyperlinks[H].remove();
              }
          

           

          (^/)

          • 2. Re: Code to be evaluated! [011] Hyperlinks to be removed! …
            Loic.Aigon Adobe Community Professional

            Hi Obi,

             

            If you are looking for improvements, you may want to :

             

            1) Include you code into a function to reduce global scope variables. The more global variables you have the slower your script will go.

            2) Check object references validity. your script may throw an error if the script is run without any open document.

            3) Use a reference to the array length instead of reading the property every loop

            4) Besides you do not need to transform the collection into an array here as you don't take advantage of any specific array only function such as sorting, slicing, concatening…

             

            So you script could have been:

             

            var main = function() {
            
              var d = app.properties.activeDocument, 
              hs, h, ds, n = 0;
            
              if ( !d) return;
            
              hs = d.hyperlinks, n = hs.length;  
            
              while ( n-- )  {
              h = hs[n];
              ds = h.properties.destination;
              !ds && h.remove();
              }
            
            
            }
            
            
            main();
            

             

            HTH

            Loic

            http://www.ozalto.com/

            1 person found this helpful
            • 3. Re: Code to be evaluated! [011] Hyperlinks to be removed! …
              Obi-wan Kenobi Adobe Community Professional

              Hi Loïc,

               

              Nice code and interesting writing! 

              Even if mine works, I indicate it as "correct"! Sample to be followed!

               

              I take it! Thanks!

               

              (^/)