Skip to main content
Participating Frequently
July 20, 2013
Answered

How to get event target?

  • July 20, 2013
  • 1 reply
  • 1729 views

I've registered the following Hide event: 

app.notifiers.add("Hd  ", ...file path);

Now, how can get object that triggered the event?  [app.active document.activeLayer]  only give me the 'selected' layer.  This obviously does not work in a scenario where the user hides a layer that is not selected.

I would appreciate any help!

This topic has been closed for replies.
Correct answer Michael_L_Hale

That is certainly strange!  It seems that there should be a guid for every object in Photoshop.  I can certainly make each layer name unique but I was hoping for a more direct access approach.

Thanks for all your help


Action Manager does not have direct access to objects in the Object Model. It has it's own classes and object. Sometimes there can be a more direct access than by name. If the event descriptor had the layer index or ID you could use that instead of the name.

Sorry, when I threw this together for you I only tested hidding layers that were not the activeLayer. I don't think the reason the alert is empty is because the top layer was hidden. I think it's because the activeLayer was hidden so the actionReference is different.

Try this( seems to work for me with both )

try {

    if (arguments.length >= 2) {

    var desc = arguments[0];

    var event = arguments[1];

    // make sure it's the hide event

    if (event == charIDToTypeID('Hd  ')) {

        // get the list of what was hidden

        var list = desc.getList(charIDToTypeID('null'));

        // get the actionReferences from the list

        var ref = list.getReference(0);

        var psClass = ref.getDesiredClass();

        // make sure it was a layer that was hidded

        if(psClass == charIDToTypeID('Lyr ')){

            // check to see what is in the reference

            var dataEnum = ref.getForm();

            // should either be an enum if activeLayer

            if(dataEnum == ReferenceFormType.ENUMERATED) {

                    alert(app.activeDocument.activeLayer.name);

            }else{// or the layer name

                alert(ref.getName());

            }

        }

    }

  }

} catch (e) {

  alert( "Error: " + e + ":" + e.line );

}

1 reply

Inspiring
July 20, 2013

When notifier calls an event script it passes two arguments. An ActionDescriptor object and the eventID. You have to know how to use Action Manger but you can pull apart the descriptor for any information it contains.

Do you have much experience with Action Manager?

Inspiring
July 20, 2013

Something like this

try {

    if (arguments.length >= 2) {

        var desc = arguments[0];

        var event = arguments[1];

        // make sure it's the hide event

        if (event == charIDToTypeID('Hd  ')) {

            // get the list of what was hidden

            var list = desc.getList(charIDToTypeID('null'));

            // get the actionReference for what was hidden

            var ref = list.getReference(0);

            // get it's Photoshop class

            var psClass = ref.getDesiredClass();

            // make sure it was a layer that was hidded and do something with the layerName

            if(psClass == charIDToTypeID('Lyr ')) alert(ref.getName());

        }

    }

} catch (e) {

    alert( "Error: " + e + ":" + e.line );

}

unity3Author
Participating Frequently
July 20, 2013

Thank you for your reply!  This is exactly what I was hoping to find!