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. 

Stephen Marsh
Community Expert
Community Expert
May 6, 2021

Thank you, I still consider myself a beginner/hacker at scripting, so working through selected layers to target a layer in a certain position in the stack's selection is not easy for me. You may have missed the original code that I posted, this version has been simplified somewhat by removing a chunk of AM code for standard DOM code.

 

If I can figure out how to target a specific layer among selected layers, then yes, I agree it would be more logical and efficient to get the name from the selected layers than to get it from the SO. Small steps...


The previous script ran in approx. 6.7 seconds to run on a random set of 3 layers stacked.

 

The new code below took approx. 4 seconds for the same 3 layers.

 

It's still a less than ideal hack though... A better coder than me should be able to make this much faster. Can somebody upgrade my code?

 

EDIT: It appears that the best/common way is to temporarily group the selected layers, then do something with the group (rather than with a new doc as my second script uses).

 

 

 

/*
Select all required contiguous layers before executing this script
Selected Layers to SO Named After Bottom Layer.jsx
v1.2 - Stephen Marsh, 6th May 2021
*/

#target photoshop

// Dupe selected layers to temp doc, yes it's hack!
tempDoc("", 5);

// Select the back layer
app.activeDocument.activeLayer = app.activeDocument.layers[app.activeDocument.layers.length - 1];

// Capture the active layer name
var lyrName = app.activeDocument.activeLayer.name;

// Close temp doc without saving
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

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

// Rename the base document SO layer
app.activeDocument.activeLayer.name = lyrName;

function tempDoc(name2, version) {
    var c2t = function (s) {
        return app.charIDToTypeID(s);
    }
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    }
    var descriptor = new ActionDescriptor();
    var reference = new ActionReference();
    var reference2 = new ActionReference();
    reference.putClass(s2t("document"));
    descriptor.putReference(c2t("null"), reference);
    descriptor.putString(s2t("name"), "", "TempLayerCountDoc", name2);
    reference2.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    descriptor.putReference(s2t("using"), reference2);
    descriptor.putInteger(s2t("version"), version);
    executeAction(s2t("make"), descriptor, DialogModes.NO);
}