Skip to main content
jonathankemp
Inspiring
May 22, 2020
Answered

Detect if mask or layer is selected with JS script.

  • May 22, 2020
  • 3 replies
  • 2201 views

Hey there,

So, I know that "app.activeDocument.activeLayer" returns the actual active layer.  Now, I'd like to know how can I do to detect if the layer itself is selected, or if its mask is selected.

 

I'd also like to be able to control that... So if the layer is selected, I'd like a function that select its mask, and the other way around too.

 

Any clue ?

 

Thanks !

This topic has been closed for replies.
Correct answer jazz-y

 

 

s2t = stringIDToTypeID;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('hasUserMask'));
r.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
if (executeActionGet(r).getBoolean(p)) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('name'));
    (r = new ActionReference()).putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    layerName = executeActionGet(r).getString(p);

    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('channelName'));
    r.putEnumerated(s2t("channel"), s2t("ordinal"), s2t("targetEnum"));
    channelName = executeActionGet(r).getString(p);

    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('alphaChannelOptions'));
    r.putEnumerated(s2t("channel"), s2t("ordinal"), s2t("targetEnum"));
    alphaChannel = executeActionGet(r).hasKey(p)

    if (channelName.indexOf(layerName) == 0 && !alphaChannel) {
        var select = confirm('Layer mask selected\nSelect layer?') ? 'RGB' : null
    } else {
        var select = confirm('Layer selecter\nSelect mask?') ? 'mask' : null
    }
    if (select) {
        (r = new ActionReference()).putEnumerated(s2t("channel"), s2t("channel"), s2t(select));
        (d = new ActionDescriptor).putReference(s2t("null"), r);
        executeAction(s2t("select"), d, DialogModes.NO);
    }
}

 

* RGB when selecting a channel means a composite channel (regardless of the current color mode)

 

3 replies

jonathankemp
Inspiring
May 24, 2020

Thanks mate.  This did the trick.  I did have to change the end part a little, since I only wanted a function that would return true or false.  Here is the updated version... for people that might stumble on this page in the future...

 

function layerMaskIsSelected() {
	s2t = stringIDToTypeID;

	(r = new ActionReference()).putProperty(s2t('property'), p = s2t('hasUserMask'));
	r.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
	if (executeActionGet(r).getBoolean(p)) {

	    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('name'));
	    (r = new ActionReference()).putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
	    layerName = executeActionGet(r).getString(p);

	    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('channelName'));
	    r.putEnumerated(s2t("channel"), s2t("ordinal"), s2t("targetEnum"));
	    channelName = executeActionGet(r).getString(p);

	    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('alphaChannelOptions'));
	    r.putEnumerated(s2t("channel"), s2t("ordinal"), s2t("targetEnum"));
	    alphaChannel = executeActionGet(r).hasKey(p);

			return (channelName.indexOf(layerName) == 0 && !alphaChannel);
	}
}
jazz-yCorrect answer
Legend
May 22, 2020

 

 

s2t = stringIDToTypeID;

(r = new ActionReference()).putProperty(s2t('property'), p = s2t('hasUserMask'));
r.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
if (executeActionGet(r).getBoolean(p)) {
    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('name'));
    (r = new ActionReference()).putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
    layerName = executeActionGet(r).getString(p);

    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('channelName'));
    r.putEnumerated(s2t("channel"), s2t("ordinal"), s2t("targetEnum"));
    channelName = executeActionGet(r).getString(p);

    (r = new ActionReference()).putProperty(s2t('property'), p = s2t('alphaChannelOptions'));
    r.putEnumerated(s2t("channel"), s2t("ordinal"), s2t("targetEnum"));
    alphaChannel = executeActionGet(r).hasKey(p)

    if (channelName.indexOf(layerName) == 0 && !alphaChannel) {
        var select = confirm('Layer mask selected\nSelect layer?') ? 'RGB' : null
    } else {
        var select = confirm('Layer selecter\nSelect mask?') ? 'mask' : null
    }
    if (select) {
        (r = new ActionReference()).putEnumerated(s2t("channel"), s2t("channel"), s2t(select));
        (d = new ActionDescriptor).putReference(s2t("null"), r);
        executeAction(s2t("select"), d, DialogModes.NO);
    }
}

 

* RGB when selecting a channel means a composite channel (regardless of the current color mode)

 

JJMack
Community Expert
Community Expert
May 22, 2020

You will need to use Action Manager Code. If you search this forum there are many scripts posted the code you need  to use to see see if a layer has a layer masks is most likely posted in this forum. Here is a script the will create an array of selected layers.  You could use it to reselect the layer before existing your script.

var doc = app.activeDocument;
var layers = doc.layers;

function getSelectedLayers() {
    var resultLayers = new Array();
    try {
        var idGrp = stringIDToTypeID("groupLayersEvent");
        var descGrp = new ActionDescriptor();
        var refGrp = new ActionReference();
        refGrp.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
        descGrp.putReference(charIDToTypeID("null"), refGrp);
        executeAction(idGrp, descGrp, DialogModes.NO);
        for (var i = 0; i < app.activeDocument.activeLayer.layers.length; i++) {
            resultLayers.push(app.activeDocument.activeLayer.layers[i])
        }
        var id8 = charIDToTypeID("slct");
        var desc5 = new ActionDescriptor();
        var id9 = charIDToTypeID("null");
        var ref2 = new ActionReference();
        var id10 = charIDToTypeID("HstS");
        var id11 = charIDToTypeID("Ordn");
        var id12 = charIDToTypeID("Prvs");
        ref2.putEnumerated(id10, id11, id12);
        desc5.putReference(id9, ref2);
        executeAction(id8, desc5, DialogModes.NO);
        return resultLayers;
    } catch (e) {
        alert("No Layer selected or locked layer selected");
        return resultLayers;
    }
}
var selectedLayers = getSelectedLayers();
alert(selectedLayers.length + " layers selected\n" +  selectedLayers.join("\n"));
JJMack