Changing order of layers in Photoshop

I’m having trouble with moving a newly created layer above another existing layer in Photoshop CS3. Here is the script:

tell application "Adobe Photoshop CS3"
	activate
	set thisDoc to current document
	tell thisDoc
		set layerName to name of current layer
		set duplicateLayer to make art layer with properties {name:layerName}
		move layer duplicateLayer to after (layer whose name is layerName)
	end tell
end tell

The script correctly creates a new layer and names it the same as the current layer, but then I get this error when it tries to change the layer order: Can’t get layer whose name = “Blah”.

I’m eventually going to put this script into a repeat loop with this desired result:

“Layer A” - empty
“Layer A” - with stuff
“Layer B” - empty
“Layer B” - with stuff

I know it seems like an odd thing to want to do, but it’s part of a much larger script. BTW, what is the difference between a “layer” and an “art layer”?

Im not sure with what you are doing but if you want an empty layer with the same name above the current layer then you could.

tell application "Adobe Photoshop CS2"
	activate
	set thisDoc to current document
	tell thisDoc
		set layerName to name of current layer
		set duplicateLayer to make art layer before current layer with properties {name:layerName}
	end tell
end tell

OK, that got the one-pass script working, but I now I’m having a problem when I insert it into a repeat loop. I’m not sure how to reference the current layer variable.

tell application "Adobe Photoshop CS3"
	activate
	set thisDoc to current document
	tell thisDoc
		set nonColorLayers to layers whose name does not start with "COLOR:"
		repeat with i in nonColorLayers
			set layerName to name of i
			set duplicateLayer to make art layer before i with properties {name:layerName}
		end repeat
	end tell
end tell

You do not need the whose clause, just target the layer by name:

tell application "Adobe Photoshop CS3"
	activate
	set thisDoc to current document
	tell thisDoc
		move layer "Layer 1" to before layer "Layer 2"
	end tell
end tell

The problem is that the object reference changes when you add in a layer. Use the layer name instead:

tell application "Adobe Photoshop CS3"
	activate
	set thisDoc to current document
	tell thisDoc
		set nonColorLayers to name of layers whose name does not start with "COLOR:"
		repeat with layerName in nonColorLayers
			set duplicateLayer to make art layer before layer layerName with properties {name:layerName}
		end repeat
	end tell
end tell

OK, I’ve got all this to work beautifully. Now the snag is with the layers that are in layer sets. If I am just looking at layer names when I set the “mergeLayers” variable, the script works fine, but when I start targeting “kind” properties, the script doesn’t seem to be able to look into layer sets and I get an error. Does this seem odd that the script can see the “name” of layers inside layer sets, but not any other property?

Is there a way to just ignore layers that are inside of layer sets? There is a “container” property, but of course to call that I have to look inside the layer sets!

tell application "Adobe Photoshop CS3"
	set thisDoc to current document
	tell thisDoc
		set mergeLayers to (name of layers whose name does not start with "COLOR:" and name is not equal to "Background" and kind is not text layer and kind is not smart object layer)
		repeat with layerName in mergeLayers
			make art layer after layer layerName with properties {name:layerName}
			merge layer layerName
		end repeat
	end tell
end tell

If you change the line where you set the variable mergeLayers to specify art layers it works. The following ignores layers in layer sets:

set mergeLayers to (name of art layers whose name does not start with "COLOR:" and name is not equal to "Background" and kind is not text layer and kind is not smart object layer)

The next line collects the names of layers in layer sets:

set mergeLayers to (name of art layers of layer sets  whose name does not start with "COLOR:" and name is not equal to "Background" and kind is not text layer and kind is not smart object layer)