Photoshop, use layer class stored in variable, with index

Hi everyone!
This is my first post here! :slight_smile:

I’m trying to figure out a part of a larger script that I’m working on for Photoshop (CS5). Basically I’m trying to set a variable, myClass, to the class of the current layer. This successfully sets the variable to art layer or layer set, depending on what the current layer is. I think it’s successfully, anyway. To me, it seems to return correctly.

These simplified codes below shows basically what I’m trying to do. The first one is working, but of course, it doesn’t use the variable:


tell application id "com.adobe.Photoshop"
	tell current document
		
		set current layer to art layer 1 --works
		set current layer to layer set 1 --works
		
	end tell
end tell

When I’m trying to use the myVar variable instead of “art layer” or “layer set” in the code I get a Syntax Error:


tell application id "com.adobe.Photoshop"
	tell current document
		
		tell current layer to set myClass to class --returns art layer or layer set
		set current layer to myClass 1 --this returns the error (Expected end of line, etc. but found a number.)
		
	end tell
end tell

Is there a way to use variables like this? To make something like “myClass 1” be read as “art layer 1”. That is, to make the second example work as the first example.

Thank you!
/Daniel

I think that class is read only. In other words you can only find out what class the layer is.

What are you trying to achieve? Are you trying to change a layer from a layer set to an art layer? If so why?

Thanks for your reply,

No, that’s not what I’m trying to do. I’m not trying to write to the class, so I don’t know if it matters whether it’s read only or not(?). I’m storing the class in a variable.
Also, I’m not trying to change a layer from a layer set into an art layer. I’m trying to change the selection of an art layer/layer set to another art layer/layer set based on if the class of the current layer is an art layer or a layer set.

“set current layer to art layer 1” tells Photoshop to select the art layer with index 1. (edit: typo, changed “layer” to “art layer”)
“set current layer to layer set 1” tells Photoshop to select the layer set with index 1. (edit: typo, changed “layer” to “layer set”)
“set current layer to layer 1” tells Photoshop to select a layer with index 1, this can be both an art layer or a layer set, and both an art layer and a layer set can have an index of 1 at the same time.

Say that the current layer (the selected layer in the layer palette) is an art layer. Now I want to select a different layer based on the fact that the current layer is an art layer. I’m storing the class (art layer) in a variable (myClass). Now I can’t figure out how to use this variable to tell Photoshop to select (in this case) an art layer with, say, index 1. And that’s what I’m trying to achieve in my second example of my first post.

Thank you,
Daniel

Does this do what you want?

tell application id "com.adobe.Photoshop"
	tell current document
		
		tell current layer to set myClass to class --returns art layer or layer set
		
		if myClass is equal to layer set then
			set current layer to layer set 1
		else if myClass is equal to art layer then
			set current layer to art layer 1
		end if
		
	end tell
end tell

I tried (for a short while!) to set a variable in the way you wanted and couldn’t.

Yes, thank you. That would achieve what I want in this particular example. I was planning to use it in some more complex scenarios, though. With more than 2 classes, and combinations of different classes. It would be a pain to write conditions for every imaginable combination, or even for every class for that matter.

I found a thread about something called “run script” which looks interesting, I think I remember something called “eval” from other scripting languages, that seems kind of similar, if I’m not mistaken:
https://discussions.apple.com/thread/1858037?start=0&tstart=0

Seems like you can store strings in different variables and have “run script” to read them as code. Can’t really figure out how use it and if it would help me at all.

Thanks,
Daniel

Hi,

why not throw it to a function¿ so you just have to set it once …


--old fashioned ..
tell application "Adobe Photoshop CS"
	tell current document
				tell current layer to set myClass to class
		set Nbr to 1
		set current layer to (my setLayer(myClass, Nbr))
			end tell
end tell

on setLayer(myClass, Nbr)
	tell application "Adobe Photoshop CS"
		tell current document
						if myClass is equal to layer set then
				return layer set Nbr
			else if myClass is equal to art layer then
				return art layer Nbr
			end if
		end tell
	end tell
end setLayer

“run script” would be like this

tell application "Adobe Photoshop CS"
	tell current document
		set Nbr to (1 as text)
		set theclass to class of current layer
		run script "set current layer of current document to " & theclass & space & Nbr & " of current document"
	end tell
end tell