16 Replies Latest reply: Aug 16, 2012 12:44 PM by NateO12 RSS

    group items with the same script_label

    JulienFR Community Member

      hi,

       

      I search to group items in document with the same script_label.

      So in my document there are many itmes with the same script_label. (just a part of the label) and i want to group there items with the same.

      it's begin to "kkk22_33_xxxx" and after there different. so it's possible. i try in javascript but there are problem to select it and put diffrent label in the list to use it before to group items (text and pictures).

      i do after a script to cut and paste an other document and there stay group...

      thanks indesigners !!!!

        • 1. Re: group items with the same script_label
          JulienFR Community Member

          i working to list all script labels and ID in a array. after comparaison i will group ID with the same part of script labels. i prefer the security of ID as Script labels directly.

          • 2. Re: group items with the same script_label
            Marc Autret MeganK

            Hi Julien,

             

            Preliminary notice: since a group cannot extend over a spread you have to build your groups spread by spread.

             

            Then, this kind of code should basically do the job:

             

            // Group labelled items spread by spread
            // =====================================
            var LABEL_PREFIX = "kkk22_33_";
            
            var doc = app.documents.length && app.activeDocument,
                spreads = doc && doc.spreads,
                s = spreads ? spreads.length : 0,
                items, a=[], t, i, z=0;
            
            while( s-- )
                {
                items = spreads[s].pageItems.everyItem().getElements();
                i = items.length;
                z = 0;
                while( i-- )
                    {
                    if( 0 != (t=items[i]).label.indexOf(LABEL_PREFIX) ) continue;
                    a[z++] = t;
                    }
                z && spreads[s].groups.add(a);
                a.length = 0;
                }
            

             

             

            @+

            Marc

            • 3. Re: group items with the same script_label
              NateO12 Community Member

              Marc,

              Can you explain a bit about the while loops in your script? Particularly this part:

               

               z = 0;   
               while( i-- )
                      {
                      if( 0 != (t=items[i]).label.indexOf(LABEL_PREFIX) ) continue;
                      a[z++] = t;
                      }
                  z && spreads[s].groups.add(a);
                  a.length = 0;
              

               

              I'm trying to do some grouping of objects as well, but I have two pages, and three different script labels. I'm trying to modify this code but can't really figure out what that while loop is doing. Thanks.

               

              Nate

              • 4. Re: group items with the same script_label
                Marc Autret MeganK

                Hi Nate,

                 

                The purpose of this loop is to store into an array (a) the pageitems (t=items[i]) whose the label property meets a certain condition.

                 

                1) Before the loop begins, a is empty and z (=0) represents its length. Thus, each time we need to store a new object t into a, we just have to do:

                 

                a[z++] = t; // z is post-incremented so this means: a[z]=t; z++;

                 

                2) About while( i-- ) it's just a convenient way to loop from N-1 to zero, where N e.g. represents the length of a source array. In my snippet, i is initialized to items.length, where items is an array of pageitems found in a spread.

                 

                i = N; while( i-- ){…}

                 

                    is equivalent to:

                 

                for( i = N-1 ; i >= 0 ; i-- ){…}

                 

                3) Finally, the code:

                 

                if( 0 != (t=items[i]).label.indexOf(LABEL_PREFIX) ) continue;

                 

                means: “if the label of t = items[i] does not meet my condition, then skip this item.”

                 

                The condition here is that the label starts with the string LABEL_PREFIX, so I use the method String.indexOf(needle) which returns 0 if, and only if, needle is found at position 0.

                 

                @+

                Marc

                • 5. Re: group items with the same script_label
                  Trevorׅ Community Member

                  Hi Marc,

                   

                  That was a very clear explanation.

                   

                  I thought to add an explanation of the way you are using && as resourceful non standard shortcut.

                   

                   

                  var doc = app.documents.length && app.activeDocument,
                      spreads = doc && doc.spreads,
                      s = spreads ? spreads.length : 0,
                  

                   

                  doc is going to be assign to app.activeDocument only if the active document has a length to it, i.e. it has pages otherwise it's going to be made 0.

                   

                  spreads is going to be made doc.spreads if the document has a length to it and therefore doc is not 0, otherwise if doc is 0 then speads is going to be made 0.

                   

                  s is made spreads.length  if the the document has a length to it, otherwise it's going to be made 0 and the script would end here without giving any nasty error messages .

                   

                  The line

                   

                   z && spreads[s].groups.add(a);

                   

                  Is saying if the labels were found and therefore z is a positive integer then add to a to the group.

                  The longer (albeit in this case just slightly) more commonly used for would be.

                  if (z > 0) spreads[s].groups.add(a);
                  

                   

                  Very coding!

                   

                  Please let me know if I explained something incorrectly.

                   

                  Regards

                   

                  Trevor

                  • 6. Re: group items with the same script_label
                    NateO12 Community Member

                    Thanks Marc and Trevor. Those explanations helped out immensely. However I think I’ve hit a bit of a snag. This code seems to look for just one script label across all spreads. What if I wanted to have it look for more than one script label, say, three? I’ve been playing with the code, and have figured that an array of label names might be the best way to do this. However, it seems that the while loop looks for one label on spread one, then possibly the next label, but at that point it’s on page two. How can I get it to group items named the same before going to the next page? It seems like I need to switch the two while statements, and have it process and group the page one items before going to page two to process those items, but I’m having no luck. Any ideas? Thanks.

                     

                    Nate

                    • 7. Re: group items with the same script_label
                      Trevorׅ Community Member

                      Hi Nate,

                       

                      This is how I would do it.

                       

                      Not tested so I may well (probably) have made some major goof .

                       

                      Marc, PLEASE, PLEASE let me know if you would have done this differently.

                       

                      Julien, Marc wrote you a nice little script here so maybe you could show him a little gratitude and say thank you and mark hi answer as correct.

                       

                      Regards

                       

                      Trevor

                       

                      Take note of the annotations

                       

                      // Group labelled items spread by spread
                      // =====================================
                      var LABEL_PREFIX = ["kkk22_33_","Fred_55","George"]; // if you don't like Fred and George you can change to Marc & Trevor ;-)
                      
                      var doc = app.documents.length && app.activeDocument,
                          spreads = doc && doc.spreads,
                          s = spreads ? spreads.length : 0,
                          items, a=[], t, i, z=0, y=LABEL_PREFIX.length; // added y here
                      
                      while( y-- ) // added this loop
                      a[y]=[]; // to allow values to be assigned to the sub arrays. LINE A
                      {
                          while( s-- )
                              {
                              items = spreads[s].pageItems.everyItem().getElements();
                              i = items.length;
                              z = 0;
                              while( i-- )
                                  {
                                  if( 0 != (t=items[i]).label.indexOf(LABEL_PREFIX[y]) ) continue; // add the [y] no matter what.
                                  a[y][z++] = t; // see the y LINE B
                                  }
                              z && spreads[s].groups.add(a[y]); // see the y. LINE C
                              // If you want all 3 lables in the same group then erase LINE A,  and Leave LINE B and LINE C as the were without the [y].
                              a.length = 0;
                              }
                      } // see the  }
                      
                      • 8. Re: group items with the same script_label
                        NateO12 Community Member

                        Hi Trevor,

                        Thanks for the assistance. I'm relieved to see that I was on the right track, but I would have never thought to use a sub array! However, I couldn't get it to work as is; nothing was being grouped. So I moved 'Line A' two lines down, and it will group the second set of items on page 1, and then the items on page 2, but the first set of items on page 1 is ignored. It also ends with an 'Object is invalid' error.

                         

                        Perhaps I should start a new thread with this, instead of hijacking this one?

                         

                        Nate

                        • 9. Re: group items with the same script_label
                          Trevorׅ Community Member

                          ~ Trevor ~ wrote:

                           

                          Not tested so I may well (probably) have made some major goof .

                          Well I did say probably!

                           

                          You can of course put Marc's code into a function like this

                           

                           

                          // Group labelled items spread by spread 
                          // Functionized by Trevor
                          // =====================================
                          MarcsGroupLabelledItemsSpreadBySpreadScript("kkk22_33_");
                          MarcsGroupLabelledItemsSpreadBySpreadScript("Nate");
                          MarcsGroupLabelledItemsSpreadBySpreadScript("Fred");
                          
                          function MarcsGroupLabelledItemsSpreadBySpreadScript(LABEL_PREFIX)
                           {
                          var doc = app.documents.length && app.activeDocument,
                              spreads = doc && doc.spreads,
                              s = spreads ? spreads.length : 0,
                              items, a=[], t, i, z=0;
                          
                          while( s-- )
                              {
                              items = spreads[s].pageItems.everyItem().getElements();
                              i = items.length;
                              z = 0;
                              while( i-- )
                                  {
                                  if( 0 != (t=items[i]).label.indexOf(LABEL_PREFIX) ) continue;
                                  a[z++] = t;
                                  }
                              z && spreads[s].groups.add(a);
                              a.length = 0;
                              }
                          }
                          

                           

                          This will work, but is not realy the way to go about things in bigger examples.

                           

                          This will make 3 seperate groups if you want to make one group containing all three lables then it won't work.

                           

                          I saw a few of major goofs (it was late and I was very tired) that I made in the origional script I posted but the basic idea of using a sub array is I think correct

                           

                          Trevor

                          • 10. Re: group items with the same script_label
                            Trevorׅ Community Member

                            Hi Nate

                            // Group labelled items spread by spread
                            // =====================================
                            LABEL_PREFIX = ["kkk22_33_","Fred_55","George"]; // if you don't like Fred and George you can change to Marc & Trevor ;-)
                            a=[]; 
                            var y=app.documents.length && app.activeDocument && app.activeDocument.spreads && LABEL_PREFIX.length;
                            while (y--) 
                            {
                             a[y]=[];
                            var doc = app.documents.length && app.activeDocument,
                                   spreads = doc && doc.spreads,
                                    s = spreads ? spreads.length : 0, z, i, items; 
                                while ( s-- )
                                    {
                                    items = spreads[s].pageItems.everyItem().getElements();
                                    i = items.length;
                                    z = 0;
                                    while( i-- )
                                        {
                                        if( 0 != (t=items[i]).label.indexOf(LABEL_PREFIX[y]) ) continue; 
                                        a[y][z++] = t; 
                                        }
                                    z && spreads[s].groups.add(a[y]); 
                                    }
                            }
                            a.length = 0;
                            

                             

                            I tested this works for me

                            • 11. Re: group items with the same script_label
                              JulienFR Community Member

                              Hi,

                              Good job !!

                              Its perfect if i do préfixs BEFORE to build my document in workfkow.

                              My préfixs are much complicated. And i thank attribut it with Xtags but it's not possible.

                              Since your first post i work enough suffix with several tests in frame.

                              It's laborious

                              There are différent roots or suffix. . 

                              Ordering, and it's possible to group i

                               

                              n different level.

                              After to be move groups... Of course

                              exemple :

                              (1) pict : "aaaaa_bbbb_number"

                              (2)Text (several) : "xxxxdifferent_bbbb_number"

                              (3)Other pics (it's my blame) : "yyyyyydiferent_bbbbb" only

                              i group (1) and (2) all frames because There're différent "bbbbb" in document.

                              i thank pass one to one and keep ID with table and working with. Ordering and group with ID in document.

                              There are pict frames and text frames with différent ID.

                              So i working to it but very slowly

                               

                               

                               

                              Le 15 août 2012 à 18:44, ~ Trevor ~ <forums@adobe.com> a écrit :

                               

                              Re: group items with the same script_label

                              created by ~ Trevor ~ in InDesign Scripting - View the full discussion

                              Hi Nate

                               

                              // Group labelled items spread by spread

                              // =====================================

                              LABEL_PREFIX = ["kkk22_33_","Fred_55","George"]; // if you don't like Fred and George you can change to Marc & Trevor

                              a=[];

                              var y=app.documents.length && app.activeDocument && app.activeDocument.spreads && LABEL_PREFIX.length;

                              while (y--)

                              {

                              a[y]=[];

                              var doc = app.documents.length && app.activeDocument,

                                     spreads = doc && doc.spreads,

                                      s = spreads ? spreads.length : 0, z, i, items;

                                  while ( s-- )

                                      {

                                      items = spreads[s].pageItems.everyItem().getElements();

                                      i = items.length;

                                      z = 0;

                                      while( i-- )

                                          {

                                          if( 0 != (t=items[i]).label.indexOf(LABEL_PREFIX[y]) ) continue;

                                          a[y][z++] = t;

                                          }

                                      z && spreads[s].groups.add(a[y]);

                                      }

                              }

                              a.length = 0;

                               

                               

                              I tested this works for me

                               

                              Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page: http://forums.adobe.com/message/4623519#4623519

                              To unsubscribe from this thread, please visit the message page at http://forums.adobe.com/message/4623519#4623519. In the Actions box on the right, click the Stop Email Notifications link.

                              Start a new discussion in InDesign Scripting by email or at Adobe Forums

                              For more information about maintaining your forum email notifications please go to http://forums.adobe.com/message/2936746#2936746.

                               

                              • 12. Re: group items with the same script_label
                                JulienFR Community Member

                                Regards

                                 

                                 

                                 

                                Le 15 août 2012 à 18:44, ~ Trevor ~ <forums@adobe.com> a écrit :

                                 

                                Re: group items with the same script_label

                                created by ~ Trevor ~ in InDesign Scripting - View the full discussion

                                Hi Nate

                                 

                                // Group labelled items spread by spread

                                // =====================================

                                LABEL_PREFIX = ["kkk22_33_","Fred_55","George"]; // if you don't like Fred and George you can change to Marc & Trevor

                                a=[];

                                var y=app.documents.length && app.activeDocument && app.activeDocument.spreads && LABEL_PREFIX.length;

                                while (y--)

                                {

                                a[y]=[];

                                var doc = app.documents.length && app.activeDocument,

                                       spreads = doc && doc.spreads,

                                        s = spreads ? spreads.length : 0, z, i, items;

                                    while ( s-- )

                                        {

                                        items = spreads[s].pageItems.everyItem().getElements();

                                        i = items.length;

                                        z = 0;

                                        while( i-- )

                                            {

                                            if( 0 != (t=items[i]).label.indexOf(LABEL_PREFIX[y]) ) continue;

                                            a[y][z++] = t;

                                            }

                                        z && spreads[s].groups.add(a[y]);

                                        }

                                }

                                a.length = 0;

                                 

                                 

                                I tested this works for me

                                 

                                Replies to this message go to everyone subscribed to this thread, not directly to the person who posted the message. To post a reply, either reply to this email or visit the message page: http://forums.adobe.com/message/4623519#4623519

                                To unsubscribe from this thread, please visit the message page at http://forums.adobe.com/message/4623519#4623519. In the Actions box on the right, click the Stop Email Notifications link.

                                Start a new discussion in InDesign Scripting by email or at Adobe Forums

                                For more information about maintaining your forum email notifications please go to http://forums.adobe.com/message/2936746#2936746.

                                 

                                • 13. Re: group items with the same script_label
                                  Trevorׅ Community Member

                                  Hi Julien

                                   

                                  Nice to see you are still alive.

                                   

                                  I'm sorry I couldn't understand your English.

                                   

                                  Please can you write that again more clearly in a better English.

                                   

                                  Trevor

                                  • 14. Re: group items with the same script_label
                                    JulienFR Community Member

                                    yes I'm still here ... but I have not always time (2 little children and

                                    job)

                                    Good job for your script! Excellent ...

                                    This is a good script if the labels exist before building the document in a

                                    workflow.

                                    My prefixes are more complicated. I thought with the changed Xtags but it

                                    does not work well in the stream after.

                                    Since the first post I work with labels without the full test on the frames

                                    ... it's tedious ..

                                    My problematic on different prefixs on the labels are:

                                    eg (I have already called in the blocks) :

                                    (1) pict: "aaaaa_bbbb_number"

                                    (2) Text (several): "xxxxdifferent_bbbb_number"

                                    (3) Other peaks (it's my blame): "yyyyyydiferent_bbbbb" only

                                    I groups (1) and (2) but there are different "bbbbbb" in the document.

                                    I thought went through a comparison script by retrieving all the labels and

                                    IDs each frames of the then grouped and moved in their rallies...

                                    I have grid to move the groups in template with x,y position

                                     

                                    (google tradution)

                                     

                                    2012/8/16 ~ Trevor ~ <forums@adobe.com>

                                     

                                    **

                                       Re: group items with the same script_label  created by ~ Trevor ~<http://forums.adobe.com/people/%7ETrevor%7E>in

                                    InDesign Scripting - View the full discussion<http://forums.adobe.com/message/4624711#4624711

                                     

                                    • 15. Re: group items with the same script_label
                                      Trevorׅ Community Member

                                      Sorry Julien

                                       

                                      Still not clear on what to need.

                                      Maybe try writing the problem in French, Marc's pretty fluent in French and there are quite a lot of French speakers that visit the Forum

                                       

                                      à bientôt

                                       

                                      Trevor

                                      • 16. Re: group items with the same script_label
                                        NateO12 Community Member

                                        Thanks a million Trevor, the solution you posted for me works perfectly! And the explanations are starting to help this all make sense now. I only wish I would have started a new thread so I could give you and Marc the recognition!!

                                         

                                        Nate