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?

unity3Author
Participating Frequently
July 20, 2013

Okay, I'm stumped again.  Your code example was very clear but I'm still confused on how to get artlayer object itself.

I'm coding this script for a client that works in construction.  They want a .PSD file that they can show a variety of building options to their customers. (Siding colors, roofing styles etc.)

My script is to make only one option(layer) visible at one time.  For example, if my client clicks on roofing style #2, then my script would hide roofing style #1.

If I could just get the object reference of the art layer, I would be good to go.

Inspiring
July 20, 2013

All the event descriptor has is the name of the layer hidden.

If your layers have unique names and are on the top level you can use document.layers.getByName();

If the names are unique and could be in layerSets you can either search all the layer in all the layerSets using the Photoshop Object Model or use Action Manager.

function selectLayerByName(layerName) {

    var desc1 = new ActionDescriptor();

    var ref1 = new ActionReference();

    ref1.putName(app.charIDToTypeID('Lyr '), layerName);

    desc1.putReference(app.charIDToTypeID('null'), ref1);

    desc1.putBoolean(app.charIDToTypeID('MkVs'), false);

    executeAction(app.charIDToTypeID('slct'), desc1, DialogModes.NO);

};

Then the activeLayer would be the one just hidden. Other than making the layer active, I don't know a good way to get an artLayer object from Action Manager.

I don't know how you could tell which layer was hidden if you have more than one layer with the same name.