I am needing a script that will “do action” for the currently selected action and repeat it for every layer within the currently selected layer set.
For the same reason recording an action increases efficiency, I would like to be able to repeat that action without having to manually select each layer, one at a time, and then play the action each time. Some files I work with can have hundreds of layers which need the same action run on.
I had thought about that and I thought the best solution would be to group the desired layers into a layer set and have the script run only on each layer within the selected set. Everything outside of the layer set would not be affected.
I am just not sure how to do this with AppleScript, or even if AppleScript is the best way to address it. Perhaps Javascript would be better?
Without knowing what your action does its hard to say if its scriptable…
I don’t think grouping the layers would make any difference in determining when the action is done I think you’ll still need to select each layer one at a time within the group then run the action correct ?
Javascript is an option but I don’t use it so I couldn’t help you there.
As i recall, can’t check it out here at the moment, a script will wait till PS is done with an action (or any other command) unless you tell the script not to. You just need a way to identify the layers and possibly increase the time out settings for the tell block. I have done something like this and it works fine over multiple layers. I will see if I can find the code and post it later today.
As for javascript, it probably won’t be much better than AppleScript. As far as the events are concerned it is pretty much the same implementation just a different syntax.
Some thing like this should get you started. Should just run your action on all the visible layers of the first set.
tell application "Adobe Photoshop CS2"
set Doc_Ref to current document
tell Doc_Ref
set LS_Count to count layer sets
if LS_Count ≠0 then
set L_Count to count layers of layer set 1
repeat with i from 1 to L_Count
if visible of layer i of layer set 1 is true then
set current layer to layer i of layer set 1
do action "My Action" from "My Action Set" -- Case sensitive
end if
end repeat
end if
end tell
end tell
That was a huge help!!! From what you suggested, I updated my current script to the following:
It works great. I just need to figure out how to set the first 3 variables to default to what is currently selected/highlighted. The work around is to type the variables in each time, which is still faster than playing the action manually hundreds of times.
I also came across an error when I had an empty layer. I will need to teach the script to ignore empty layers as well.
tell application "Adobe Photoshop CS3"
tell current document
set myActionSet to "" --current action set
set myAction to "" --current action from myActionSet
set myLayerSet to "" --current layer set
set layerCount to layers of layer set myLayerSet
repeat with i from 1 to the count of layerCount
set current layer to layer i of layer set myLayerSet
set the visible of layer i of layer set myLayerSet to true
do action myAction from myActionSet
set the visible of layer i of layer set myLayerSet to false
end repeat
end tell
end tell
You can get the current layer and test if it is a layer set:
tell application "Adobe Photoshop CS2"
set myLayerSet to current layer of document 1
if class of myLayerSet is layer set then
else
display dialog "A layer set must be selected."
end if
end tell
Unfortunatly you cannot access the actions set names or the action names so you will have to adjust those as needed. If you have more than one action that you need to perform on the image then you might be able to hard code them in and either test for which one to perform (if it is taller than it is wide then do a, if it is wider than it is tall then do b). You could also do a Choose from list command and put the various actions in the list to select from.
Best way to approach a JavaScript (via do script in AppleScript) is to use the ScriptListener plugin Adobe supplies to just have it watch what you’re doing. I’ve found doing it this way is easier than wading through the AppleScript library to figure out how to make Photoshop jump through the hoops I need. Also found it a bit faster than using AppleScript or Actions.
Think of ScriptListener as a way to “record” what Photoshop is doing, only not with AppleScript.
On the other hand, you have to be handy with JavaScript so you know what you can trim from the ScriptListener text file and integrate into AppleScript. I did it when I wrote Clorox File Repair, worked well.
Thanks for the input. I was not able to get your suggestion to work. It would error out when trying to compile.
I decided simply to adjust my workflow to always put the desired actions in an Action Set named “Run Scripts” and always name the desired Layer Set “Graphics”. Then I am promted to enter the action name.
tell application "Adobe Photoshop CS3"
tell current document
set myActionSet to "Run Scripts"
set myAction to text returned of (display dialog "Run Which Action?" default answer "")
set myLayerSet to "Graphics"
set layerCount to layers of layer set myLayerSet
repeat with i from 1 to the count of layerCount
set current layer to layer i of layer set myLayerSet
set the visible of layer i of layer set myLayerSet to true
do action myAction from myActionSet
set the visible of layer i of layer set myLayerSet to false
end repeat
end tell
end tell
This works for me in CS2 and unless they have changed the syntax it should work in CS3 as well. It is using object references rather than indexes for referencing the layers which I find to be easier to write and more consistant in behavior. You can expand the list ActionList with the names of the scripts in your Action set and have all of them available when you run it. It will preform the selected action of every layer in the selected layer set.
set myActionSet to "Run Scripts"
set ActionList to {"Action 1", "Action 2"}
set myAction to (choose from list ActionList with prompt "Run Which Action?" without empty selection allowed and multiple selections allowed) as string
tell application "Adobe Photoshop CS2"
tell current document
set myLayerSet to current layer
if class of myLayerSet is layer set then
set theLayers to layers of myLayerSet
repeat with aLayer in theLayers
set current layer to aLayer
set the visible of aLayer to true
do action myAction from myActionSet
set the visible of aLayer to false
end repeat
else
display dialog "A layer set must be selected."
end if
end tell
end tell