Highlighted

Count Layer Numbers

Explorer ,
Sep 09, 2020

Copy link to clipboard

Copied

Expected Script for count numbers of layers by specified name.

e.g. 8 Layers (name = "Ok_layers) +  2 Layers (name = "Top_layers) = Total 10 Layers

 

so if want to count  Layers with name = "Ok_layers then it should show message  ... 8 layers

Adobe Community Professional
Correct answer by rob day | Adobe Community Professional
#target photoshop
var lyrs = app.activeDocument.layers
var c = 0;
var n = "Ok_layers"
for (var i = 0; i < lyrs.length; i++){
    if (lyrs[i].name == n) {c++} 
};   
alert("Document has a total of " + lyrs.length + " layers\n" + c + " are named " + n)
TOPICS
Actions and scripting

Views

128

Likes

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

Count Layer Numbers

Explorer ,
Sep 09, 2020

Copy link to clipboard

Copied

Expected Script for count numbers of layers by specified name.

e.g. 8 Layers (name = "Ok_layers) +  2 Layers (name = "Top_layers) = Total 10 Layers

 

so if want to count  Layers with name = "Ok_layers then it should show message  ... 8 layers

Adobe Community Professional
Correct answer by rob day | Adobe Community Professional
#target photoshop
var lyrs = app.activeDocument.layers
var c = 0;
var n = "Ok_layers"
for (var i = 0; i < lyrs.length; i++){
    if (lyrs[i].name == n) {c++} 
};   
alert("Document has a total of " + lyrs.length + " layers\n" + c + " are named " + n)
TOPICS
Actions and scripting

Views

129

Likes

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
Sep 09, 2020 0
LEGEND ,
Sep 09, 2020

Copy link to clipboard

Copied

// 2020, use it at your own risk;
if (app.documents.length > 0) {
var thisName = "aaa";
var theLayers = collectLayersByName(thisName);
alert (theLayers.length+" layers of the name »"+thisName+"«\n\n"+theLayers.join("\n"))
};
////// collect layers with certain name //////
function collectLayersByName (aName) {
// the file;
var myDocument = app.activeDocument;
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") ); 
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if group collect values;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart" && isBackground != true*/) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
if (theName == aName) {theLayers.push([theName, theIndex, theID])}
};
}
catch (e) {};
};
return theLayers
};

Likes

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
Reply
Loading...
Sep 09, 2020 1
Adobe Community Professional ,
Sep 09, 2020

Copy link to clipboard

Copied

#target photoshop
var lyrs = app.activeDocument.layers
var c = 0;
var n = "Ok_layers"
for (var i = 0; i < lyrs.length; i++){
    if (lyrs[i].name == n) {c++} 
};   
alert("Document has a total of " + lyrs.length + " layers\n" + c + " are named " + n)

Likes

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
Reply
Loading...
Sep 09, 2020 2
Explorer ,
Sep 09, 2020

Copy link to clipboard

Copied

just i want to alert = 

Please Open {No of counted "ok_Layers"}Photos     [Bold]

Second line text here                                        [Normal]

 

 

 

 

Likes

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
Reply
Loading...
Sep 09, 2020 0
Explorer ,
Sep 09, 2020

Copy link to clipboard

Copied

Want to replace alert as below. please provide

 

Please Open {No of counted "ok_Layers"}Photos     [Bold]

Second line text here                                        [Normal]

Likes

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
Reply
Loading...
Sep 09, 2020 0
Adobe Community Professional ,
Sep 09, 2020

Copy link to clipboard

Copied

Try the following code, it does not use AM code and can handle layers in groups as well

function findLayerCount(l, layerName)
{
	var count = 0;
	var layerCount = l.layers.length;
	for (var j = layerCount - 1; j >= 0; j--)
	{
		if(l.layers[j].constructor.name == "LayerSet")
		{
			var retval = findLayerCount(l.layers[j], layerName)
			count += retval;
			continue;
		}
			
		if(l.layers[j].name == layerName)
			count++;
	}
	return count ++
}
		
var c = findLayerCount(app.activeDocument, "Layer 8")
alert(c + " layers")

-Manan

Likes

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
Reply
Loading...
Sep 09, 2020 3
LEGEND ,
Sep 09, 2020

Copy link to clipboard

Copied

I didn’t run a test in this case, but in my experience the DOM-code might be noticably slower than AM-code depending on the number of layers in particular. 

Likes

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
Reply
Loading...
Sep 09, 2020 4
Adobe Community Professional ,
Sep 09, 2020

Copy link to clipboard

Copied

Yeah, sure you are right about that. It's just that the AM code looks quite intimidating, my intention was to give an alternative so that a user gets to know what can be and can't be done using the DOM code. When I started PS scripting I wanted to avoid AM as a plague, during that phase when I could make no sense of AM I did not like to pick code snippets that I did not understand but did do what I needed.

It's just an effort to help someone like-minded 🙂

-Manan

Likes

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
Reply
Loading...
Sep 09, 2020 2
LEGEND ,
Sep 10, 2020

Copy link to clipboard

Copied

No argument there, from a readability-standpoint DOM-code sure is advantageous. 

Likes

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
Reply
Loading...
Sep 10, 2020 0
Participant ,
Sep 10, 2020

Copy link to clipboard

Copied

I, as a beginner, also intimidate myself with the AM code, I always gave DOM preferences for being a cleaner code and I had in mind that it would be faster, so only now thanks to the smart c.pfaffenbichler  I saw that AM is more fast..

I was sadly mistaken, until I read this discussion.

Likes

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
Reply
Loading...
Sep 10, 2020 0