Photoshop: Smarter layer names when merging

  • 7
  • Idea
  • Updated 2 years ago
  • Implemented
  • (Edited)
When merging layers, the top layer’s name is used for the result. It would be really nice if the topmost layer with a non-“Layer n” (where n is a number) name was used, keeping the topmost user-entered layer name. As it stands today, merging layers usually requires retyping in the layer name straight afterwards.

Photo of Marc Edwards

Marc Edwards

  • 130 Posts
  • 21 Reply Likes
  • happy

Posted 8 years ago

  • 7
Photo of David Jensen

David Jensen

  • 131 Posts
  • 46 Reply Likes
Here's a script I put together (several of the functions from Mike Hale). If you put it in photoshop/presets/scripts/ you can assign it to Cmd/Ctrl-E.

It only looks at the name of selected layers, not layers inside a selected group.
It not only tries to not use "Layer N" but also anything else generated: adjustment layers, shape layers, fill layers, and groups.

It can merge layers, merge down and merge groups.

http://sites.google.com/site/davidjen...
Photo of Marc Edwards

Marc Edwards

  • 130 Posts
  • 21 Reply Likes
Thanks David. Your scripts are great!
Photo of Sheldon McLean

Sheldon McLean

  • 3 Posts
  • 0 Reply Likes
Hi David,
Would it be possible of you to revise the script so that when merging layers (and layer groups) the resulting merged layer is named with the bottom most layer's name? Thats would be very useful for me.
Thanks, in advance!

--Sheldon.
Photo of David Jensen

David Jensen

  • 131 Posts
  • 46 Reply Likes
Here you go. It names the merged layer based on the lowest selected layer with a user created name, or just the bottom selected layer if there is no user created layer name.
http://sites.google.com/site/davidjen...
Photo of Sheldon McLean

Sheldon McLean

  • 3 Posts
  • 0 Reply Likes
Awesome, but doesn't always work as expected. if I show you some cases where it doesn't work, are you interested in tweaking it?

Thanks, Sheldon.
Photo of David Jensen

David Jensen

  • 131 Posts
  • 46 Reply Likes
Sure.
Is it a problem with layer sets? Currently, only items that are highlighted are taken into account when naming.
Photo of David Jensen

David Jensen

  • 131 Posts
  • 46 Reply Likes
This feature is now in in Photoshop CS6 13.0.2! That's the Retina update for those who aren't Creative Cloud subscribers. I guess one CC feature slipped through.

I noticed this when I was trying to update my script for CS6 :-P
Photo of Sheldon McLean

Sheldon McLean

  • 3 Posts
  • 0 Reply Likes
hey, thats great!
also, thanks again for your work on this before.

Sheldon.
Photo of René Damkot

René Damkot

  • 37 Posts
  • 1 Reply Like
Would be very useful.
However, my CS6 Extended (13.0.6) on OSX 10.6.8 does not "include" this.

Second script works, but only if you don't select the bottom layer (Unless it's the "Background" layer...

Any solution?
Photo of René Damkot

René Damkot

  • 37 Posts
  • 1 Reply Like
Okay, edited the script. Problem was that, if there wasn't a BG layer, the script tried to fill the array with a value for layers[0] (reserved for Background layer), where the lowest existing layer was layers[1]. That gave an error.

Since I only needed the script to rename the merged layer to whatever was the lowest selected layers name (I always name my layers), the script could get a bit simpler.

Resulting script, for those who are interested:
#target photoshop
//created by David Jensen, edit by René Damkot

var bottomName = 'Bg'
var BG = 0
var L=app.activeDocument.layers.length;
app.activeDocument.suspendHistory('Merge Layers René', 'main()');
function main(){
x=getSelectedLayerNames(); // get array of layerNames
bottomName=x[0]; // get name of the lowest selected layer
executeAction( charIDToTypeID( "Mrg2" ), undefined, DialogModes.NO ); //merge
if ( activeDocument.activeLayer.name!='Background') { //only rename if it isn't a BG, so a BG layer stays locked
activeDocument.activeLayer.name=bottomName;
}

////// Mike Hale from ps-scripts.com wrote the following functions
function getSelectedLayerNames(){
var names = [];
var selectedIdxs = getSelectedLayersIdx();
for(var l =0; l < selectedIdxs.length; l++ ){
names.push( getLayerNameByIndex( selectedIdxs[ l ] ) );
}
return names;
}
function getSelectedLayersIdx(){
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count
if (activeDocument.layers[L-1].name!='Background') { // test if the file has a 'Background' at the bottom
BG=1; // if not, we cannot test the name of layer[0], so we need layer[1]
}
var selectedLayers = new Array();
for(var i=BG;i<c;i++){ // work up from the bottom layer
selectedLayers.push( desc.getReference( i ).getIndex() );
}
}else{
try{
activeDocument.backgroundLayer;
var mod = 1;
}catch(e){
var mod = 0
}
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "ItmI" ));
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
selectedLayers.push( executeActionGet(ref).getInteger(charIDToTypeID( "ItmI" ))-mod);
}
return selectedLayers;
}
function getLayerNameByIndex( idx ) {
var ref = new ActionReference();
ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm " ));
ref.putIndex( charIDToTypeID( "Lyr " ), idx );
return executeActionGet(ref).getString(charIDToTypeID( "Nm " ));;
}
}
Photo of John Weber

John Weber

  • 50 Posts
  • 4 Reply Likes
I tried this script with Photoshop CC 2015 but it gives me an error 1220:

Line: 56->  ref.putProperty( charIDToTypeID("Prpr") , charIDToTypeID( "Nm " )); 

Any thoughts?