Skip to main content
Mugen777
Inspiring
May 5, 2021
Answered

Is it possible to make the name of combined layers the bottom one's?

  • May 5, 2021
  • 2 replies
  • 15088 views

Sorry for the bad English in the title. What I mean is, for example I have 3 layers

top - Edit 2

mid - Edit

bot - Main layer with a name that makes sense

When I combine them together, the name of the combined layer becomes "Edit 2". Is it possible to make it the bottom one with a setting? Maybe with an action? 

(just to be clear, it is not always 3 layers and the names are not "Edit, Edit2" etc.)

This topic has been closed for replies.
Correct answer jazz-y

It is fast when there are 10 layers but when the numbers become 100+, you feel the difference 😕😕 

I honestly have no idea how yours work, but mine is basically create 2 arrays with for then compare them to find difference. And since it is a loop, it gets slower with more layers. 


I did not see the simplest way - to get the name of the 1st target layer and assign it after convert to SO.

 

function main() {
	var s2t = stringIDToTypeID;

	(tr = new ActionReference).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
	tr.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));

	(lr = new ActionReference).putProperty(s2t('property'), n = s2t('name'));
	lr.putIdentifier(s2t('layer'), executeActionGet(tr).getList(p).getReference(0).getIdentifier(s2t('layerID')));
	var nm = executeActionGet(lr).getString(n)

	executeAction(s2t('newPlacedLayer'), undefined, DialogModes.NO);

	(r = new ActionReference()).putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
	(d = new ActionDescriptor()).putReference(s2t("null"), r);
	(d1 = new ActionDescriptor()).putString(s2t("name"), nm);
	d.putObject(s2t("to"), s2t("layer"), d1);
	executeAction(s2t("set"), d, DialogModes.NO);
}

app.activeDocument.suspendHistory("Smart Obj Script", "main()");

 

2 replies

Kukurykus
Legend
August 6, 2021

 

sTT = stringIDToTypeID;
(ref = new ActionReference()).putEnumerated
(sTT('layer'), sTT('ordinal'), sTT('targetEnum'));
(dsc = new ActionDescriptor()).putReference(sTT('null'), ref)
executeAction(sTT('linkSelectedLayers'), dsc)
with((actveDcmnt = activeDocument).activeLayer)
	nme = linkedLayers.pop().name, merge()
actveDcmnt.activeLayer.name = nme

 

or:

 

(aD = activeDocument).suspendHistory('', ''), aHS = aD.activeHistoryState
	sTT = stringIDToTypeID, dsc = new ActionDescriptor(), arr = ['back', 'for']; while(arr.length)
		(ref = new ActionReference()).putEnumerated(sTT('layer'), sTT('ordinal'), sTT(arr.shift() + 'wardEnum')),
		dsc.putReference(sTT('null'), ref), executeAction(sTT('select'), dsc); nme = aD.activeLayer.name
aD.activeHistoryState = aHS, (lyr = aD.activeLayer).merge(), aD.activeLayer.name = nme

 

or else:

 

sTT = stringIDToTypeID;
(ref = new ActionReference()).putEnumerated
(sTT('layer'), sTT('ordinal'), sTT('targetEnum'));
(dsc = new ActionDescriptor()).putReference(sTT('null'), ref)
executeAction(sTT('groupLayersEvent'), dsc), aD.activeLayer.merge()
.name = (lrs = (aD = activeDocument).activeLayer.layers)[lrs.length - 1].name

 

Mugen777
Mugen777Author
Inspiring
August 6, 2021

Hey there


Looks like it is faster than what I created before, I just changed 

    (lyr = aD.activeLayer).merge()

with 

    executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO)

to turn layers to smart object instead, and looks like it is lot better than before : ) 

Thank you!

Kukurykus
Legend
August 6, 2021

These versions were due your to original question, but after I found you changed mind to make Smart Object I wrote forth code accordingly to your new requirement: Aug 06, 20121

 

Regarding to the speed when using these codes for merging the fastest is 1st, then 3rd & 2nd. When using the 2nd code to make a smart object from layers it is as fast as the 4th code 🙂

 

