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

How do I automate merging multiple layers with same name?

New Here ,
Apr 13, 2013 Apr 13, 2013

Copy link to clipboard

Copied

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

TOPICS
Actions and scripting

Views

3.5K

Translate

Translate

Report

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
Adobe
Valorous Hero ,
Apr 13, 2013 Apr 13, 2013

Copy link to clipboard

Copied

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;
};

Votes

Translate

Translate

Report

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 ,
Apr 15, 2013 Apr 15, 2013

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Valorous Hero ,
Apr 15, 2013 Apr 15, 2013

Copy link to clipboard

Copied

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;
};


Votes

Translate

Translate

Report

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 ,
Apr 15, 2013 Apr 15, 2013

Copy link to clipboard

Copied

Thanks a lot Paul I'll try it out tomorrow and let you know - thanks again.

J

Votes

Translate

Translate

Report

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 ,
Apr 17, 2013 Apr 17, 2013

Copy link to clipboard

Copied

OK Paul tried it out. Pasted the code into AppleScript Editor - yes? But when I run it a box comes up saying 'Syntax Error. Expected end of line, etc. but found unknown token.'

Any ideas? Would be very helpful if I can get this to work.

Best

J

Votes

Translate

Translate

Report

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
Valorous Hero ,
Apr 17, 2013 Apr 17, 2013

Copy link to clipboard

Copied

The code is JavaScript not AppleScript. Open ExtendScript Toolkit or a plain text editor and paste the code into it, save the code to the Photoshop Presets/Scripts folder with an extension of .jsx Once it has been saved restart Photoshop if it was open, then you can run the script by.. File - Scripts - select the script.

Votes

Translate

Translate

Report

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 ,
Apr 17, 2013 Apr 17, 2013

Copy link to clipboard

Copied

Thanks Paul. I'll try it.

Votes

Translate

Translate

Report

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 ,
Apr 17, 2013 Apr 17, 2013

Copy link to clipboard

Copied

LATEST

Paul

It works - thanks - brilliant!!!

Youre a genius.

J

Votes

Translate

Translate

Report

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