9 Replies Latest reply on Dec 12, 2016 2:23 AM by OutlineG

    group all items on page in a document

    OutlineG Level 1

      Hi,

       

      I'm very new at this -

      I have a script for

       

      //group all on active spread

       

      var doc = app.activeDocument;

      var myItems = app.activeWindow.activeSpread.pageItems;

      app.activeWindow.activeSpread.groups.add(myItems);

      app.select(myItems);

      app.select(app.activeWindow.activePage.allPageItems);

       

      but I can't get it to work for the whole document.

       

      I tried things like

       

      var doc = app.activeDocument;

      var myItems = app.activeDocument.pageItems;

      app.activeDocument.groups.add(myItems);

       

      but I can't figure it out

      Thanks for any help

      Vanessa

        • 1. Re: group all items on page in a document
          tpk1982 Level 4

          use app.activeDocument.pageItems instead of app.activeWindow.activeSpread.pageItems

          • 2. Re: group all items on page in a document
            OutlineG Level 1

            //group all in document

            var doc = app.activeDocument;

            var myItems = app.activeDocument.pageItems;

            app.activeDocument.pageItems.groups.add(myItems);

            app.select(myItems);

             

            that gives an error...?

             

            the groups.add is wrong I think, but I don't know what the correct code is. I've been trying to open the chm file from JongWare (think that has all indd coding names?) but haven't succeeded yet...

            • 3. Re: group all items on page in a document
              tpk1982 Level 4

              try this

               

              var doc = app.documents[0];  
              var myItems = app.activeWindow.activeSpread.pageItems.everyItem();  
              myItems.locked = false;  
              var myGroup = doc.groups.add(myItems);  
              app.select(myGroup);
              
              • 4. Re: group all items on page in a document
                Peter Kahrel Adobe Community Professional & MVP

                What exactly are you trying to achieve? When you say "but I can't get it to work for the whole document", you should realise that the items you want to group must be on the same spread. So if you want to work on the document, you probably want to group items per spread. Correct? If yes, use this:

                 

                spreads = app.documents[0].spreads.everyItem().getElements();
                for (i = 0; i < spreads.length; i++) {
                  if (spreads[i].pageItems.length > 1) {
                    spreads[i].pageItems.everyItem().locked = false;
                    spreads[i].groups.add (spreads[i].pageItems);
                  }
                }
                

                 

                Peter

                • 5. Re: group all items on page in a document
                  Laubender Adobe Community Professional & MVP

                  OutlineG wrote:

                  …I've been trying to open the chm file from JongWare (think that has all indd coding names?) but haven't succeeded yet...

                  Hi Vanessa,

                  when on Mac OSX—don't know if you are—you can open Jongware's chm files with the free app iChm for Mac.

                  I'm not sure what is required for Windows, but it seems that there is an add-on for Firefox browser, that can read chm files.

                   

                  Depending on your version of InDesign you could also browse online for InDesign's DOM documentation.

                   

                  Compiled by Jongware:

                  Adobe InDesign CS5 (7.0) Object Model JS: Table of Contents

                  Adobe InDesign CS5.5 (7.5) Object Model JS: Table of Contents

                  Adobe InDesign CS6 (8.0) Object Model JS: Table of Contents

                   

                  Compiled by Gregor Fellenz:

                  InDesign ExtendScript API (8.0)

                  InDesign ExtendScript API (11.0)

                  InDesign ExtendScript API (12.0)

                   

                   

                  Regards,
                  Uwe

                  • 6. Re: group all items on page in a document
                    OutlineG Level 1

                     

                     

                    var doc = app.activeDocument;

                    app.activeDocument.groups.everyItem().ungroup();

                    does ungroup all spreads in a doc but the opposite does not work?

                    • 7. Re: group all items on page in a document
                      Laubender Adobe Community Professional & MVP

                      Hi,

                      the "why" is the same as working within the UI.

                      everyItem().ungroup() is not the opposite of everyItem().group().


                      With document.groups.everyItem().ungroup() every single group in the document that is not nested is addressed and will be ungrouped. You are addressing not a single group.

                       

                      Whereas document.groups.add() is always trying to add one single group.

                       

                      Further:

                      You are doing: document.groups.add()

                      So you are working with the document.

                       

                      You do not tell InDesign on what spread or page the group should be added. So InDesign tries to add the group on the first spread. And that will fail, if not all page items of the document do not exist on the first spread.

                       

                      It will work if all page items you want to group are on the first spread.

                       

                      Regards,
                      Uwe

                      • 8. Re: group all items on page in a document
                        Laubender Adobe Community Professional & MVP

                        A document is organized in master spreads and spreads.
                        With document.spreads you are addressing all spreads that are not masters.

                         

                        If you want to loop through only a subset of the spreads you have to start with the appropriate index number and the approriate length of the loop.

                         

                        Example where you start with the third spread and you end with the fourth spread, that means doing a loop through two spreads:

                         

                        var spreads = app.documents[0].spreads;
                        
                        for(var n=2;n<4;n++)
                        {
                            // Doing something with the third and the fourth spread:
                            // Where n can be 2 or 3. The loop will stop if 3 is reached.
                            // n is used as value for the spreads index number.
                        
                            var currentSpreadLooped = spreads[n];
                        
                            // Alternatively this could be written as:
                            var currentSpreadLooped = spreads.item(n);
                        
                            // because variable spreads is holding a collection of spreads.
                        
                            // If variable spreads would hold an Array type of items with:
                            // var spreads = app.documents[0].spreads.everyItem().getElements();
                        
                            // you have to use the first alternative for denoting a single spread in the loop:
                            // spreads[n] and not spreads.item(n) .
                        
                            // ** WARNING **
                            // The code above inside this loop will also work with documents with only a SINGLE spread!
                            // InDesign is not looking, if the a denoted spread exists before you do not like to do something with the spread.
                            // We can say: In this case the current spread stored in variable currentSpreadLooped "is not resolved"!
                            
                            // Doing something with a spread that is not existing in the document will throw an error!
                            // For example: alert(spreads[n].pages.length) will throw:
                            // "Object is invalid"
                            
                            // You could "resolve" the denoted spread by using:
                            var currentSpreadLooped = spreads[n].getElements()[0];
                            // or:
                            var currentSpreadLooped = spreads.item(n).getElements()[0];
                            
                            // OR YOU DECIDE TO WORK WITH AN ARRAY TYPE FROM THE START AND RESOLVE THE OBJECTS THAT SHOULD BE STORED:
                            // var spreads = app.documents[0].spreads.everyItem().getElements();
                            
                            $.writeln(n);
                        
                        };
                        

                         

                        Regards,
                        Uwe

                        • 9. Re: group all items on page in a document
                          OutlineG Level 1

                          I see why the group cannot be set up the same as the ungroup script.

                           

                          Thanks for all your help!! I still got a LOT to learn