I’m not really a big fan of the DOM implementation (yet), since I don’t know what it’s doing under the hood. Doing what you want seems no problem with batchPlay:
Im getting the ids of the selected layers and then update the name of the last one. All other layers remain selected and also stay the same in terms of visibility etc.
When I tried layers name last time then I could rename only the active layer regardless of ID. I hope this will be improved… maybe already was so we could get different behavior in different PS versions.
Now it’s a pure mess with layer activity and visibility. Especially when you’re trying to manipulate multiple active layers. Some actions doesn’t care if ID is passed - applies something to all active layers. You can’t do something if layer is not active. Doing something on active layer changes visibility. Currently my very simple plugin has nearly 1/3 of the code just to workaround that mess
I’ll try your approach @simonhenke and let you know. Thanks
Good luck
If you’re not using typescript, don’t forget to remove the typing from my code, for example __setLayerProperty(prop, id)
instead of __setLayerProperty(prop: Partial<LayerDescriptor>, id?: number)
These are just things we have to get used to and consider when writing scripts/algorithms that use Photoshop functionality, as this behavior will probably not change internally any time soon.
I don’t really see the problem though, if you know that a selection is necessary, just select the layer beforehand:
Selecting a layer is not a heavy task that requires much computing on Photoshop’s side, so you won’t lose a lot of milliseconds.
And if you want to have everything after your script like it was before, just store the initially selected IDs and reset them afterwards via
That’s exactly what I ended up with (just not all batchPlay). But the issue goes much deeper if you have a mixed visibility active layers. Results for other active layers are completely unpredictable while trying to manipulate one active layer. What I do now:
Get needed layers
Set active/visible state of each layer to array
Do action on each layer
Restore state of each layer
But trouble doesn’t end here. There are still some corner cases in my plugin, where this still is an issue. To fix that, I should set all layers inactive, because some actions are performed on active layers and not on IDs that are provided, then on performing an action set that one layer active and then again set inactive after action is done. But by setting layers inactive, it again messes up visibility… It’s a bit difficult to explain in detail. It’s just a nightmare…
Mostly renaming, changing colors and moving. I’ll make plugin public hopefully this weekend. Will upload obfuscated ccx to my page, but might also give full version here for you guys to check. I’m pretty sure it can be improved. This is my first plugin (also never done CEP or JSX scripts before)
I do not agree. The renaming layer is the core DOM operation. And from my experiments before batchPlay… this action can cost you about 10-30ms. It doesn’t sound like a lot but for 100 layers it is 1-3s and 10-30s for thousands. If you squeeze all descriptors into single batchplay then it probably will be super fast. But again layer rename in Layer class as part of DOM in an object-oriented approach means just renaming of one layer + selection restore. And if more operations needs selection and script does a lot of other steps it can easily sum up into a long time.
Maybe I don’t get your point, but isn’t that just another reason to stick to batchPlay instead of DOM?
Also, the naming isn’t the overhead in this case, since that’s what @Karmalakas wanted to do in his script. It was more about the additional selecting (or restoring visibility etc) of the layers, which he has to do as a workaround.
Would be interesting to have some benchmark tests for how long these individual operations take.
But once again, what I was trying to say is: That’s how Photoshop behaves, so we have to deal with it and the resulting workarounds.
My point is that all core features should be able to run without selecting anything in first place. That is how it should be done. But that is nothing we can do right now.
Yes, I fully agree. It’s a weird design decision, but part of Photoshops core architecture now…I also wish such internal things would be updated or fixed over time, but I feel like new things are mostly built on top of the existing architecture.