Skip navigation
JulienFR 13 posts
Nov 30, 2009
Currently Being Moderated

group items with the same script_label

Mar 27, 2012 1:10 PM

Tags: #script #javascript #indesign #indesign_cs5.5 #tables #scripting #paragraph_styles #automation

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 !!!!

 
Replies
  • Currently Being Moderated
    Apr 10, 2012 5:22 AM   in reply to JulienFR

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 10, 2012 7:33 AM   in reply to Marc Autret

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 11, 2012 4:21 AM   in reply to NateO12

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 11, 2012 10:53 PM   in reply to Marc Autret

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 14, 2012 12:24 PM   in reply to Trevorׅ

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 14, 2012 2:37 PM   in reply to NateO12

    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  }
    
     
    |
    Mark as:
  • Currently Being Moderated
    Aug 15, 2012 7:23 AM   in reply to Trevorׅ

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 15, 2012 9:08 AM   in reply to Trevorׅ

    ~ 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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 15, 2012 9:44 AM   in reply to Trevorׅ

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 15, 2012 4:56 PM   in reply to JulienFR

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 16, 2012 6:24 AM   in reply to JulienFR

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Aug 16, 2012 12:44 PM   in reply to Trevorׅ

    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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (1)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points