5 Replies Latest reply on May 10, 2017 12:27 AM by tomz41329778

    Scripting and smartTextReflow

    a2d2a Level 1

      Hi, I've been through loads of posts on this forum and haven't found an answer although a couple of people have had similar problems. Hopefully some guru will have an answer.

      1. ImportXML via script - OK

      2. Run some XML Rules e.g. adding page breaks - OK

      3. Having smartTextReflow via a master page primary text flow - NOT OK

       

      As others have pointed out, smartTextReflow runs asynchronously. I know this to be true since getting the page count after step 2 returns the initial number of pages (2), not the final number of pages.

       

      Have tried:

      app.waitForAllTasks()

      document.recompose()

      $.sleep(1000...)

      All to no avail.

       

      Is there any event listener or way to trigger some code (to remove any empty pages etc) after the reflow has finished?

        • 1. Re: Scripting and smartTextReflow
          Skemicle Level 3

          after the reflow script call this function.

           

          function removeEmptyPages(){

              var doc = app.activeDocument;

              var pages = doc.pages;

              for(c=0;c<pages.length;c++){

                  if(pages[c].pageItems.length == 0){

                      pages[c].remove();

                      c--;

                  }

              }

          }

          • 2. Re: Scripting and smartTextReflow
            a2d2a Level 1

            Thanks, but the point is that after importing XML and running some XML rules the page count is still the initial page count until after some arbitrary time when InDesign has finished processing XML and reflowing it

            • 3. Re: Scripting and smartTextReflow
              a2d2a Level 1

              OK, I found a solution, but it would be much nicer if there was some event that could be handled asynchronously when the reflowing has finished.

              The alternative to an async callback is polling, so when I tried using the plain JS but unsupported setTimeout() I came across the ExtendScript equivalent: IdleTasks e.g.:

               

              //set up IdleTask
              var myIdleTask = app.idleTasks.add({name:"myIdleTask", sleep:100}); //Run every 100ms - adjust as required
              var onIdleEventListener = myIdleTask.addEventListener("onIdle", testPageCount); // That's emulated setTimeout...
              
              xmlImport();     // Do XML Import
              runXMLRules();     // Modify XML
              ...               // Other code...
              
              var testCounter=0, initialLength=0;
              function testPageCount() {
                   if (testCounter==0) initialLength=document.pages.length;     //Get initial page count
                   testCounter++;
                   if (document.pages.length != initialLength) {
                        $.writeln("["+(testCounter/10)+"s] FINISHED AUTOFLOW");     
                        myIdleTask.removeEventListener(IdleEvent.ON_IDLE, testPageCount);     // Remove IdleTask
              
                        ...     // Whatever you want to do now document reflowed...
              
                   }
              }
              
              1 person found this helpful
              • 4. Re: Scripting and smartTextReflow
                Skemicle Level 3

                have you tried to define the number of pages before the reflow and compare if the number of pages is the same..

                 

                something like this maybe..??

                var initialPages = app.activeDocument.pages.length

                 

                //script

                reflow();

                 

                function reflow(){

                     if(app.activeDocument.pages.length == initialPages){

                          reflow();

                     }else {removeEmptyPages();

                     }

                }

                function removeEmptyPages(){

                    var doc = app.activeDocument;

                    var pages = doc.pages;

                    for(c=0;c<pages.length;c++){

                        if(pages[c].pageItems.length == 0){

                            pages[c].remove();

                            c--;

                        }

                    }

                }

                 

                 

                 

                I don't know if this will work but it seems like it should. Of course if the reflow doesn't change the number of pages this wouldn't be effective but by what I'm understanding the number of pages will always change?

                • 5. Re: Scripting and smartTextReflow
                  tomz41329778

                  Hi,

                   

                  I know this post is from digital ages ago, but I am trying to implement this and can't get any further.
                  Can you help me out please?

                   

                  Regards,

                   

                  Tom