• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Help with "Events Manager" scripting

Engaged ,
Jun 11, 2009 Jun 11, 2009

Copy link to clipboard

Copied

OK. I'm new to using Events Manager and Script Listener so I don't have all the little things down yet.

What I'm trying to do is set up an Events Manager to activate a script when I select a layer. As I have it now, the script activates no matter what I select and I am having trouble limiting it to only activate when I select a layer. The output from ScriptListener was:

var idslct = charIDToTypeID( "slct" );
    var desc10 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref19 = new ActionReference();
        var idLyr = charIDToTypeID( "Lyr " );
        ref19.putName( idLyr, "Face" );
    desc10.putReference( idnull, ref19 );
    var idMkVs = charIDToTypeID( "MkVs" );
    desc10.putBoolean( idMkVs, false );
executeAction( idslct, desc10, DialogModes.NO );

I used slct as the Event Name when creating the new event in the Events Manager. However, this same ID is used when a tool is selected. That isn't a huge problem but for the fact that my whole reason for setting this up is so that when I first select a layer, I want to set up a specific tool preset to use on it. If I then change tools (select another tool) and still have the same layer selected the script will automatically select the initial tool for the layer that is still selected since that layer is still the active layer.

Example: Let's say I have the following layers...

Texture

Heal

Tones

Eyes

Lips

Face

These aren't my normal layers, but as an example when I select the "Heal" layer I would want to start with the healing brush tool. However, sometimes I want to use clone on this layer as well. Using the Event as it is now, Heal would still be the active layer when I make the selection to the clone tool so the script would then change the selected tool back to the healing brush tool.

So, is there any way to limit the selection event ID to only activate when a layer is selected? I notice the line above in bold/red is the Layer event ID. I'm just not sure how to tie this into the Event Manager and/or script.

Thanks for any help.

Mike

TOPICS
Actions and scripting

Views

2.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advisor , Jun 11, 2009 Jun 11, 2009

Here ya go...

//
// SelectLayerHandler.jsx
//

function _selectLayerHandler(desc) {
  // do whatever when a layer is selected
  var doc = app.activeDocument;
  var layer = doc.activeLayer;
  alert("Layer " + layer.name + " selected");
};

cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };

