Proper way to detect when different layers are selected

We need some detection when different layers are selected. So far I know about this:
Select layer
Delete layers
Convert to smartobject
New layer
Duplicate artboard
Place event

Ah yes, that’s something I’ve came across and thought about multiple times already - wish there was some kind of event or hook for this specific type of change.
You’re list is already quite good and covers the most cases.

What about:

  • select/make/delete document
  • convert to layers (reverse of smart object)
  • internal scripts like “delete all empty layers” (a0754df2-9c60-4b64-a940-6a2bb1102652)
  • selectAllLayers / selectNoLayers
  • “AdobeScriptAutomation Scripts” (such as "Load into Stack)
  • flattenImage (you probably count that under “merge”)
  • Select and Mask or Contentaware fill with the “output to new layer” option

That’s all I can think of right off the bat

yes we need same event for document change. I proposed that in pre-release long ago and I think it will be done.

Anyway, when you change the document it actually won’t change selected layers. Layer reference is the same but document reference is different. And in some cases, you can run action within inactive documents.

selectNoLayers is what I meant by deselect :slight_smile:

internal script “delete all empty layers” is simply “delete” event. But the question is how this could be handled. Some different scripts could emit hundred of events and you don’t want to check it after each event. So perhaps it could be delayed and merged into one event once script is done.

To be honest I think that as long as we don’t get any native callback for that, the list will do pretty fine already. Often times you want to refresh the displayed data in your plugin relative to the selected layer - What I thought of before was adding a little refresh/reload icon that allows the user to manually reload the data for the currently active layer, just in case.

Internally there must be some mechanism already, as all the other panels like “properties” are always perfectly up-to-date. Maybe there will be some way of exposing that change in the future.

I agree that Document-Change would also be a welcomed addition, not only Selected-Layer-Change

Yes I think that all those events are calling some method that changes the selected layers. So they could possibly add a hook inside that one method.

@Jarda Some more events just came to my mind: Undo/redo or generally selecting and deleting history states or history snapshots can also change the selected layer I guess

edit: Well, under the hood it might also be select & delete events with different targets. Depending on how specifically you want to test for a selection change, this could be relevant. If you just listen for those events in general, then this doesn’t matter I assume.