Skip to main content
Participating Frequently
April 13, 2013
Question

How do I automate merging multiple layers with same name?

  • April 13, 2013
  • 1 reply
  • 3858 views

It was suggested I ask this question in this forum - any advise gratefully received!

I have to combine over a 800 files into one photoshop file. Each file has with sixty layers. The 60 layers in each file are named identically: Layer 1, Layer 2, Layer 3 up to Layer 59, Layer 60. My intention is to create a final composite file with 60 merged layers (for an animation). That is, with all the Layer 1's merged, all the Layer 2's merged, all the Layer 3's merged and so on. What I need is a script which will merge all layers with the same layer name, ie all the Layer 1s, all the Layer 2s etc (ideally without having to merge each set of layers separately).

I am using Photoshop CS6 and at present I am merging by using the find/name function, then highlighting all the layers selected and then using 'merge layers'.

Any ideas?

Best

JR

This topic has been closed for replies.

1 reply

Paul Riggott
Inspiring
April 13, 2013

This might be close, it will prompt for a folder containing all the 800 files, it will use tif or psd files.

Once you have selected the folder sit back and watch...

#target photoshop
app.bringToFront();

main();
function main(){
selectedFolder = Folder.selectDialog( "Please select input folder");
if(selectedFolder == null) return;
var fileList = selectedFolder.getFiles(/\.(tif|psd)$/i);
open(fileList[0]);
checkBackGround();
setLayersVisOn();
for(var z = 1;z<fileList.length;z++){
    open(fileList);
    checkBackGround();
    setLayersVisOn();
    dupLayers(app.documents[0].name);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
if(z % 9 == 0) mergeSameNamedLayers();
    }
mergeSameNamedLayers();
}
function checkBackGround(){
activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-1];
if(activeDocument.activeLayer.isBackgroundLayer){
    activeDocument.activeLayer.name=activeDocument.activeLayer.name;
    }
}
function twoOrMoreSelected(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) )  return true;
return false;
}
function mergeSameNamedLayers(){
//Get a list of the layer names
var layerNameList = getNamesPlusIDs();
//create an array for unique layer names
var uniqueName = new Array();
for(var s in layerNameList){
    if(layerNameList[2] == "false") uniqueName.push( layerNameList[1].toString());
    }
//now we should have unique layer names
uniqueName = UniqueSortedList( uniqueName ).sort();
//select all layers with the same name, merge them and set blendmode
var BlendMode = new String();
for ( var w in uniqueName){
    deselectLayers();
    for(var z in layerNameList){
        if(uniqueName.toString() == layerNameList[1].toString()){
            //select these layers and get blendmode.
            BlendMode = layerNameList[3].toString();
            selectLayerById(Number(layerNameList[0]), true);
        }
        }
if(twoOrMoreSelected()) activeDocument.activeLayer.merge();
        setBlendMode(BlendMode);
}
};
function setBlendMode(blendMode) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc.putReference( charIDToTypeID('null'), ref );
var desc2 = new ActionDescriptor();
desc2.putEnumerated( charIDToTypeID('Md  '), charIDToTypeID('BlnM'), stringIDToTypeID(blendMode) );
desc.putObject( charIDToTypeID('T   '), charIDToTypeID('Lyr '), desc2 );
executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
};
function deselectLayers() {
    var desc01 = new ActionDescriptor();
        var ref01 = new ActionReference();
        ref01.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc01.putReference( charIDToTypeID('null'), ref01 );
    executeAction( stringIDToTypeID('selectNoLayers'), desc01, DialogModes.NO );
};
function UniqueSortedList(ArrayName){
var unduped = new Object;
for (var i = 0; i < ArrayName.length; i++) {  
unduped[ArrayName] = ArrayName;
};
var uniques = new Array;for (var k in unduped) {
   uniques.push(unduped);}
return uniques;
};
function selectLayerById(ID, add) {
    add = (add == undefined)  ? add = false : add;
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), ID);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID('null'), ref);
if (add) {
  desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
}
desc.putBoolean(charIDToTypeID('MkVs'), false);
executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
};
function dupLayers(DocName) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc.putReference( charIDToTypeID('null'), ref );
var ref2 = new ActionReference();
ref2.putName( charIDToTypeID('Dcmn'), DocName);
desc.putReference( charIDToTypeID('T   '), ref2 );
desc.putInteger( charIDToTypeID('Vrsn'), 5 );
executeAction( charIDToTypeID('Dplc'), desc, DialogModes.NO );
};
function selectAllLayers() {
    var desc29 = new ActionDescriptor();
        var ref23 = new ActionReference();
        ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc29.putReference( charIDToTypeID('null'), ref23 );
    executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );
};
function setLayersVisOn(){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
try{
    activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
   for(i;i<count;i++){
        ref = new ActionReference();
        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
        var desc = executeActionGet(ref);
        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ))
        if(layerName.match(/^<\/Layer group/) ) continue;
        if(!desc.getBoolean(stringIDToTypeID('visible'))){
            var list = new ActionList();
            list.putReference( ref );
            desc.putList( charIDToTypeID('null'), list );
            executeAction( charIDToTypeID('Shw '), desc, DialogModes.NO );
            }
   };
};
function getNamesPlusIDs(){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
   var Names=[];
try{
    activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
   for(i;i<count;i++){
       if(i == 0) continue;
        ref = new ActionReference();
        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
        var desc = executeActionGet(ref);
        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
        var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
        if(layerName.match(/^<\/Layer group/) ) continue;
        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
         desc.hasKey( stringIDToTypeID( 'smartObject' ) )  ?  SO = true :  SO=false;
         var blendmode = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'mode' )));
