3 Replies Latest reply on Nov 16, 2010 4:48 AM by Peter Kahrel

    Finding the group from a page level

    pvsk123

      I have a requirement to detach a particular page from the master page so that i can edit the respective item.

       

      I have written the below script that look for the group inside masterPageItems and then detach the page.

       

      When i printed "currentPage.masterPageItems.length" in alert, it shown me 120 counts. The issue here is that it is looping through all the 120 items to get the group and then detaching it.  Is there a better way to find the group once i get the "currentPage" rather than looping through all the masterpageitems?  Please help if you have a solution for this.  The script takes a lot of time to execute and need to optimize it.  If anybody require i can send you the INDD sample file for testing .  Thanks in advance

       


           
                   var myDCPDocument = app.activeDocument;
                  
                   for (var pageIndex =0; pageIndex < myDCPDocument.pages.length; pageIndex++)
                   {
                          var currentPage = myDCPDocument.pages.item(pageIndex);
                           alert(currentPage.masterPageItems.length);
                           for (var masterPageItemIndex =0; masterPageItemIndex < currentPage.masterPageItems.length; masterPageItemIndex++)
                            {
                                  var currentPageItem = currentPage.masterPageItems[masterPageItemIndex];
                                     
                                  if (currentPageItem.constructor.name == "Group")
                                 {
                                       var groupPageItems = currentPageItem.pageItems;
                                         
                                       for (var pageItemIndex = 0; pageItemIndex < groupPageItems.length; pageItemIndex++)
                                       {
                                            var cntrl = groupPageItems[pageItemIndex];  
                            
                                             if (cntrl.label == "Markets")
                                             {
                                                  var table = cntrl.tables[0];
                                                  alert("Total No of Cells :" + tbl.cells.length );
                                                  currentPageItem.override(currentPage);
                                                  break;
                                              }
                                      }
                              }
                        }
                   }

        • 1. Re: Finding the group from a page level
          Peter Kahrel Adobe Community Professional & MVP

          Maybe you should approach it differently. Knowing that a table's parent is a text frame (in CS5), you can do this:

           

          var tables = app.activeDocument.stories.everyItem().tables.everyItem().getElements();

          for (i = 0; i < tables.length; i++)

             {

             if (tables[i].parent.label == "Markets")

                {

                // code

                }

              }

          1 person found this helpful
          • 2. Re: Finding the group from a page level
            pvsk123 Level 1

            Hi Peter,

             

            That was a good information.  I was able to get the Markets section of the document.  I modified the code as per your suggestion below.

             

                          for (var pageIndex =0; pageIndex < myDCPDocument.pages.length; pageIndex++)
                         {
                                var currentPage = myDCPDocument.pages.item(pageIndex);   

                                var currentPageItem = currentPage.masterPageItems[0];

                                var tables = myDCPDocument.stories.everyItem().tables.everyItem().getElements();
                               
                                for (i = 0; i < tables.length; i++)
                                {
                                        if (tables[i].parent.label == "Markets")
                                        {                                    
                                               var table = tables[i];
                                               alert("Total No of Cells :" + table.cells.length );
                                               currentPageItem.override(currentPage);
                                               break;
                                        }
                                 }
                             }

             

            But i am unable to override the pageItem.  Neither it doesn't throw any error nor i am unable to apply the swatch (code pasted below).  Do you think the above code is still having problem? 

             

            Apply swatch on to the detached Markets group

            ----------------------------------------------------------------------

                       
                         for (var pageIndex =0; pageIndex < myDCPDocument.pages.length; pageIndex++)
                         {
                                var currentApplyPage = myDCPDocument.pages.item(pageIndex);
                        
                                 for (var applyGroupIndex = 0; applyGroupIndex < currentApplyPage.groups.count(); applyGroupIndex++)
                                 {
                                       var applyPageItems =currentApplyPage.groups[applyGroupIndex].pageItems;

                                        
                                       for (var applyPageItemIndex = 0; applyPageItemIndex < applyPageItems.length; applyPageItemIndex++)
                                       {
                                                var tableControl = applyPageItems[applyPageItemIndex];    
                                                           
                                                if (tableControl.label == "Markets")
                                                {
                                                    var applyTable = tableControl.tables[0];
                                                    for (cellIndex = 0; cellIndex < applyTable.cells.length; cellIndex++)
                                                    {
                                                            var applyCellInfo = applyTable.cells[cellIndex];
                                                            if (applyCellInfo.contents == "MW-2" )
                                                            {                           
                                                                applyCellInfo.fillColor = slugNoFillSwatch;
                                                                break;
                                                                //alert('NoFill Swatch is applied for MW-2 market segment sucessfully');
                                                            }
                                                     }
                                                     break;
                                                }
                                           }
                                     }
                             }           

            • 3. Re: Finding the group from a page level
              Peter Kahrel Adobe Community Professional & MVP

              Don't cycle through the documents pages, but through the tables. If you're looking for something on a master spread it could be sped up a bit more, probably.

               

               

               

              var tables = app.activeDocument.stories.everyItem().tables.everyItem().getElements ();
              var frame;
              for (var i = 0; i < tables.length; i++)
                 {
                 frame = tables[i].parent;
                 if (frame.label == "Markets")
                    {
                    // CS4:
                    // frame.override (parent.parent);
                    // CS5:
                    frame.override (parent.parentPage);
                    }
                  }

               

               

              (or something close to this, I didn't try it; but you get the picture)

               

               

              Peter