2 Replies Latest reply on Jan 19, 2017 1:50 AM by Oriup

    run faster than current speed

    Oriup Level 1

      Hi,

       

      I have made a script that is working good, but running very slowly.

      Can any edit run faster than current?

       

      #target "InDesign-8.0"
      var doc = app.activeDocument;  
      attrDelete(doc);  
      function attrDelete(span)  
      {  
          for (var i = 0; i < span.xmlElements.length; i++)  
          {   
              try{  
                  for(n=0; n<span.xmlElements[i].xmlAttributes.length; n++)  
                  {  
                      if(span.xmlElements[i].xmlAttributes[n].name == "aid:cstyle")  
                      {  
      
      
                      if(span.xmlElements[i].xmlContent.contents <= 643 && span.xmlElements[i].xmlContent.contents>= 608){
                          span.xmlElements[i].xmlAttributes.add("href", "M18_BERK9695_07_SE_C18.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 607 && span.xmlElements[i].xmlContent.contents>= 568){
                          span.xmlElements[i].xmlAttributes.add("href", "M17_BERK9695_07_SE_C17.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 567 && span.xmlElements[i].xmlContent.contents>= 536){
                          span.xmlElements[i].xmlAttributes.add("href", "M16_BERK9695_07_SE_C16.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 535 && span.xmlElements[i].xmlContent.contents>= 506){
                          span.xmlElements[i].xmlAttributes.add("href", "M15_BERK9695_07_SE_C15.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 505 && span.xmlElements[i].xmlContent.contents>= 468){
                          span.xmlElements[i].xmlAttributes.add("href", "M14_BERK9695_07_SE_C14.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 467 && span.xmlElements[i].xmlContent.contents>= 436){
                          span.xmlElements[i].xmlAttributes.add("href", "M13_BERK9695_07_SE_C13.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 435 && span.xmlElements[i].xmlContent.contents>= 406){
                          span.xmlElements[i].xmlAttributes.add("href", "M12_BERK9695_07_SE_C12.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 405 && span.xmlElements[i].xmlContent.contents>= 366){
                          span.xmlElements[i].xmlAttributes.add("href", "M11_BERK9695_07_SE_C11.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 365 && span.xmlElements[i].xmlContent.contents>= 334){
                          span.xmlElements[i].xmlAttributes.add("href", "M10_BERK9695_07_SE_C10.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 333 && span.xmlElements[i].xmlContent.contents>= 292){
                          span.xmlElements[i].xmlAttributes.add("href", "M09_BERK9695_07_SE_C09.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 291 && span.xmlElements[i].xmlContent.contents>= 256){
                          span.xmlElements[i].xmlAttributes.add("href", "M08_BERK9695_07_SE_C08.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 255 && span.xmlElements[i].xmlContent.contents>= 214){
                          span.xmlElements[i].xmlAttributes.add("href", "M07_BERK9695_07_SE_C07.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 213 && span.xmlElements[i].xmlContent.contents>= 182){
                          span.xmlElements[i].xmlAttributes.add("href", "M06_BERK9695_07_SE_C06.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 181 && span.xmlElements[i].xmlContent.contents>= 148){
                          span.xmlElements[i].xmlAttributes.add("href", "M05_BERK9695_07_SE_C05.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 147 && span.xmlElements[i].xmlContent.contents>= 114){
                          span.xmlElements[i].xmlAttributes.add("href", "M04_BERK9695_07_SE_C04.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 113 && span.xmlElements[i].xmlContent.contents>= 74){
                          span.xmlElements[i].xmlAttributes.add("href", "M03_BERK9695_07_SE_C03.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else if(span.xmlElements[i].xmlContent.contents <= 73 && span.xmlElements[i].xmlContent.contents>=42){
                          span.xmlElements[i].xmlAttributes.add("href", "M02_BERK9695_07_SE_C02.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
                      else(span.xmlElements[i].xmlContent.contents <= 41){
                          span.xmlElements[i].xmlAttributes.add("href", "M01_BERK9695_07_SE_C01.xhtml#page"+span.xmlElements[i].xmlContent.contents);  
                          span.xmlElements[i].markupTag = "a";
                          span.xmlElements[i].xmlAttributes.item("class").remove()
                          }
      
      
      
      
      
      
                      }  
                  }  
              }catch(e){}  
              attrDelete(span.xmlElements[i]);  
          }  
      }
      alert("Done!\rCreated by Sumit from Elhi 7.")
      

       

       

      Sumit

        • 1. Re: run faster than current speed
          Marc Autret Level 4

          Hi Oriup,

           

          Here is likely how I would do that, although there might be a faster approach:

           

          // YOUR SETTINGS
          // ---
          const ATT_NAME_TARGET = 'aid:cstyle';
          const HREF_PATTERN = "M%1_BERK9695_07_SE_C%1.xhtml#page%2";
          const RANGES = [41,73,113,147,181,213,255,291,333,365,405,435,467,505,535,567,607,643];
          
          const PAGE_TO_SECTION = (function(/*uint[]&*/a,  n,s,i)
          // -------------------------------------
          // Return the func that converts a page number into a
          // section number (1..N) depending on RANGES.
          // E.g. PAGE_TO_SECTION(320) => 9
          {
              for( n=a.length, s='1', i=-1 ; ++i < n ; s+='+(x>'+a[i]+')' );
              return Function('x', 'return '+s);
          })(RANGES);
          
          const updateAttributes = function F(/*DOM*/span,  a,i,x,atts,p,s,t)
          // -------------------------------------
          // Recursive function that update all XMLElems having an
          // ATT_NAME_TARGET attribute.
          {
              // Aliases.
              // ---
              const __ = $.global.localize;
              const P2S = PAGE_TO_SECTION;
          
              // Loop.
              // ---
              for( a=span.xmlElements.everyItem().getElements(), i=a.length ; i-- ; F(x) )
              {
                  atts = (x=a[i]).xmlAttributes;
          
                  // Check some conditions.
                  // ---
                  if( !atts.itemByName(ATT_NAME_TARGET).isValid ) continue;
                  if( ! ('contents' in (t=x.xmlContent)) ) continue;
                  
                  // Page number (assuming contents represents such number!)
                  p = parseInt(t.contents,10);
          
                  // Apply the 'a' markuptag to the xmlElement.
                  // ---
                  x.markupTag = 'a';
          
                  // Add (or rewrite!) the 'href' attribute.
                  // ---
                  if( (t=atts.itemByName('href')).isValid ) t.remove();
                  isNaN(p) || atts.add('href', __(HREF_PATTERN,('0'+P2S(p)).substr(-2),p));
          
                  // Remove the 'class' attribute, if present.
                  // ---
                  if( (t=atts.itemByName('class')).isValid ) t.remove();
              }
          };
          
          
          // Test.
          // ---
          var doc = app.properties.activeDocument;
          updateAttributes(doc);
          alert("Done!");
          

           

          @+

          Marc

          • 2. Re: run faster than current speed
            Oriup Level 1

            Thank you Marc.