6 Replies Latest reply on Jan 25, 2013 1:56 AM by Marc Autret

    Remove content from every text frame on the active page.

    bgfrhlp

      Dear all,

       

      Just wrote a simple script, which removes content of every text frame on the active page.

       

      app.activeDocument.layoutWindows[0].activePage.textFrames.everyItem().contents = "";

       

      If there is a more efficient way to do this, please don't hesitate to post it here.

       

      Cheers!

      bgfrhpl

        • 1. Re: Remove content from every text frame on the active page.
          Marc Autret Level 4

          Hi bgfrhlp,

           

          According to my tests

           

          app.activeWindow.activePage.textFrames.everyItem().

          texts.everyItem().remove();

           

          is 2X faster than:

           

          app.activeWindow.activePage.textFrames.everyItem().

          contents = '';

           

          but this probably depends on the ID version and other paramaters.

           

          Anyway, take note that neither your code, nor mine, can deal with overset contents and/or threaded frames on multiple pages.

           

          @+

          Marc

          • 2. Re: Remove content from every text frame on the active page.
            Laubender Adobe Community Professional & MVP

            Hi, Marc & bgfrhlp – and it would destroy anchored text frames, anchored images and tables, too…

             

            Uwe

            • 3. Re: Remove content from every text frame on the active page.
              bgfrhlp Level 1

              Thanks for the replies,

               

              Uwe,

              True. On the topic I wrote "remove content". It is a simple script and I wrote it for a special purpose for myself. I didn't find anything on that in this forum, so I thought why not put it there.

               

              Marc,

              I tested your code and mine was about 25% faster (Windows 7 Ultimate, InDesign CS5.5). And I thought first seeing your script that removing content should be faster than adding new content. How odd. Did you use $.hiresTimer to measure time? Anyway, in my use the time spent is around one second so it makes no big difference. The remove() function also demands content to be in every frame.

               

              Cheers!

              bgfrhlp

              • 4. Re: Remove content from every text frame on the active page.
                Marc Autret Level 4

                > The remove() function also demands content to be in every frame.

                 

                Right!!! I forgot that Text.remove() triggers a runtime error when the Text object happens to be empty—which IMHO is really a bad design choice. Anyway...

                 

                > Did you use $.hiresTimer to measure time?

                 

                Yep. I've used ComparePerf.js — http://www.indiscripts.com/post/2011/06/comparing-the-performance-of-extendscript-snippets — with those functions:

                 


                function fRemove()
                {
                    app.activeWindow.activePage.textFrames.everyItem().texts.everyItem().remove();
                    app.activeDocument.undo();
                };
                
                function fEmptyContents()
                {
                    app.activeWindow.activePage.textFrames.everyItem().contents = '';
                    app.activeDocument.undo();
                };
                
                comparePerf(fEmptyContents, fRemove);
                

                 

                and it typically returned:

                 

                ============================
                
                fEmptyContents   vs.    fRemove
                
                ============================
                
                
                Average time after 10 passes:
                
                   fEmptyContents:  13109 µs
                   fRemove:  6603 µs
                
                RATIO:  1.9
                

                 

                 

                @+

                Marc

                • 5. Re: Remove content from every text frame on the active page.
                  bgfrhlp Level 1

                  With the codes below I get constantly the result I mentioned above.

                   

                  $.hiresTimer;

                  app.activeDocument.layoutWindows[0].activePage.textFrames.everyItem().contents = "";

                  alert($.hiresTimer);

                   

                  and

                   

                  $.hiresTimer;

                  app.activeWindow.activePage.textFrames.everyItem().texts.everyItem().remove();

                  alert($.hiresTimer);

                   

                   

                  In my computer comparePerf(fRemove, fEmptyContents); returns

                  Capture1.PNG

                   

                   

                  whereas (when switching the parameters) comparePerf(fEmptyContents, fRemove); returns

                   

                  Capture2.PNG

                   

                   

                  It seams that the latter function gets an edge for some reason.

                   

                  Cheers!
                  bgfrhlp

                  • 6. Re: Remove content from every text frame on the active page.
                    Marc Autret Level 4

                    "after undefined passes" indicates that you probably have removed:

                     

                    comparePerf.PASSES = 10; // or any other positive integer

                     

                    Therefore comparePerf only triggered a single pass, I think.

                     

                    (As DOM access and script launches are highly time-indeterministic, I suggest you set at least 10 passes to get sth significant... Anyway I admit this is not an ultimate tool to measure performances, there are so many imponderables from one system to another!)

                     

                    @+

                    Marc

                     

                    EDIT: I'd love to have an online tool like http://jsperf.com dedicated to InDesign devs ;-)