Skip to main content
Known Participant
February 11, 2011
Answered

Help: need a script to merge all layers, even if there is only one layer

  • February 11, 2011
  • 3 replies
  • 15156 views

I am trying to build an action that includes "merge all layers" or "merge visible layers". However when I record that throught the actions tool (usung the layers panel flyout dialog), if a file being batched doesn't have more than two layers or has one layer that is flat, I get an error and the actions stops. I wanted to add a step in the action to run a script that can do one of the two layer merges, and if there is only one layer will just continue without erroroing out.

Could anyone suggest some code snippets or point me the obvious answer?

Thanks so much!

- Not even a script noob yet. Please be kind.

This topic has been closed for replies.
Correct answer Paul Riggott

This should do it...


try{
    activeDocument.mergeVisibleLayers();
    }catch(e){}

3 replies

Known Participant
December 31, 2016

Ok well it's been 5 years since I asked this question, forgot the answer, looked it up in 2016, and found out I asked already in 2011. Thanks again everyone!

JJMack
Community Expert
Community Expert
February 15, 2011

Its aslo easy to do in an action.  Record the following

Shift+Ctrl+Alt+N (Add New Empty Layer without a prompt)

Shift+Ctrl+Alt+E (Stamp Visible Layers)

Shift+Ctrl+]         (move current layer to the front top of stack)

You need to record the first step because sometins stamp visible layers does nothing it depends what the current Photoshop target is.  When you add a new empty layer it will be added above the current Layer and become the Photoshop target and be a visible layer. You have no idea where in the stack this layer has been added.  When the curren Photoshop Target is a visble empty layer stamp visible layer will place a merge of all visible layers into that empty layer and it will still be the Photoshop Target.  To record the third step you may have to first stop recording the action add some layers and target sone layer below the top of the stack that can be moved up not the background layer. Then press the record button and use "Shift+Ctrl+]" if the current layer is the top of the stack using "Shift+Ctrl+]" will not be recorded.  I often start actions like this for doing things like sharpening.  I convert the layer to a smart object load a edge mask alpha channel as a selection then use the smart sharpen filter as a smart filter the selection get added as a filter mask. .  I then change the layers blending mode to luminosity to protect the colors and adjust the blend if gray sliders to protect the shadows and highligts more on less creates a sharpeng adjustment layer.  Which can be readjusted and adjusted via its opacity adjustment slider and you can even add a layer mask to paint away over sharpened areas.

JJMack
Paul Riggott
Paul RiggottCorrect answer
Inspiring
February 11, 2011

This should do it...


try{
    activeDocument.mergeVisibleLayers();
    }catch(e){}

Inspiring
August 8, 2014

Trying to get your code to work in extendscript for CS5,but not having any joy. Is  there an equivalent for this?

d1g1talphyre
Inspiring
August 8, 2014

Make sure extendscript is targeting photoshop, or add this to the top of your co: #target photoshop

you could also try to use this instead of merge visable: activeDocument.flatten()   that should do the same as merging visable.