Skip to main content
Mohamed Hameed21513110
Inspiring
June 29, 2023
Answered

script for mask to layer or group

  • June 29, 2023
  • 3 replies
  • 1405 views

Greetings to all
I want a script that creates a layer mask
In the event that the layer already has a mask, it will go to the mask

 

I mean, a script that checks the layer if it does not have a mask, it makes a mask for it, and if there is a mask, it goes directly to it

 

- Note if possible
I want it to be done on the layers and also the group

 

Thank you for your interest and good work..

This topic has been closed for replies.
Correct answer Stephen Marsh

@Stephen Marsh 

I just want a line of code on just this

create a reveal all layer mask

 

Sorry sir if I bothered you


quote

@Stephen Marsh 

I just want a line of code on just this

create a reveal all layer mask

 

Sorry sir if I bothered you


By @Mohamed Hameed21513110

 

Well, that does not appear to be what you asked earlier when conditionals were mentioned.

 

The following function will add a reveal all layer mask to an active layer (with no error checks or conditions).

 

addMask("RvlA"); // or "HdAl"

function addMask(revealOrHide) {
    var idMk = charIDToTypeID("Mk  ");
    var desc2 = new ActionDescriptor();
    var idNw = charIDToTypeID("Nw  ");
    var idChnl = charIDToTypeID("Chnl");
    desc2.putClass(idNw, idChnl);
    var idAt = charIDToTypeID("At  ");
    var ref1 = new ActionReference();
    var idChnl = charIDToTypeID("Chnl");
    var idChnl = charIDToTypeID("Chnl");
    var idMsk = charIDToTypeID("Msk ");
    ref1.putEnumerated(idChnl, idChnl, idMsk);
    desc2.putReference(idAt, ref1);
    var idUsng = charIDToTypeID("Usng");
    var idUsrM = charIDToTypeID("UsrM");
    var idHideOrReveal = charIDToTypeID(revealOrHide); 
    desc2.putEnumerated(idUsng, idUsrM, idHideOrReveal);
    executeAction(idMk, desc2, DialogModes.NO);
}

 

or

 

addMask("revealAll"); // or "hideAll"

function addMask(revealOrHide) {
	function c2t(s) {
        return app.charIDToTypeID(s);
    }
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	descriptor.putClass(s2t("new"), s2t("channel"));
	reference.putEnumerated(s2t("channel"), s2t("channel"), s2t("mask"));
	descriptor.putReference(s2t("at"), reference);
	descriptor.putEnumerated(s2t("using"), c2t("UsrM"), s2t(revealOrHide)); 
	executeAction(s2t("make"), descriptor, DialogModes.NO);
}

 

 

3 replies

Stephen Marsh
Community Expert
Community Expert
June 30, 2023

To select the mask channel (or composite channel):

 

selectLayerCompositeChannel("mask");

function selectLayerCompositeChannel(chanPara) {
    // "RGB" | "mask"
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putEnumerated( s2t( "channel" ), s2t( "channel" ), s2t( chanPara ));
	descriptor.putReference( s2t( "null" ), reference );
	descriptor.putBoolean( s2t( "makeVisible" ), false );
	executeAction(s2t( "select" ), descriptor, DialogModes.NO);
}

 

To toggle between the composite and mask channels:

 

/* 
Based on a script by jazz-y
https://community.adobe.com/t5/photoshop-ecosystem-discussions/detect-if-mask-or-layer-is-selected-with-js-script/m-p/11153109
*/

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
        var select = 'RGB';
    } else {
        // var select = confirm('Layer selecter\nSelect mask?') ? 'mask' : null
        var select = 'mask';
    }
    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);
    }
}

 

 

Mohamed Hameed21513110
Inspiring
June 30, 2023

@Stephen Marsh 

Very beautiful

I just want a code to make a mask for the layer or the group

Mohamed Hameed21513110
Inspiring
June 30, 2023
quote

@Stephen Marsh 

I just want add reveal layer mask code


By @Mohamed Hameed21513110

 

OK, so presuming that there is an active layer:

 

1) Check if there is a layer mask

 

2) If there is a mask, select/target/activate the mask channel (not the composite RGB channel)

 

3) If there is no mask, create a reveal all layer mask and select/target/activate the mask channel (not the composite RGB channel)

 

Is that correct?

 


@Stephen Marsh 

I just want a line of code on just this

create a reveal all layer mask

 

Sorry sir if I bothered you

Stephen Marsh
Community Expert
Community Expert
June 30, 2023

To check if a mask is active:

 

/*
https://community.adobe.com/t5/photoshop-ecosystem-discussions/view-layer-mask-status-via-scripting/td-p/13278583
by jazz-y
*/
#target photoshop
s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('userMaskEnabled'));
r.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
if (executeActionGet(r).hasKey(p)) alert (executeActionGet(r).getBoolean(p));

 

Mohamed Hameed21513110
Inspiring
June 30, 2023

@Stephen Marsh 

Thank you, sir, for your interest and help

But I tried the code, it only checks the layer and does not do any other action


I want, in the absence of a mask, to create a mask for the specified layer or group

And if there is a mask, it will go to the mask directly


I want, in the absence of a mask, to create a mask for the specified layer or group

And if there is a mask, it will go to the mask directly

Stephen Marsh
Community Expert
Community Expert
June 30, 2023
quote

@Stephen Marsh 

Thank you, sir, for your interest and help

But I tried the code, it only checks the layer and does not do any other action


By @Mohamed Hameed21513110

 

That is correct, I only provided related resources, not full working code.

 

I have updated my original post asking further questions as it is not crystal clear what you are requesting.

 

Stephen Marsh
Community Expert
Community Expert
June 30, 2023

@Mohamed Hameed21513110 

 

I am presuming a raster mask, not vector.

 

Do you mean that an existing mask would be added to, or that an existing mask would be replaced?

 

What is the source of the mask?

 

Is there a selection?

 

If no selection, is it just a simple reveal all or hide all mask?

 

Some resources for you to check if a layer has a mask:

 

 

/* 
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-select-the-mask-of-a-layer-through-scripting-and-save-it-as-a-new-layer/td-p/13380033
c.pfaffenbichler
*/

alert(hasLayerMask());

// from discussions with Mike Hale
function hasLayerMask () {
  var ref = new ActionReference();
  ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
  var desc = executeActionGet(ref);
  return desc.hasKey(charIDToTypeID("UsrM"));
};

 

or

 

if (hasLayerMask() == true) {
	alert("true");
} else {
	alert("false");
}

///////////////////////////////////////////////////////////////////////////////
// Function: hasLayerMask
// Usage: see if there is a raster layer mask
// Input: <none> Must have an open document
// Return: true if there is a vector mask
///////////////////////////////////////////////////////////////////////////////
function hasLayerMask() {
	var hasLayerMask = false;
	try {
		var ref = new ActionReference();
		var keyUserMaskEnabled = app.charIDToTypeID( 'UsrM' );
		ref.putProperty( app.charIDToTypeID( 'Prpr' ), keyUserMaskEnabled );
		ref.putEnumerated( app.charIDToTypeID( 'Lyr ' ), app.charIDToTypeID( 'Ordn' ), app.charIDToTypeID( 'Trgt' ) );
		var desc = executeActionGet( ref );
		if ( desc.hasKey( keyUserMaskEnabled ) ) {
			hasLayerMask = true;
		}
	}catch(e) {
		hasLayerMask = false;
	}
	return hasLayerMask;
}