Skip to main content
Participating Frequently
April 15, 2022
Answered

indesign-extendScript to group items by page and layer

  • April 15, 2022
  • 2 replies
  • 1385 views

Hello all,

I've been working on this small script :

var docA = app.activeDocument,
lastPage = docA.pages.lastItem(),
group_it = function() {
    var group0 = [];
    for(var i = 0; i<=docA.layers.length-1; i++){
        for(var j = 0; j<=lastPage.pageItems.length-1; j++){
            if (lastPage.pageItems[j].itemLayer.id == docA.layers[i].id) {
                alert(docA.layers[i].id + " - " + lastPage.pageItems[j].id);
                group0[i] = lastPage.groups.add(lastPage.pageItems[j],docA.layers[i],LocationOptions.AT_END);
                group0[i].name = "myGroup" + i;
            }
        }
    }
};
group_it();

 I tested it with a document with 3 layers, 2 pages and 4 items... But it always seems to give me an error at the line where the grouping occurs (group0[i] = lastPage...).

If I comment that line and the one following it, the alert box shows me the correct number of items-id.

I have to say, I'm rather puzzled by the whole thing... If some kind soul could shed some light on this problem, I would be most gratefull.

Mike

This topic has been closed for replies.
Correct answer brian_p_dts

A few issues with your code. You want to collect the layers page item into a temp array that you then pass to the groups.add() function for each layer. You were trying to create a group for each individual page item. Check out the below and let me know if you have questions: 

 

var docA = app.activeDocument;
var lastPage = docA.pages.lastItem();

group_it = function() {
    var group0;
    for(var i = 0; i<=docA.layers.length-1; i++){
        group0 = []; //reinit an empty array for each layer
        for(var j = 0; j<=lastPage.pageItems.length-1; j++){
            if (lastPage.pageItems[j].itemLayer.id == docA.layers[i].id) {
                group0.push(lastPage.pageItems[j]); //push the page item to your array
            }
        }
        //groups.add wants an array of page items, so that's what we give it
        //you can pass any number of properties at the end of the add function, including name
        //so... no need for a variable and separate property set
        lastPage.groups.add(group0,docA.layers[i],LocationOptions.AT_END,{name:"myGroup" + i});
    }
};
group_it();

 

 

2 replies

brian_p_dts
Community Expert
brian_p_dtsCommunity ExpertCorrect answer
Community Expert
April 15, 2022

A few issues with your code. You want to collect the layers page item into a temp array that you then pass to the groups.add() function for each layer. You were trying to create a group for each individual page item. Check out the below and let me know if you have questions: 

 

var docA = app.activeDocument;
var lastPage = docA.pages.lastItem();

group_it = function() {
    var group0;
    for(var i = 0; i<=docA.layers.length-1; i++){
        group0 = []; //reinit an empty array for each layer
        for(var j = 0; j<=lastPage.pageItems.length-1; j++){
            if (lastPage.pageItems[j].itemLayer.id == docA.layers[i].id) {
                group0.push(lastPage.pageItems[j]); //push the page item to your array
            }
        }
        //groups.add wants an array of page items, so that's what we give it
        //you can pass any number of properties at the end of the add function, including name
        //so... no need for a variable and separate property set
        lastPage.groups.add(group0,docA.layers[i],LocationOptions.AT_END,{name:"myGroup" + i});
    }
};
group_it();

 

 

truf0Author
Participating Frequently
April 15, 2022

Hey thanks for your answer Brian 🙂

I tried it but still no joy.

The error happens at :

lastPage.groups.add...

And its' funny because if you take a llok above at my answer to BarlaeDC's post I did more or less the same modifications that you suggest.

Yeah well, this one seems like a tough nut to crack.

I'll continue working on it but I wonder if I need to implement some sort of call back thingie in the inner loop. Because I'm not quite sure it traverses the whole layer of items before the add method is run.

BarlaeDC
Community Expert
Community Expert
April 15, 2022

Hi,

 

I am not sure exactly what you are trying to do but the groups property of a page is readonly, which means you would not be able to add to it this way.

 

If you can explain what you are trying to achieve maybe we can provide assistance as to how best to achieve this?

brian_p_dts
Community Expert
Community Expert
April 15, 2022

@BarlaeDC Most collections like Groups are read-only, but most (if not all) have a .add() method to create new single instances on the fly. 

BarlaeDC
Community Expert
Community Expert
April 15, 2022

HI,

 

Thanks for this, I did not know that, and that will teach me to rely on the docs only 😄