Identifying clipping masks in Photoshop

How can I identify all the layers in my file that are clipping masks? I need to merge each clipping mask with the layer below it for the rest of my script to work.

You don’t say exactly what a clipping mask is? By this do you mean a layer that has a ‘vector mask’ or a layer that has a ‘channel mask’ or both? I don’t know what version of Photoshop your running but there is NO access to any of this in AppleScript with CS2 at least. The only methods of working with any of this is via ‘ScriptListener’ code. Here is the only way I have found to find out if a layer has a channel mask. The idea is to try and make a new layer mask (what the scriptlistener output does) if it succeeds then there was no mask fails there was already a mask. (you can’t make a new one before deleting the old one)


tell application "Adobe Photoshop CS2"
	activate
	set display dialogs to never -- Must have this or the JavaScript will throw an error dialog
	set Doc_Ref to current document
	tell Doc_Ref
		set current layer to layer 1 -- Make the layer you want to test the current one
		set Saved_State to current history state
		set Masked_Layer to false
		select region {{0, 0}, {10, 0}, {10, 10}, {0, 10}} -- Must make some active selection
		if my Layer_Mask_Make(Doc_Ref, "RvlS") then
			set current history state to Saved_State
		else
			set Masked_Layer to true
			set current history state to Saved_State
		end if
		-- do your stuff here.
	end tell
end tell
Masked_Layer

on Layer_Mask_Make(Doc_Ref, Mask_Ref)
	-- Mask_Ref = RvlA, HdAl, RvlS, HdSl as strings
	tell application "Adobe Photoshop CS2"
		tell Doc_Ref
			try
				do javascript "Layer_Masks(); function Layer_Masks() {function cTID(s) { return app.charIDToTypeID(s); }; function sTID(s) { return app.stringIDToTypeID(s); }; var desc01 = new ActionDescriptor(); desc01.putClass( cTID('Nw  '), cTID('Chnl') ); var ref01 = new ActionReference(); ref01.putEnumerated( cTID('Chnl'), cTID('Chnl'), cTID('Msk ') ); desc01.putReference( cTID('At  '), ref01 ); desc01.putEnumerated( cTID('Usng'), cTID('UsrM'), cTID('" & Mask_Ref & "') ); executeAction( cTID('Mk  '), desc01, DialogModes.NO );};" -- show debugger on runtime error
				return true -- A layer mask was made.
			on error
				return false -- A layer mask was NOT made one must already exist.
			end try
		end tell
	end tell
end Layer_Mask_Make

Vector Masks are a little different in the fact that they do list in path items when a layer is made the active one. Here is how to test for this one too.

tell application "Adobe Photoshop CS2"
	activate
	set display dialogs to never
	set Doc_Ref to current document
	tell Doc_Ref
		set current layer to layer 3 -- Make the layer you want to test the current one
		if kind of every path item contains vector mask then
			set Masked_Layer to true
		else
			set Masked_Layer to false
		end if
		-- do your stuff here.
	end tell
end tell
Masked_Layer

You also could be talking about a clipping layer with any number of layers grouped above it. There is a “grouped” property for the layer. The clipping layer will have this set to false, those in the group above it will be set to true. If you want to merge these grouped layers then something like this should work:

tell application "Adobe Photoshop CS3"
	set thisDoc to current document
	tell thisDoc
		set groupedLayers to name of every layer whose grouped is true
		repeat with aLayer in groupedLayers
			merge layer aLayer
		end repeat
	end tell
end tell