10 Replies Latest reply on Jul 20, 2013 3:24 PM by Michael L Hale

    How to get event target?

    unity3

      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!

        • 1. Re: How to get event target?
          Michael L Hale Level 5

          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?

          • 2. Re: How to get event target?
            Michael L Hale Level 5

            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 );
            }
            
            1 person found this helpful
            • 3. Re: How to get event target?
              unity3 Level 1

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

              • 4. Re: How to get event target?
                unity3 Level 1

                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.

                • 5. Re: How to get event target?
                  Michael L Hale Level 5

                  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.

                  • 6. Re: How to get event target?
                    unity3 Level 1

                    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

                    • 7. Re: How to get event target?
                      unity3 Level 1

                      And what about this?  From the code you gave me,  [alert(ref.get name());] returns empty if the 'hide' action is generated by the top most layer.  Every subsequent layer works fine!

                      • 8. Re: How to get event target?
                        Michael L Hale Level 5

                        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 );
                        }
                        
                        • 9. Re: How to get event target?
                          unity3 Level 1

                          Thanks again for your helpful information.  I'm going home for the day but do you know if I can manipulate the DOM using JavaScript functions such as [OnClick()]?

                          • 10. Re: How to get event target?
                            Michael L Hale Level 5

                            Not in the way I think you mean. Documents, Layers, etc. don't have onClick events. Some ScriptUI controls have onClick events and you can use the Object Model inside those event handlers.