try {
  if (arguments.length >= 2) {
    var desc = arguments[0];
    var event = arguments[1];
    if (event == cTID('slct')) {
      var ref = desc.getReference(cTID('n

...

Votes

Translate

Translate
Adobe
Advisor ,
Jun 11, 2009 Jun 11, 2009

Copy link to clipboard

Copied

Here ya go...

//
// SelectLayerHandler.jsx
//

function _selectLayerHandler(desc) {
  // do whatever when a layer is selected
  var doc = app.activeDocument;
  var layer = doc.activeLayer;
  alert("Layer " + layer.name + " selected");
};

cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };

try {
  if (arguments.length >= 2) {
    var desc = arguments[0];
    var event = arguments[1];
    if (event == cTID('slct')) {
      var ref = desc.getReference(cTID('null'));
      var cls = ref.getDesiredClass();
      if (cls == cTID('Lyr ')) {
        _selectLayerHandler(desc);
      }
    }
  }
} catch (e) {
  alert( "Error: " + e + ":" + e.line );
}

"SelectLayerHandler.jsx";
// EOF

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 11, 2009 Jun 11, 2009

Copy link to clipboard

Copied

This works perfectly. Thank you.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 13, 2010 Jun 13, 2010

Copy link to clipboard

Copied

First, I'm using CS4.

Yeah, I know this is an old thread. I've developed a problem or maybe I've had it all along and just didn't notice. Using Mike's script (modified some for my specific use) it works as advertised. Here's the problem:

When I select a layer, the tool I use on that layer is selected for me just as I wanted the script to do. When I select a tool using the keyboard shortcuts I start to get undesired results. When I select a tool by clicking on it in the tools palette, everything is fine. I'll give examples as that's the easiest way to describe the problem. This problem does not happen every time, but probably about 90% of the time.

I use the following keys for the associated tools:

B-brush tool

C-clone tool

H-healing tool

If I am using the clone tool and want to switch to the healing tool, I would tap "H". When I do this, the healing tool is selected. If I then press the key to resize the healing brush, nothing happens but when I let go of that key Photoshop reverts back to the clone tool. It doesn't matter which key I press, the tool gets reverted back to the previous tool. I could tap the "H" key and then tap it again and it will do this.

If I am using the clone tool and tap "B" I get the brush. I can start painting with the brush and it works. If I then want to resize the brush and press the key to resize, nothing happens but when I let go of that key, the tool is reverted back to the previous tool.

I have 3 Event Listeners active. One when Photoshop starts up, one when a document is opened, and this one when something is selected. When I turn off Event Listener, I never get this problem. When I turn it back on, the problem comes back.

Here is the script. I just added in the "PreviousLayer" code to try to work around this but it still happens. I'm wondering if it may be a problem within the Event Listener for the 'slct' event itself and not due to this script since the code doesn't run if all I do is select a tool without changing layers.

//
// SelectLayerHandler.jsx
//
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var cTool = typeIDToStringID(executeActionGet(ref).getEnumerationType(stringIDToTypeID('tool')));
//alert( cTool );
var doc = app.activeDocument;
var layer = doc.activeLayer;
var HFClone=false;
var PreviousLayer="Background";
var SCRIPTS_FOLDER = new Folder(app.path + '/' + localize("$$$/ScriptingSupport/InstalledScripts=Presets/Scripts"));
self=new File([SCRIPTS_FOLDER + "/Event Scripts Only/LayerSelect.ini"]);
if (self.created) $.evalFile(self);

function saveini() {
        self.open('w');
        self.writeln("HFClone=true;");
        self.writeln("PreviousLayer="+'"'+layer.name+'"');
        self.close();
}
// A list of tools for selectTools() is located at the end of this script file.

function selectTool(tool) {
    var desc9 = new ActionDescriptor();
        var ref7 = new ActionReference();
        ref7.putClass( app.stringIDToTypeID(tool) );
    desc9.putReference( app.charIDToTypeID('null'), ref7 );
    executeAction( app.charIDToTypeID('slct'), desc9, DialogModes.NO );
};

function _selectLayerHandler(desc) {
// do whatever when a layer is selected
    switch (layer.name) {
    case "High Frequency":
    case "Low Frequency":
        /*if (cTool != "cloneStampTool")*/ doAction('Healing','Tools');
//        else doAction('Clone Current','Tools');
        break;
    case "Dodge/Burn":
        selectTool('paintbrushTool');
        doAction('Dodge/Burn','Tools');
        break;
    case "Touchup":
        selectTool('paintbrushTool');
        doAction('Touchup','Tools');
        break;
    case "Bandpass":
    case "Sharpen":
    case "Whiten Eyes":
        doAction('Sharpen','Tools');
        break;
    case "BCI Copyright":
    case "P2P Copyright":
        layer.visible=true;
        doAction('Move','Tools');
        break;
    case "HF Clone":
        doAction('HF Clone Setup','Tools');
        saveini();
    case "Cloning":
    case "LF Clone":
        if (cTool != "cloneStampTool") doAction('Clone Below','Tools');
        break;
    default:
//        doAction('Default Brush','Tools');
        break;
    }
    if (HFClone) {
        doAction('HF Clone Unset','Tools');
        saveini();
    }
    doc.activeLayer=layer;
};


cTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };

if (layer.name!=PreviousLayer) {
    try {
        saveini();
        if (arguments.length >= 2) {
            var desc = arguments[0];
            var event = arguments[1];
            if (event == cTID('slct')) {
                var ref = desc.getReference(cTID('null'));
                var cls = ref.getDesiredClass();
        //        alert(cls+" "+cTID('Msk '));
                if (cls == cTID('Lyr ') || cls == cTID('Chnl')) {
                    _selectLayerHandler(desc);
                }
            }
        }
    }
    catch (e) {
        alert( "Error: " + e + ":" + e.line );
    }
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Jun 13, 2010 Jun 13, 2010

Copy link to clipboard

Copied

I think that your select event handler needs to check what is being selected.

try {
  if (arguments.length >= 2) {
    var desc = arguments[0];
    var event = arguments[1];
    if (event == cTID('slct')) {
      var ref = desc.getReference(cTID('null'));
      var cls = ref.getDesiredClass();
      if (cls == cTID('Lyr ')) {
        _selectLayerHandler(desc);
      }
    }
  }
} catch (e) {
  alert( "Error: " + e + ":" + e.line );
}

Otherwise when you select a tool the select event fires and your script changes the tool basied on the layer's name.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 14, 2010 Jun 14, 2010

Copy link to clipboard

Copied

Thanks, Mike.

The script already does that. I left that part that you wrote intact. I only added an OR to include the layer mask. Here is the part that I have for that. You notice it checks to see if the layer name is the same as the last time something was selected with the if (layer.name != PreviousLayer) block. So if I select something other than a layer, the layer name doesn't change and layer.name != PreviousLayer evaluates to false. Then, if a the layer name has changed there is still the "if(cls == cTID('Lyr ') || cls == cTID('Chnl') block to see if it was a layer or layer mask selected. I only added in the PreviousLayer stuff to try to work around this problem by bypassing the rest of the code.

if (layer.name!=PreviousLayer) {
    try {
        saveini();
       if (arguments.length >= 2) {
            var desc = arguments[0];
           var event = arguments[1];
            if (event == cTID('slct')) {
               var ref = desc.getReference(cTID('null'));
                var cls = ref.getDesiredClass();
        //        alert(cls+" "+cTID('Msk '));
                if (cls == cTID('Lyr ') || cls == cTID('Chnl')) {
                   _selectLayerHandler(desc);
                }
            }
       }
    }
    catch (e) {
        alert( "Error: " + e + ":" + e.line );
    }
}

Also keep in mind that when I click on the tools palette to select the tool instead of using the keyboard, I never have this problem. It only happens when I use the keyboard shortcuts. I've already gone back to Photoshop's defaults but still get the same thing.

This is what is starting to make me think more and more that it is Photoshop's Event Handler itself and not the script. To test this, I commented out the whole file and it still happens, though maybe only about half the time.

I moved the "if (layer.name != PreviousLayer) block to include everything except opening and evaluating the ini file. I'm guessing this is just something I'll have to deal with. I'm curious, though, if this would happen if I set the Event Handler to run an action and have the action call the script. I'll try it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 14, 2010 Jun 14, 2010

Copy link to clipboard

Copied

LATEST

OK. I just tried this by having the Event run an action that calls the script. Now it doesn't do it nearly as often and if I tap the key twice, it usually works like it's supposed to work. I'll just have to train myself to double tap the key for the tool I want. But I'm going to keep looking into this to see if there may be another cause, though I'm leaning away from the script itself as the cause.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines