Copy link to clipboard
Copied
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
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
...
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
@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.
Copy link to clipboard
Copied
HI,
Thanks for this, I did not know that, and that will teach me to rely on the docs only 😄
Copy link to clipboard
Copied
Hello BaralaeDC,
and thanks for pointing this out. I based my script on this:
https://indisnip.wordpress.com/2010/07/24/group-allselected-items-on-page/
and he uses a method like so :
app.activeWindow.activePage.groups.add(myObj);
However I modified my script because I noticed that I was feeding an item as the first argument of the add-method and it should have been an array of items. Here is the new version :
var docA = app.activeDocument,
aPage = docA.pages.lastItem(),
group_it = function() {
var layerGroup = [],
layerItems = [];
for(var i = 0; i<=docA.layers.length-1; i++){
for(var j = 0; j<=aPage.pageItems.length-1; j++){
if (aPage.pageItems[j].itemLayer.id == docA.layers[i].id) {
alert(docA.layers[i].id + " - " + aPage.pageItems[j].id);
layerItems.push(aPage.pageItems[j]);
}
}
layerGroup[i] = aPage.groups.add(layerItems,docA.layers[i],LocationOptions.AT_END);
layerGroup[i].name = "layer" + i + " group";
layerItems.length = 0;
}
};
group_it();
... which still isn't working I'm afraid.
The error seems to be the same, and happens at the line:
Copy link to clipboard
Copied
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();
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
I just tested again with the layers in a different order and I noticed 2 things.
1.) the first run of the inner loop is ok. it creates a group & shows all the different items in the alert.
2.) it throws an error after the second run of the inner loop at line 13 which is:
Copy link to clipboard
Copied
Well... I did some more investigations and it seems that the script works perfectly 🙂
Except that it stops and throws an error when it encounters a layer with only one object... XD.
So here is the final script:
var docA = app.activeDocument,
aPage = docA.pages.lastItem(),
group_it = function() {
var layerGroup;
for(var i = 0; i<=docA.layers.length-1; i++){
layerGroup = [];
var layerItems = [];
for(var j = 0; j<=aPage.pageItems.length-1; j++){
if (aPage.pageItems[j].itemLayer.id == docA.layers[i].id) layerItems.push(aPage.pageItems[j])
}
if (layerItems.length > 1) {
layerGroup[i] = aPage.groups.add(layerItems,docA.layers[i],LocationOptions.AT_END);
layerGroup[i].name = "layer" + i + " group";
layerItems = [];
}
}
};
group_it();
Thanks to both of you for your help 🙂
Much appreciated!
Mike
Copy link to clipboard
Copied
Hi truf0,
just note, that you could still add a group with one single item to the page if you must because perhaps further code expects only group items on the page. How do you do this? Just add a rectangle to the spread and group that together with the other item. Then remove the added rectangle from the group. The group lives on with just one item. Groups with zero items are not possible…
Regards,
Uwe Laubender
( ACP )
Copy link to clipboard
Copied
Duly noted.
Thanks man 🙂