5 Replies Latest reply on Sep 11, 2010 6:46 PM by Yuliaart

    Length of Rectangles on a page in InDesign CS5 js

    Yuliaart Level 1

      Hi, I need to have amount of rectangles on specific layer on specific page, and it doesn't work together:

       

      myBackRectangles = myDoc.pages[1].rectangles.length(myDoc.layers.item ("Layer 1"));
      

       

      It gives me length of the rectangles on all layers, but I need specific layer and that doesn't work.

       

      Thank you for your help.

      Yulia

        • 1. Re: Length of Rectangles on a page in InDesign CS5 js
          [Jongware] Most Valuable Participant

          The nested parentheses stuff you wrote makes no sense at all, for Javascript.

          Write it the other way around:

           

          myBackRectangles = myDoc.layers.item ("Layer 1").rectangles.length;

           

          This will tell you the number of all rectangles on that particular layer for the entire document; you cannot use the combination "page x and layer y" in one single command, because pages don't have layers, it's the document that owns them.

           

          With that in mind, you can choose what works best for you:

           

          a. Collect all rectangles on that layer and go over them, only counting the ones that occur on a particular page, or

           

          b. Collect all rectangles on that page and go over them, only counting the ones that occur on a particular layer.

          • 2. Re: Length of Rectangles on a page in InDesign CS5 js
            Yuliaart Level 1

            I think I am getting closer, but when I am running the following script, the alert shows that all rectangles on the 1st page, even though I have only one rectangle on the 1st page and 3 on the second:

             

             

            myBackRectangles = myDoc.layers.item ("Layer 1").rectangles;
                
                for (var a =0; a<myBackRectangles.length; a++){
                      alert (myBackRectangles[a].parentPage.index);
                    if (myBackRectangles[a].parentPage.index == 1){
                        var myArtBack = myBackRectangles[a].graphics[0];
                        if (myArtBack.isValid == false){
                            myDoc.pages[1].remove();
                        }
                    }
                }

             

             

            Is it something I am missing.

             

            Thank you.

            Yulia

            • 3. Re: Length of Rectangles on a page in InDesign CS5 js
              PankChat Level 1

              Well I am not following the thread thoroughly but it seems you should be

              using

              alert (myBackRectangles[a].parentPage.name);

               

              instead of index.

               

              Ignore it if I am missing something.

              • 4. Re: Length of Rectangles on a page in InDesign CS5 js
                [Jongware] Most Valuable Participant

                I can spot one possible cause of bad behavior: the list myBackRectangles should be processed from last to first. If you remove a page containing one of these rectangles, the order will get messed up

                So I thought this would do the trick

                 

                for (var a =myBackRectangles.length-1; a>=0; a--)
                 ...
                
                

                 

                .. but .. it does not.

                 

                Some further head-scratching later I changed the alert to this

                 

                alert (myBackRectangles[a].parent.index+"; page "+myBackRectangles[a].parent.name);
                
                

                 

                where the 'name' displays the actual page number as seen in InDesign. Confusion! For some reason the page index of all pages is '0'!

                I think this has to do with spreads. Internally, each page is 'on a spread' (even when it's only one single page), and therefore it's the first page on that spread (with index 0). The spread itself has another index, and that leads to yet another problem! In a facing page document, the spread index 1 contains pages 2 and 3, spread index 2 contains pages 4 and 5, and so on.

                 

                So you cannot use 'index' to check for a specific page in a document with facing pages. With no facing pages, you can use parentPage.parent.index -- which gives you the index of its spread.

                 

                If you have a facing pages document ... I don't know how to do this! <- Scratch This!

                 

                Hip hip hooray! I thought I had seen it somewhere before.

                Don't use 'index' for a page because that doesn't work. Use documentOffset instead! ("The sequential number of the page within the document.", says the Help.)

                 

                I cannot test for CS5, but this ought to work slightly better:

                 

                myBackRectangles = myDoc.layers.item ("Layer 1").rectangles;
                    
                    for (var a = myBackRectangles.length-1; a>=0; a--){
                          alert (myBackRectangles[a].parentPage.documentOffset);
                        if (myBackRectangles[a].parentPage.documentOffset == 1){
                            var myArtBack = myBackRectangles[a].graphics[0];
                            if (myArtBack.isValid == false){
                                myDoc.pages[1].remove();
                            }
                        }
                    }

                • 5. Re: Length of Rectangles on a page in InDesign CS5 js
                  Yuliaart Level 1

                  This is it, it works.

                   

                  Thank you very much.

                  Yulia