Copy link to clipboard
Copied
I want to merge all layerset in the document
var layerSets = app.activeDocument.layerSets;
for(var i = 0; i < layerSets.length; i++)
{
layerSets.merge();
}
But some of the layerset merged and some of them didn't. What's the problem and is there any other way to do it?
The problem is that layerSets.length is changing on each iteration of the for loop (because a layerSet dissapears with each call of merge()!
For example, if you have 3 layerSets to begin:
1st iteration: i = 0, layerSets.length = 3, layerset_0 is merged
2nd iteration: i = 1, layerSets.length = 2, layerset_1 is merged
3rd iteration: i = 2, layerSets.length = 1, i > layerSets.length, loop terminated
One solution is to iterate in reverse starting with the final element. This way, as layerSets.length shri
...Copy link to clipboard
Copied
The problem is that layerSets.length is changing on each iteration of the for loop (because a layerSet dissapears with each call of merge()!
For example, if you have 3 layerSets to begin:
1st iteration: i = 0, layerSets.length = 3, layerset_0 is merged
2nd iteration: i = 1, layerSets.length = 2, layerset_1 is merged
3rd iteration: i = 2, layerSets.length = 1, i > layerSets.length, loop terminated
One solution is to iterate in reverse starting with the final element. This way, as layerSets.length shrinks, it is okay, because you have already handled the associated layerSets.
See below for example:
var layerSets = app.activeDocument.layerSets;
for(var i = layerSets.length - 1; i >= 0; i--)
{
layerSets.merge();
}
Copy link to clipboard
Copied
Right, you are so clever!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now