Skip to main content
Participating Frequently
July 31, 2022
Answered

Rename layer comps

  • July 31, 2022
  • 4 replies
  • 3285 views

This is the layer renaming script I am using now. I want to change it to only rename the layer comps. How can I modify the code?
Tthank you very much~

 

app.bringToFront();

if (documents.length == 0) {

alert("No documents to process");

}

else{

var visibility = false;

var docRef = activeDocument;

changeLayerName(docRef);

}

function changeLayerName(layer){

var layers = layer.layers;

if(layers){

for(var i = 0; i < layers.length; i ++){

layers[i].name = "Layer " + [i];

changeLayerName(layers[i]);

}

}

}

This topic has been closed for replies.
Correct answer Stephen Marsh

As my previous script was reactive, renaming existing comps, the following script might be preferred as it is proactive.

 

Create a new comp using the visible layer names:

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/rename-layer-comps/m-p/14139107
Add Layer Comp Using Visible Layer Names.jsx
v1.0 7th October 2023, Stephen Marsh
*/

#target photoshop

// Set the layer variable
var theLayers = activeDocument.layers;

// Create an empty array to hold the layer names
var layerNames = [];

// Backwards loop over the top-level layers
//for (var j = theLayers.length - 1; j >= 0; j--) {
// Forwards loop over the top-level layers
for (var j = 0; j < theLayers.length; j++) {
	if (theLayers[j].visible === true) {
		// Push the layer names to the array
		layerNames.push(theLayers[j].name);
	}
}

// Join the layer names with an underscore separator
var joinLayerNames = layerNames.join("_");

// Add the layer comp (String name, String comment, bool appearance, bool position, bool visibility)
activeDocument.layerComps.add(joinLayerNames, '', true, true, true);

 

4 replies

Stephen Marsh
Stephen MarshCorrect answer
Adobe Expert
October 7, 2023

As my previous script was reactive, renaming existing comps, the following script might be preferred as it is proactive.

 

Create a new comp using the visible layer names:

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/rename-layer-comps/m-p/14139107
Add Layer Comp Using Visible Layer Names.jsx
v1.0 7th October 2023, Stephen Marsh
*/

#target photoshop

// Set the layer variable
var theLayers = activeDocument.layers;

// Create an empty array to hold the layer names
var layerNames = [];

// Backwards loop over the top-level layers
//for (var j = theLayers.length - 1; j >= 0; j--) {
// Forwards loop over the top-level layers
for (var j = 0; j < theLayers.length; j++) {
	if (theLayers[j].visible === true) {
		// Push the layer names to the array
		layerNames.push(theLayers[j].name);
	}
}

// Join the layer names with an underscore separator
var joinLayerNames = layerNames.join("_");

// Add the layer comp (String name, String comment, bool appearance, bool position, bool visibility)
activeDocument.layerComps.add(joinLayerNames, '', true, true, true);

 

Participating Frequently
October 5, 2023

Is there anyway to update this script to Comp Layer names to be the visible layer for each comp?
The script I'm using just changes the names to whichever layer i have selected when I run the script:

app.bringToFront();

if (documents.length == 0) {

alert("No documents to process");

}

else{

var visibility = false;

var docRef = activeDocument;

changeCompName(docRef);

}

function changeCompName(comp){

var comps = comp.layerComps;

if(comps){

for(var i = 0; i < comps.length; i ++){
comps[i].name = docRef.activeLayer.name + [i];

changeCompName(comps[i]);

}

}

}

 

c.pfaffenbichler
Adobe Expert
October 6, 2023

Could you please post screenshots with the pertinent Panels (Toolbar, Layers, Options Bar, …) visible to clarify what you mean? 

Stephen Marsh
Adobe Expert
October 7, 2023

Worked perfectly. Thank you so much!


You're welcome!

Stephen Marsh
Adobe Expert
August 1, 2022

@jimmy0918 

 

Please try the following code:

 

// Rename Layer Comps.jsx

#target photoshop

function renameComps() {
    var counter = 1;
    for (i = 0; i < activeDocument.layerComps.length; i++) {
        myComps = activeDocument.layerComps[i];
        myComps.name = 'Comp ' + counter;
        counter++;
    }
}
app.activeDocument.suspendHistory("Rename Layer Comps.jsx", "renameComps()");
jimmy0918Author
Participating Frequently
August 2, 2022

Thanks for your reply
I use this code to test but nothing changes
I am using PS2022, will it have something to do with this?

Known Participant
August 2, 2022

Hi @jimmy0918,
Please try this code.

app.bringToFront();

if (documents.length == 0) {
alert("没有可处理的文档");
}

else{
var visibility = false;
var docRef = activeDocument;
changeCompName(docRef);
}

function changeCompName(comp){

var counter = 1;
for (i = 0; i < activeDocument.layerComps.length; i++) {
myComps = activeDocument.layerComps[i];
myComps.name = 'LayComp ' + counter;
counter++;
}
}


Thanks
Asuvath

c.pfaffenbichler
Adobe Expert
August 1, 2022

You need to address the LayerComps instead of the Layers then. 

jimmy0918Author
Participating Frequently
August 1, 2022

thanks
Because I don't know programming, according to your prompt, I can't succeed after modifying it many times.

c.pfaffenbichler
Adobe Expert
August 1, 2022

According to the documentation the name of a LayerComp is read-write, so what is the problem?