13 Replies Latest reply on Jan 9, 2013 10:43 PM by *Rike*

    Delete last element on page with certain scriptlable

    *Rike*

      Hi @ all,

      I try to solve the following problem, hopefully one of you can help me:

       

      I import xml files from a database in InDesign (CS 5.5, Windows 7) to get a complete product cataloque.

      Before each group of products a subheadline (seperate textframe with the scriptlable "katalog_untergruppe") is inserted, but in case of a page break it's the last element on a page.

      Can I somehow select these last elements and delete them with a script in the InDesign dokument?

       

      I tried to do it on counting the objects on each page and delete the frame if it has a higher number than 40, but the number of objects differs, so sometimes needed frames are deleted and some are still there.

      So I need to check if it is the last element on the page. Any ideas?

       

      Here my test:

       

      function test(){

          for(var i=0;i<document.allPageItems.length; i++){

                  if(document.allPageItems[i].label != ""){

                      //pagestart

                      if(document.allPageItems[i].label == 'katalog_untergruppe'){

                          var n= 1;

                              while(document.allPageItems[i+n].label == ""){

                                  n++;

                              }

                          if(n>40){

                              document.allPageItems[i].remove();

                           }

                       }

                      if(document.allPageItems[i].label == 'katalog_untergruppe' && document.allPageItems[i+1].label == 'pagestart'){

                          document.allPageItems[i].remove();

                       }

                  }

          }

          }

      test();

       

       

      Thanks in advance :-)

      *Rike*

        • 1. Re: Delete last element on page with certain scriptlable
          Jump_Over Level 5

          Hi,

           

          Your test function is "page_independent" (it takes even items from masterSpreads)

          You should iterate over pages and page[k].allPageItems.

          But more important is what is the last item of page for you (and for script)?

          Nearest to the bottom? Last placed? Container of last part of text which is linked story?

          len = page[k].allPageItems.length

          page[k].allPageItems[len - 1] ==> this is a pageItem placed on the bottom of last page layer (last if you mean deepest placed);

           

          How to find the last item depends on how you define it.

           

          rgds

          • 2. Re: Delete last element on page with certain scriptlable
            *Rike* Level 1

            How to find the last item depends on how you define it.

             

            The item I'm searching for is the textframe deepest placed on the page except those from the masterpage. It has to be marked with the scriptlable 'katalog_untergruppe' as well.

            I think it's also the last placed. But not quite sure.

             

            Can you please help me to get my script working?

            I'm not good in scripting at all, sorry. The test script was mostly done by my colleque (but he left the firm) so it's on me to find a soloution.

             

            thx

            • 3. Re: Delete last element on page with certain scriptlable
              Jump_Over Level 5

              Hi,

              Try this:

              -------

              myPages = app.activeDocument.pages;

              Highlight = app.activeDocument.swatches.add(

                  {name: "highlight",

                      space: ColorSpace.RGB,

                      model: ColorModel.PROCESS,

                      colorValue: [255,255,150]

                      } ;

               

              for (k=0; k<myPages.length; k++) {

                  currPItems = myPages[k].allPageItems;

                  last = currPItems.length - 1;

                  for (i = last; i >=0; i--)

                      if (currPItems[i].label == "katalog_untergruppe") {

                          currPItems[i].fillColor = Highlight;   

                             // if it works OK change above to: currPItems[i].remove()

                          break;

                          }

                  }

              -------

              This code should fill every 'last Page' item (deepest placed with chosen label) with light yellow.

              If it is OK change proper line to remove().

               

              rgds

              • 4. Re: Delete last element on page with certain scriptlable
                Laubender Adobe Community Professional & MVP

                @Rike – if you are looking for the last text frame that was created in the document, you could look for the highest ID in all text frames*.

                 

                To get all IDs for all text frames in the document, you can gather them in an Array by everyItem().
                After getting all the IDs, you can do sort them by numbers and return the highest value  with pop().
                pop() takes away the last element of the array and returns its value (in this example the highest ID number).

                 

                *One drawback on this:

                 

                myDocument.textFrames.everyItem();
                

                 

                can only get to all "plain" text frames.

                For example: anchored ones will not be listed…

                There are other exceptions possible with MSOs (MultiStateObjects) as well.

                 

                But if you are looking for a "normal" text frame including ones on master pages, the following snippet could work:

                 

                //Here a simple Number sort function:
                function Numsort (a, b) {return a - b};
                
                var d=app.documents[0];
                
                //The last "text frame" created has the highest ID number of all text frames in the document:
                var myLastCreatedTextFrame = d.textFrames.itemByID(d.textFrames.everyItem().id.sort(Numsort).pop());
                
                //Reading the label String:
                myLastCreatedTextFrame.label;
                

                 

                 

                If you are not looking after the last text frame that was  created most recently, then we need another discription of the problem.

                 

                 

                Uwe

                • 5. Re: Delete last element on page with certain scriptlable
                  Laubender Adobe Community Professional & MVP

                  Of course you can restrict everyItem() on the text frames to one single page or spread, if you are absolutely sure of the  name or the page's documentOffset property.

                   

                  Uwe

                  • 6. Re: Delete last element on page with certain scriptlable
                    *Rike* Level 1

                    @ Jump_over:

                     

                    It's now deleting the first textframe with the label "katalog untergruppe" not the last one on each page :-)

                     

                    function test(){

                    myPages = app.activeDocument.pages;

                     

                    for (k=0; k<myPages.length; k++) {

                        currPItems = myPages[k].allPageItems;

                        last = currPItems.length - 1;

                        for (i = last; i >=0; i--)

                            if (currPItems[i].label == "katalog_untergruppe") {

                                currPItems[i].remove();  

                                   // if it works OK change above to: currPItems[i].remove()

                                break;

                                }

                        }

                    }

                    test();

                     

                    @ Uwe
                    I'm not really sure if it is the last created item on each page, but it's definitly the nearest to te bottom of each page.

                    • 7. Re: Delete last element on page with certain scriptlable
                      Jump_Over Level 5

                      Hi,

                       

                      So modify:

                      ---

                      last = currPItems.length;

                          for (i = 0; i < last; i++)

                      ---

                       

                      To tell the true you should check your item's position on Layers panel.

                      Item which is on the top there is currPItems[0] for script;

                      Item from the bottom of that list (lists) is currPItems[last-1].

                       

                      rgds

                      • 8. Re: Delete last element on page with certain scriptlable
                        -hans- Level 4

                        Hello Rike,

                         

                        I guess not only the label but also the contents of the textframes involved are the same?!

                         

                        Then just create a array of all textframes with the specific label. Find those whose content is not unique. Compare the parentPage.documentOffset ...

                         

                        Hope it'll help

                         

                        Hans-Gerd Claßen

                        • 9. Re: Delete last element on page with certain scriptlable
                          Laubender Adobe Community Professional & MVP

                          I'm not really sure it it is the last placed item on each page, but it's definitly the nearest to te bottom of each page.

                           

                          @Rike – you could find out by looking for its ID and compare it to the IDs of all other text frames on the page. I think we could assume that it is the last one created on the page.

                           

                          But if I get you right, you have two cases you could distinguish:

                           

                          Case 1: one single text frame labeled "katalog untergruppe" on a single page => NO problem

                          Case 2: two (or even more??) text frames labeled "katalog untergruppe" on a single page => PROBLEM

                           

                          And you want to get rid of the superfluous text frames.

                           

                          For Case 2 you cannot exactly predict where on the page the superfluous text frame(s) are positioned.


                          Only (and that's the real question here) they are positioned with a higher y1-value compared with the one that's ok??

                           

                          So forget my script snippets in #4. If your answer for my question is "yes", we have to look for the geometric bounds of all  text frames with the label "katalog untergruppe" page by page.

                           

                          If no text frame of interest is present => OK
                          If one text frame is present => OK
                          If more than one text frames of interest are present, leave the one alone that has the smallest y1-value of the geometric bounds. Remove all the others.

                           

                          Is that a fair description?

                           

                          Uwe

                          • 10. Re: Delete last element on page with certain scriptlable
                            *Rike* Level 1

                            @ Jump_over

                            Thanks, getting closer

                            Now it's deleting the last/lowest textframe with the scriptlabel on each page, but even if there are other elements following.

                            I checked the layers panel, if it's the lowest element on the page, it's on the top of the list. And only in that case it should be deleted.

                             

                             

                            @Hans

                            The content of the frames is different just the label is the same.

                            • 11. Re: Delete last element on page with certain scriptlable
                              -hans- Level 4

                              exampleDocument would have been fine ;-)

                               

                              Bit of searching in the dark ;-) ;-)

                               

                              Uwes approach should work at least.

                              • 12. Re: Delete last element on page with certain scriptlable
                                Jump_Over Level 5

                                Hi,

                                so simplify it (but that is a searching in the dark, indeed):

                                 

                                function test(){

                                myPages = app.activeDocument.pages;

                                for (k=0; k<myPages.length; k++) {

                                    currPItems = myPages[k].allPageItems;

                                        if (currPItems[0].label == "katalog_untergruppe")

                                            currPItems[i].remove();

                                    }

                                }

                                test();

                                 

                                rgds

                                • 13. Re: Delete last element on page with certain scriptlable
                                  *Rike* Level 1

                                  @ Jump_Over

                                  That's not working either, sometimes now other textframes are deleted.

                                  With the help of someone else in combination with yours, I now found a working solution.

                                  Searching for the lowest element, if it's labeled "katalog_untergruppe", delete it.

                                  Thanks a lot.

                                   

                                  @Hans

                                  Sorry for not posting a file, but I'm not allowed to.

                                   

                                  @all

                                  Here a working script for the problem, perhaps it's helpful.

                                   

                                  function test(){

                                  for (var i = 0; i < app.activeDocument.pages.length; i++) {

                                      var currentPage = app.activeDocument.pages[i];

                                      var lastpageItems;

                                      for (var j = 0; j < currentPage.pageItems.length; j++) {

                                          var currentpageItems = currentPage.pageItems[j];

                                          if (j == 0)

                                              lastpageItems = currentpageItems;

                                          else if (currentpageItems.geometricBounds[0] > lastpageItems.geometricBounds[0])

                                              lastpageItems = currentpageItems;

                                      }

                                      if (lastpageItems.label == 'katalog_untergruppe')

                                          lastpageItems.remove();

                                  }

                                  }

                                  test();