Names.push([[Id],[layerName],[SO],[blendmode]]);
   };
return Names;
};

Participating Frequently
April 15, 2013

Wow Paul thanks for that. Very helpful. Only problem is I have to drag multiple layers (60 layers) from about 200 files into a composite file, position the files, then merge all Layer1, Layer 2 etc as described. So i will be merging the files in a composite file - would that still work.

I have then to repeat this process x 20-ish times - so these suggestions are very helpful

J

Paul Riggott
Inspiring
April 15, 2013

Ah, no the above script just puts all the layers on top of each other, then merges like named layers.

The following script will just merge like named layers, it might be best to run it about every ten psds, then it only working on 600 layers at a time.

Also this thread might be of interest..

http://forums.adobe.com/thread/492005?tstart=0

#target photoshop
app.bringToFront();

main();
function main(){
checkBackGround();
setLayersVisOn();
mergeSameNamedLayers();
}
function checkBackGround(){
activeDocument.activeLayer = activeDocument.layers[activeDocument.layers.length-1];
if(activeDocument.activeLayer.isBackgroundLayer){
    activeDocument.activeLayer.name=activeDocument.activeLayer.name;
    }
}
function twoOrMoreSelected(){
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) )  return true;
return false;
}
function mergeSameNamedLayers(){
//Get a list of the layer names
var layerNameList = getNamesPlusIDs();
//create an array for unique layer names
var uniqueName = new Array();
for(var s in layerNameList){
    if(layerNameList[2] == "false") uniqueName.push( layerNameList[1].toString());
    }
//now we should have unique layer names
uniqueName = UniqueSortedList( uniqueName ).sort();
//select all layers with the same name, merge them and set blendmode
var BlendMode = new String();
for ( var w in uniqueName){
    deselectLayers();
    for(var z in layerNameList){
        if(uniqueName.toString() == layerNameList[1].toString()){
            //select these layers and get blendmode.
            BlendMode = layerNameList[3].toString();
            selectLayerById(Number(layerNameList[0]), true);
        }
        }
if(twoOrMoreSelected()) activeDocument.activeLayer.merge();
        setBlendMode(BlendMode);
}
};
function setBlendMode(blendMode) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc.putReference( charIDToTypeID('null'), ref );
var desc2 = new ActionDescriptor();
desc2.putEnumerated( charIDToTypeID('Md  '), charIDToTypeID('BlnM'), stringIDToTypeID(blendMode) );
desc.putObject( charIDToTypeID('T   '), charIDToTypeID('Lyr '), desc2 );
executeAction( charIDToTypeID('setd'), desc, DialogModes.NO );
};
function deselectLayers() {
    var desc01 = new ActionDescriptor();
        var ref01 = new ActionReference();
        ref01.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc01.putReference( charIDToTypeID('null'), ref01 );
    executeAction( stringIDToTypeID('selectNoLayers'), desc01, DialogModes.NO );
};
function UniqueSortedList(ArrayName){
var unduped = new Object;
for (var i = 0; i < ArrayName.length; i++) {  
unduped[ArrayName] = ArrayName;
};
var uniques = new Array;for (var k in unduped) {
   uniques.push(unduped);}
return uniques;
};
function selectLayerById(ID, add) {
    add = (add == undefined)  ? add = false : add;
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), ID);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID('null'), ref);
if (add) {
  desc.putEnumerated(stringIDToTypeID('selectionModifier'), stringIDToTypeID('selectionModifierType'), stringIDToTypeID('addToSelection'));
}
desc.putBoolean(charIDToTypeID('MkVs'), false);
executeAction(charIDToTypeID('slct'), desc, DialogModes.NO);
};
function dupLayers(DocName) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc.putReference( charIDToTypeID('null'), ref );
var ref2 = new ActionReference();
ref2.putName( charIDToTypeID('Dcmn'), DocName);
desc.putReference( charIDToTypeID('T   '), ref2 );
desc.putInteger( charIDToTypeID('Vrsn'), 5 );
executeAction( charIDToTypeID('Dplc'), desc, DialogModes.NO );
};
function selectAllLayers() {
    var desc29 = new ActionDescriptor();
        var ref23 = new ActionReference();
        ref23.putEnumerated( charIDToTypeID('Lyr '), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc29.putReference( charIDToTypeID('null'), ref23 );
    executeAction( stringIDToTypeID('selectAllLayers'), desc29, DialogModes.NO );
};
function setLayersVisOn(){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
try{
    activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
   for(i;i<count;i++){
        ref = new ActionReference();
        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
        var desc = executeActionGet(ref);
        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ))
        if(layerName.match(/^<\/Layer group/) ) continue;
        if(!desc.getBoolean(stringIDToTypeID('visible'))){
            var list = new ActionList();
            list.putReference( ref );
            desc.putList( charIDToTypeID('null'), list );
            executeAction( charIDToTypeID('Shw '), desc, DialogModes.NO );
            }
   };
};
function getNamesPlusIDs(){
   var ref = new ActionReference();
   ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
   var count = executeActionGet(ref).getInteger(charIDToTypeID('NmbL')) +1;
   var Names=[];
try{
    activeDocument.backgroundLayer;
var i = 0; }catch(e){ var i = 1; };
   for(i;i<count;i++){
       if(i == 0) continue;
        ref = new ActionReference();
        ref.putIndex( charIDToTypeID( 'Lyr ' ), i );
        var desc = executeActionGet(ref);
        var layerName = desc.getString(charIDToTypeID( 'Nm  ' ));
        var Id = desc.getInteger(stringIDToTypeID( 'layerID' ));
        if(layerName.match(/^<\/Layer group/) ) continue;
        var layerType = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'layerSection' )));
         desc.hasKey( stringIDToTypeID( 'smartObject' ) )  ?  SO = true :  SO=false;
         var blendmode = typeIDToStringID(desc.getEnumerationValue( stringIDToTypeID( 'mode' )));
Names.push([[Id],[layerName],[SO],[blendmode]]);
   };
return Names;
};