Merge down every other layer in the layer stack?

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

OldSilverHalide
Posts: 1
Joined: Fri Feb 08, 2019 5:29 pm

Merge down every other layer in the layer stack?

Post by OldSilverHalide »

PS-Scripts,

I'm a newbie, and this is my first time posting to ps-scripts.

First, let me describe what I'd like to do. Then, I'll show you what I've come up with so far.

The task: For a Photoshop document containing numerous layers (no groups) above a locked background, I'd like to merge every other layer down ... not including the Background. For a document containing an even number of layers (not including the background), the process should halt at the locked Background layer. For a document containing an odd number of layers (not including the background), the process should halt at the layer above the Background, since it would fail to merge to the locked Background.

So, if the document had Layer 5, Layer 4, Layer 3, Layer 2, Layer 1, Background, the final result would be: Layer 5+4 Merged, Layer 3+2 Merged, Layer 1, Background. If the document had Layer 6, Layer 5, Layer 4, Layer 3, Layer 2, Layer 1, Background, the final result would be: Layer 6+5 Merged, Layer 4+3 Merged, Layer 2+1 Merged, Background.

The process I'd like to loop until the bottom-of-stack limit is essentially:

app.activeDocument.activeLayer.merge();

var doc = app.activeDocument;
var currentLayer = doc.activeLayer;


for(i=0; i < doc.layers.length; )
{
if(doc.layers==currentLayer)
{
a=i;
//alert(a);
i = doc.layers.length;
}
else{ i++; }
}


try
{
var nextLayer = doc.layers[a+1];
var check = nextLayer.visible;
}
catch(e)
{
var nextLayer = doc.layers[0];
var check = nextLayer.visible;
}

doc.activeLayer = nextLayer;
if (check == false)
doc.activeLayer.visible = false;



I admit that I'm a newbie to scripting; I found (and merged) the above code from web search results. Can somebody tell me how to loop this on an even-or-odd-numbered layer stack, ignoring the background? Is that the best way to go about this?

Thanks!
OldSilverHalide
User avatar
Jaroslav Bereza
Posts: 38
Joined: Tue Dec 27, 2016 7:22 pm

Re: Merge down every other layer in the layer stack?

Post by Jaroslav Bereza »

I think my Plugin could help you. But you still need to select every other layer manually. On the other side, it could help you even with different problems.
https://pepperbox.bereza.cz/
AnilTejwani
Posts: 20
Joined: Wed Feb 22, 2017 5:37 pm
Location: Paraguay

Re: Merge down every other layer in the layer stack?

Post by AnilTejwani »

Try this script,
// Script by Anil Tejwani | 19/Feb/2019
var ad = app.activeDocument;
var l = ad.artLayers.length; // initial number of layers in the current doc at the start
var tl = Math.floor(l/2);
if(l%2 === 0){tl = tl-1;}
for(var i = 0; i < tl; i++){ ad.activeLayer = ad.artLayers; ad.activeLayer.merge(); }


Let me know how it goes.

Cheers