Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to merge every layerset as a layer in the document?

New Here ,
Jun 12, 2018 Jun 12, 2018

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?

TOPICS
Actions and scripting
933
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Explorer , Jun 13, 2018 Jun 13, 2018

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

...
Translate
Adobe
Explorer ,
Jun 13, 2018 Jun 13, 2018

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();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 13, 2018 Jun 13, 2018
LATEST

Right, you are so clever!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines