Skip to main content
Participant
August 19, 2019
Answered

How To - Photoshop Script - Merge Two Layers

  • August 19, 2019
  • 1 reply
  • 2759 views

My Photoshop Canvas is 900X600.

The function below takes Layer X and makes Layer X copy.

It takes Layer X copy and while maintaining the ratio adjusts the height to 600px. var newdLayer

It takes Layer X and while maintaining the ratio adjusts the width to 900px and applies the Gaussian Blur. var blur.

I want to then merge Layer X copy and Layer X.

Apparently there is a merge() function, but for that you have to crate a layerset. Not really a pro at javascript.

How to merge the two layers?

(function (){

var docRef = activeDocument
var newdLayer = docRef.activeLayer.duplicate();

newdLayer
;

  
var startRulerUnits = app.preferences.rulerUnits; 
  app
.preferences.rulerUnits = Units.PIXELS; 
  
var bounds = newdLayer.bounds; 
  
var height = bounds[3].value - bounds[1].value;
  
var newSize = (100 / height) * 600; 
  newdLayer
.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);
  app
.preferences.rulerUnits = startRulerUnits; 

var blur = docRef.activeLayer;

  
var startRulerUnits = app.preferences.rulerUnits; 
  app
.preferences.rulerUnits = Units.PIXELS; 
  
var bounds = blur.bounds; 
  
var width = bounds[2].value - bounds[0].value;
  
var newSize = (100 / width) * 900; 
  blur
.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);
  app
.preferences.rulerUnits = startRulerUnits; 
  blur
.applyGaussianBlur(5)

blur
;


})();

This topic has been closed for replies.
Correct answer Stephen Marsh

Presuming that the upper Layer X Copy layer is active/targeted, the following line of code would merge Layer X Copy into the lower Layer X directly underneath in the layer stack:

app.activeDocument.activeLayer.merge();

(The equivalent of the Layer > Merge Down or CMD/CTRL + E command)

P.S. I tried your code, however, I am guessing that something was lost in the copy/paste or that the formatting has messed things up too much.

Perhaps something like this?

(function ()

{ var docRef = activeDocument

    var newdLayer = docRef.activeLayer.duplicate(); newdLayer;   

    var startRulerUnits = app.preferences.rulerUnits;   

    app.preferences.rulerUnits = Units.PIXELS;    

    var bounds = newdLayer.bounds;    

    var height = bounds[3].value - bounds[1].value;   

    var newSize = (100 / height) * 600;   

    newdLayer.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);  

    app.preferences.rulerUnits = startRulerUnits; 

    var blur = docRef.activeLayer;   

    var startRulerUnits = app.preferences.rulerUnits;   

    app.preferences.rulerUnits = Units.PIXELS;    

    var bounds = blur.bounds;    

    var width = bounds[2].value - bounds[0].value;   

    var newSize = (100 / width) * 900;   

    blur.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);  

    app.preferences.rulerUnits = startRulerUnits;   

    blur.applyGaussianBlur(5);

    }

)

();

//////////////////////////

select(false);

function select(makeVisible) {

  var c2t = function (s) {

  return app.charIDToTypeID(s);

  };

  var s2t = function (s) {

  return app.stringIDToTypeID(s);

  };

  var descriptor = new ActionDescriptor();

  var list = new ActionList();

  var reference = new ActionReference();

  reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "forwardEnum" ));

  descriptor.putReference( c2t( "null" ), reference );

  descriptor.putBoolean( s2t( "makeVisible" ), makeVisible );

  list.putInteger( 13 );

  descriptor.putList( s2t( "layerID" ), list );

  executeAction( s2t( "select" ), descriptor, DialogModes.NO );

}

//////////////////////////

app.activeDocument.activeLayer.merge();

1 reply

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
August 20, 2019

Presuming that the upper Layer X Copy layer is active/targeted, the following line of code would merge Layer X Copy into the lower Layer X directly underneath in the layer stack:

app.activeDocument.activeLayer.merge();

(The equivalent of the Layer > Merge Down or CMD/CTRL + E command)

P.S. I tried your code, however, I am guessing that something was lost in the copy/paste or that the formatting has messed things up too much.

Perhaps something like this?

(function ()

{ var docRef = activeDocument

    var newdLayer = docRef.activeLayer.duplicate(); newdLayer;   

    var startRulerUnits = app.preferences.rulerUnits;   

    app.preferences.rulerUnits = Units.PIXELS;    

    var bounds = newdLayer.bounds;    

    var height = bounds[3].value - bounds[1].value;   

    var newSize = (100 / height) * 600;   

    newdLayer.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);  

    app.preferences.rulerUnits = startRulerUnits; 

    var blur = docRef.activeLayer;   

    var startRulerUnits = app.preferences.rulerUnits;   

    app.preferences.rulerUnits = Units.PIXELS;    

    var bounds = blur.bounds;    

    var width = bounds[2].value - bounds[0].value;   

    var newSize = (100 / width) * 900;   

    blur.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);  

    app.preferences.rulerUnits = startRulerUnits;   

    blur.applyGaussianBlur(5);

    }

)

();

//////////////////////////

select(false);

function select(makeVisible) {

  var c2t = function (s) {

  return app.charIDToTypeID(s);

  };

  var s2t = function (s) {

  return app.stringIDToTypeID(s);

  };

  var descriptor = new ActionDescriptor();

  var list = new ActionList();

  var reference = new ActionReference();

  reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "forwardEnum" ));

  descriptor.putReference( c2t( "null" ), reference );

  descriptor.putBoolean( s2t( "makeVisible" ), makeVisible );

  list.putInteger( 13 );

  descriptor.putList( s2t( "layerID" ), list );

  executeAction( s2t( "select" ), descriptor, DialogModes.NO );

}

//////////////////////////

app.activeDocument.activeLayer.merge();

Participant
August 20, 2019

Thanks! Great Answer! One thing. This works perfectly if the layer is aligned center, however if it is not centered it is a little bit buggy. Is it possible to add the center active layer part first and then the rest of the function?