Can I use image events to sort my files by the number of layers?

I would like to be able to sort my images by the number of layers

For example

IMAGE_A.PSD (made of 3 layers) Image_B.PSD (made of 7 layers)

If PSD has less than 5 layers put in folder “A”

If more than 5 layers put in folder “B”

Have you tried it? I don’t think Image Events can read Photoshop layers (but I haven’t tried it either).

I don’t think Image Events can but take a look at spotlight/mdls it lists layers you may be able to parse the results of that. Are they top level or in layers sets?

This will return a layer count for any file containing more than 1 layer!

set f to choose file
set x to paragraphs of (do shell script "mdls -name kMDItemLayerNames " & quoted form of POSIX path of f)
set layerCnt to (count of every item of x) - 2

For files with only layer (Default) it returns -1.

Amended to handle files with only 1 layer.

set f to choose file
set x to paragraphs of (do shell script "mdls -name kMDItemLayerNames " & quoted form of POSIX path of f & " | cut -d '(' -f 2")
if x = {"null)"} then
	set x to {"only", "one", "layer"}
end if

set layerCnt to (count of every item of x) - 2

This worked a treat. Many Thanks

How can I get it to look at a folder contents and then move those with less than 5 to folder and and those greater to folder B?

It doesn’t quite work there are a few files which don’t go through
could the size of the image be causing the problem some are 150mb or larger?

This one din’t go through when full size, but the file shows the structure of the PSD file

I have just downloaded your ‘problem’ file and run the script (last quoted) and it does work! (on my Mac)

I can’t imagine that the file size has any impact as all this script is doing is just reading the metadata. (I may be wrong!?)

Does this image fail as a part of another/bigger script, repeating through several images?


Its being created in photoshop then the layered photoshop image gets saved to the folder then sorted, but ends up in the 2do folder (those less than 5 layers) even when there are 9 (I don’t if it caused a problem but they are in groups as well).

I’ll try and keep you posted as there are a number of files which do this but as yet I can’t see the common factor! Generally they take this form.


Strange behaviour, I had it happen again, so I moved it make to the main folder and it then went to the right place, I think it might be almost moving around to quickly as photoshop saves it goes down to the wrong place.

So I added a delay of 30 seconds and so far so good.


If the problem is that a folder action is starting, before files have finished saving, or copying into the folder, you can keep checking the file size of the added files and only start the action when the file size has stopped increasing!

I found this script on these forums (Thanks to the ‘Author’ of this script! (Sorry, I can’t remember who it was from))

on adding folder items to theFolder after receiving addedItems
	-- The rest of your wonderful, wonderful script...
end adding folder items to

on stable(addedItems)
	tell application "Finder"
		repeat with theItem in addedItems
					get info for theItem
					set sizethen to (size of the result)
				on error theError -- Stuff went wrong, master
					display dialog theError
					error number -128 -- break it off if stuff went wrong!
				end try
				delay 5
				get info for theItem
				set sizenow to (size of the result)
				if sizethen = sizenow then
					if sizenow = 0.0 then
						error number -128 -- break it off if stuff went wrong!
					end if
					exit repeat
				end if
			end repeat
		end repeat --Waiting for all files to become stable
	end tell
end stable

This is how it looks at the moment.

Rather than using folder actions, I set up the launchD so its always watching in the background.

How would I apply the “on stable” to the current script?

property type_list : {"TIFF", "JPEG", "PNGf", "PICT"}
property extension_list : {"tif", "tiff", "jpg", "jpeg", "png", "pict", "psd"}

set tFolder to ("Hal 9000:Users:matthew:Pictures:HotDestination") as string
delay 30

tell application "Finder"
	repeat with tFile in (get document files of folder tFolder whose name extension is in extension_list and (name does not start with "50" or name does not start with "MJC"))
		set nLayers to my getNumOfLayers(tFile as string)
		if nLayers < 4 then
			move tFile to the folder "Hal 9000:Users:matthew:Pictures:HotFolder2do" -- move to subFolder A
			move tFile to the folder "Hal 9000:Users:matthew:Pictures:HotFolderDone"
		end if
	end repeat
end tell

on getNumOfLayers(f)
	((do shell script "/usr/bin/mdls -name kMDItemLayerNames " & (quoted form of POSIX path of f) & " | /usr/bin/wc -l ") as integer) - 2
end getNumOfLayers

This works great for me. Thanks, intoto!

However, sometimes we do come across layered photoshop document saved in tiff format and this could not detected using this script. Could anyone advise please?

Thanks in advance!