I didn't expect you to show up after over 2 months, but if you are here add one of my answers to correct (fastest) solutions for some stray forum scripting wanderers 😉

Derek Cross
Community Expert
Community Expert
May 5, 2021

In the Layers panel, you can drag the layers to change the order, and you can relabel the layers.

 

 

Mugen777
Mugen777Author
Inspiring
May 5, 2021

So far I was renaming layers after combining them, but it takes a lot of time, so I thought maybe there is a way to change that setting. 

Mugen777
Mugen777Author
Inspiring
May 7, 2021

 

Yet another approach, this time select the bottom layer, then run the script. Enter into the prompt the number of additional layers to select above the bottom active layer.

 

This version runs in ~1 second, much better than my previous 7 and 4 second scripts.

 

There is no error checking to ensure that nothing bad happens if you enter a number longer than the length of available layers.

 

 

/*
Is it possible to make the name of combined layers the bottom one's?
https://community.adobe.com/t5/photoshop/is-it-possible-to-make-the-name-of-combined-layers-the-bottom-one-s/td-p/12016052

Target the bottom layer, then run the script to select all required sequential layers

Selected Layers to SO Named After Bottom Layer.jsx

v1.3 - Stephen Marsh, 7th May 2021
*/

#target photoshop

function main() {
    // Capture the active layer name
    var lyrName = app.activeDocument.activeLayer.name;

    // Enforce integer input
    var origInput;
    while (isNaN(origInput = prompt("Enter number of additional layers to select:", "1")));
    var inputToInteger = parseInt(origInput);

    // Loop forward layer selection N times
    for (i = 0; i < inputToInteger; i++) {

        selectForwardLayer(false);

        function selectForwardLayer(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.putEnumerated(s2t("selectionModifier"), s2t("selectionModifierType"), s2t("addToSelection"));
            descriptor.putBoolean(s2t("makeVisible"), makeVisible);
            list.putInteger(2);
            list.putInteger(3);
            descriptor.putList(s2t("layerID"), list);
            executeAction(s2t("select"), descriptor, DialogModes.NO);
        }
    }

    // Convert the selected layers to an embedded Smart Object
    executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO);
    //app.runMenuItem(stringIDToTypeID('newPlacedLayer'));

    // Rename the SO layer
    app.activeDocument.activeLayer.name = lyrName;
}
app.activeDocument.suspendHistory("Run script", "main()");

 

 


I think making it run in 2 steps is a bad idea because purpose of the script is making it work with single click, and seamless if possible. 

And, it is indeed possible. With some external help as well, I managed to write this. It works without opening SO (which I didn't like), but it can be a bit slower with bigger files (very little tho). I personally prefer this.  

 

/*
Create a new smart object with the bottom layer's name
Gökhan Şimşek - 7.5.21
v1.1
*/

#target photoshop

var myDoc = app.activeDocument.activeLayer.parent;
var myId = app.activeDocument.activeLayer.id;
var mainArray;
var beforeArray;
var afterArray;
var indexNum;
var diff;
var newName;

// Array function that creates array from the all layers 1.1
function createArray(doc){
    var myLength= doc.layers.length;
    for(i=0; i<myLength; i++){
        mainArray.push(doc.layers[i].name);
        if(doc.layers[i].id == myId){
            indexNum = i;
        }
    }
    return mainArray;
}

function main(){
    //First Array
    mainArray= [];
    beforeArray = createArray(myDoc);

    // Convert the selected layers to an embedded Smart Object
    executeAction(stringIDToTypeID("newPlacedLayer"), undefined, DialogModes.NO);

    //New Array
    mainArray= [];
    afterArray = createArray(myDoc);

    //Length Difference
    diff = beforeArray.length - afterArray.length;

    // Set the new name - 1.1
    newName= beforeArray[indexNum+diff];

    // Rename the Smart Object
    app.activeDocument.activeLayer.name = newName;
}

//To undo with single move
app.activeDocument.suspendHistory ("Smart Obj Script", "main()");

 


I made it undo friendly as well. I personally satisfied : D Thank you for all your help, it helped a lot to create this version : )