Count layers in photoshop document

Hy guys,

I’m trying to right a script who count how many layers I have in my photoshop document.

Here’s what i’ve come up with:


tell application "Adobe Photoshop CS4"
	try
		set myDoc to current document
	end try
	tell myDoc
		set myDocLayers to count every layer
		set myLayersetLayers1 to count every layer in every layer set
		set myLayersetLayers2 to count every layer in every layer set in every layer set
		set myLayersetLayers3 to count every layer in every layer set in every layer set in every layer set
		set myLayersetLayers4 to count every layer in every layer set in every layer set in every layer set in every layer set
		set myLayersetLayers5 to count every layer in every layer set in every layer set in every layer set in every layer set in every layer set
		set myLayersCount to (myDocLayers + myLayersetLayers1 + myLayersetLayers2 + myLayersetLayers3 + myLayersetLayers4 + myLayersetLayers5)
		display dialog "There's " & myLayersCount & " layers in this document." buttons {"Ok"} default button 1 with title "Layers count"
	end tell
end tell

It’s working well but it’s taking so much time when I have huge document. I wonder if it’s possible to fine tune it a little more or if there’s other and simpler ways of doing it.

Thanks!

Am I missing a nuance you’re after? Otherwise, it’s as simple as.

tell application "Adobe Photoshop CS4"
	set layerCount to number of layers of current document
end tell

Works fine with layers in subfolders (groups).

I don’t know if you tried it but it is not working for me. It’s counting the number of layers, or groups, at the first level, but not those that are inside layer set, or group.

Same here. I don’t have the time at the moment to work this up but you could try this for speed.
If your doc is PSD then you don’t need to open the file.

set This_PSD to POSIX path of (choose file without invisibles)
set The_Layers to paragraphs 3 thru -2 of (do shell script "/usr/bin/mdls -name kMDItemLayerNames " & quoted form of This_PSD)

The list contains both layers and layer sets () so you could count these and remove from end of list where their name string appears.

Okay, I didn’t understand you needed sub-sub-groups (we don’t get that complex here). Still not sure what you mean by “sets” (I must be getting rusty, or it’s not something I’ve ever used).

That is odd.if you have a bunch of layers, and 1 level of groups, it counts fine:

Group 1
–Layer 7
–Layer 6
Layer 5
Layer 4
Layer 3
Layer 2
Layer 1

Counts the above as 7 layers just fine.

But if you sub-sub-group it, it counts funny:

Group 1
–Group 2
----Layer 5
----Layer 4
----Layer 3
–Layer 7
–Layer 6
Layer 2
Layer 1

It counts this as 4 layers. Bizarre, misses the sub-sub group (Group 2) entirely. It gets worse from there (talk about bad implementation), more than one group (first level) also starts mis-counting. Oye!

I took a peek, because I’m limited on time, using:

set docInfo to info of current document

And it seems to get an accurate count you’d need to read the document container for the layers (and Groups count as layers), then check to see if each layer is of type “layer set” (i.e. Group, gotta love the terminology switch) in which case you’d have to detect the layers within, and re-check those layers to see if they are a “layer set” and so forth.

(Was watching the values and information as ScriptDebugger was recording it.)

So you’d need a handler to do the “see if the current layer being examined is a layer set” check, and a repeating loop that could call the handler as it read-through layer lists and bumped into sets. Wish I had time for this, would be a fun one to unravel.

It would be similar to looping through the contents of folders and subfolders in the Finder, in theory. I don’t have a loop handy though and I’m sure someone will beat me to it. Of course Mark67’s shell script probably just cuts to the chase.

Sorry for the initial confusion.

Browser: Firefox 3.5.3
Operating System: Mac OS X (10.6)

I’ve got to leave for the weekend, but maybe someone else can finish this up.

It works on 2nd-level sublayers, but there is a recursion problem with 3+ levels. Probably staring me in the face, but I’m not seeing it with 10 minutes before I go home. :wink:

global g_totalLayers


on subLayers(layerSetName)
	tell application "Adobe Photoshop CS4"
		tell current document
			--search a particular layer set for number of sublayers and add to g_totalLayers
			set layerCount to number of layers in layer layerSetName
			set setCount to number of layer sets in layer layerSetName
			set realLayerCount to layerCount - setCount
			
			--increment global
			set g_totalLayers to g_totalLayers + realLayerCount
			
			--examine sublayers
			if setCount > 0 then
				repeat with k from 1 to setCount
					my subLayers(name of layer k)
				end repeat
			end if
		end tell
	end tell
end subLayers

tell application "Adobe Photoshop CS4"
	tell current document
		
		--for testing purposes, remove from final
		set docLayerInfo to info
		
		--count first-level layers
		set docLayerCount to number of layers
		set docLayerSetCount to number of layer sets
		set topLevelLayerCount to docLayerCount - docLayerSetCount
		
		--initialize master counter
		set g_totalLayers to topLevelLayerCount
		
		--examine sublayers
		if docLayerSetCount > 0 then
			repeat with i from 1 to docLayerSetCount
				my subLayers(name of layer i)
			end repeat
		end if
		
	end tell
end tell

Hi Twider,
this script should do the job, hopefully ;-). myLayerList indludes all art layers “ MyNumber the count of myLayerList. Perhaps you don’t want to count all kinds of art layers. here’s is a list to use for exceptions:

set MyKind to {"brightness contrast layer", "channel mixer layer", "color balance layer", "curves layer", "gradient fill layer", "gradient map layer", "hue saturation layer", "inversion layer", "levels layer", "pattern fill layer", "posterize layer", "selective color layer", "solid fill layer", "text layer"} 
	
		if (kind of myLayer as string) is in MyKind then
...

 
global myLayerList

set myLayerList to {}
tell application "Adobe Photoshop CS4"
tell current document
set MyName to name
my doGetLayers(layers)
set MyNumber to count myLayerList
display dialog "Count of art layers in " & MyName & ": " & MyNumber
end tell
end tell

on doGetLayers(myCurrentLayers)
tell application "Adobe Photoshop CS4"
repeat with i from 1 to count myCurrentLayers
set myLayer to item i of myCurrentLayers
if class of myLayer is art layer then
set end of myLayerList to myLayer
else if class of myLayer is layer set then
my doGetLayers(layers of myLayer)
end if
end repeat
end tell
end doGetLayers

Sorry for the late reply but i was in vacations! :cool:

For the solutions from “Hans-Gerd Classen” and “CalvinFold” it worked but when you have .psd with hundred of layers and layer set it’s taking to much time. “Mark67” came up with the fastest way of doing it, so here’s the script if come up with:

on open these_items
	tell application "Finder"
		repeat with anItem in these_items
			set myFileName to name of anItem
			set myFileExt to name extension of anItem
			
			if myFileExt is "psd" then
				set myPSD to POSIX path of anItem
				set theLayers to paragraphs 3 thru -2 of (do shell script "/usr/bin/mdls -name kMDItemLayerNames " & quoted form of myPSD)
				set myLayerCount to count every item in theLayers
				display dialog "The document " & myFileName & return & "have " & myLayerCount & " layers." buttons "Ok" default button "Ok" with title myFileName
			else
				display dialog "The document " & myFileName & return & "is not a Photoshop document." buttons "Ok" default button "Ok" with title myFileName
			end if
		end repeat
	end tell
end open

Thanks for everything guys!

Hi Twider,

I didn’t know the mdls command at all … it’s easy going :slight_smile:

perhaps it’s interesting for you to create layers (and txtfile) which include height and weight of bounds of every layer in your screendesign¿ we discussed this here: http://www.hilfdirselbst.ch/foren/gforum.cgi?do=post_view_flat;post=428093;page=2;sb=post_latest_reply;so=ASC;mh=15;