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

script for mask to layer or group

Enthusiast ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

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..

TOPICS
Actions and scripting , SDK

Views

832
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 2 Correct answers

Community Expert , Jun 29, 2023 Jun 29, 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 );
	ex
...

Votes

Translate
Community Expert , Jun 29, 2023 Jun 29, 2023
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 Hameed

 

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 id
...

Votes

Translate
Adobe
Community Expert ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

@Mohamed Hameed 

 

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;
}

 

 

Votes

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
Community Expert ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

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));

 

Votes

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
Enthusiast ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

@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

Votes

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
Community Expert ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

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 Hameed

 

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.

 

Votes

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
Community Expert ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

quote

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

 

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?

 

Votes

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
Community Expert ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

quote

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

 

Do you mean to select/target the mask channel?

 

Votes

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
Community Expert ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

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);
    }
}

 

 

Votes

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
Enthusiast ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

@Stephen Marsh 

Very beautiful

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

Votes

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
Community Expert ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

quote

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


By @Mohamed Hameed

 

THEN WHY DON'T YOU ANSWER THE QUESTIONS SO THAT I CAN HELP YOU?!

 

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?

 

And if there is an existing mask, do you want to select/target the mask channel?

 

Votes

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
Enthusiast ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

@Stephen Marsh 

Sorry for not answering your question

I want to add a mask to the layer in general, not at the selection

Votes

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
Enthusiast ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

@Stephen Marsh 

I just want add reveal layer mask code

Votes

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
Community Expert ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

quote

@Stephen Marsh 

I just want add reveal layer mask code


By @Mohamed Hameed

 

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?

 

Votes

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
Enthusiast ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

@Stephen Marsh 

I just want a line of code on just this

create a reveal all layer mask

 

Sorry sir if I bothered you

Votes

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
Community Expert ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

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 Hameed

 

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);
}

 

 

Votes

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
Enthusiast ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

@Stephen Marsh 

Thank you very much sir
Really a great effort that deserves thanks and appreciation

Votes

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
Community Expert ,
Jun 29, 2023 Jun 29, 2023

Copy link to clipboard

Copied

LATEST

You're welcome!

Votes

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