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

How to "add layer mask"

New Here ,
Oct 26, 2008 Oct 26, 2008

Copy link to clipboard

Copied

Hi, i readed all JavaScript Reference but now found function to add mask to layer, can anyone have solution to this?

PS I have some solution(i got it through scriptlistener.8bi):
"
var id21 = charIDToTypeID( "Mk " );
var desc4 = new ActionDescriptor();
var id22 = charIDToTypeID( "Nw " );
var id23 = charIDToTypeID( "Chnl" );
desc4.putClass( id22, id23 );
var id24 = charIDToTypeID( "At " );
var ref2 = new ActionReference();
var id25 = charIDToTypeID( "Chnl" );
var id26 = charIDToTypeID( "Chnl" );
var id27 = charIDToTypeID( "Msk " );
ref2.putEnumerated( id25, id26, id27 );
desc4.putReference( id24, ref2 );
var id28 = charIDToTypeID( "Usng" );
var id29 = charIDToTypeID( "UsrM" );
var id30 = charIDToTypeID( "RvlA" );
desc4.putEnumerated( id28, id29, id30 );
executeAction( id21, desc4, DialogModes.NO );
"
Mb there is another solution not such hard to understand and write?
TOPICS
Actions and scripting

Views

10.7K

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
Adobe
Explorer ,
Oct 26, 2008 Oct 26, 2008

Copy link to clipboard

Copied

>
> PS I have some solution(i got it through scriptlistener.8bi):]

This code is the only way to do it.


> Mb there is another solution not such hard to understand and write?

All that can be done is make that code a bit more readable.

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

createLayerMask = function(doc, layer, fromSelection) {
var desc = new ActionDescriptor();
desc.putClass(cTID("Nw "), cTID("Chnl"));
var ref = new ActionReference();
ref.putEnumerated(cTID("Chnl"), cTID("Chnl"), cTID("Msk "));
desc.putReference(cTID("At "), ref);
if (fromSelection == true) {
desc.putEnumerated(cTID("Usng"), cTID("UsrM"), cTID("RvlS"));
} else {
desc.putEnumerated(cTID("Usng"), cTID("UsrM"), cTID("RvlA"));
}
executeAction(cTID("Mk "), desc, DialogModes.NO);
}
};



If you have a lot of code like this, you can also use create symbols like this:

PSClass.Channel = cTID('Chnl');
PSKey.New = cTID('Nw ');
PSEnum.RevealAll = cTID('RvlA');
PSEnum.RevealSelection = cTID('RvlS');

and use them instead.

-X

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
New Here ,
Oct 27, 2008 Oct 27, 2008

Copy link to clipboard

Copied

Thx.

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
Participant ,
Apr 13, 2009 Apr 13, 2009

Copy link to clipboard

Copied

I didn't get the code (Too Messy).

Could anyone elaborate how to create a mask using a script?

Thanks.

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
Valorous Hero ,
Apr 13, 2009 Apr 13, 2009

Copy link to clipboard

Copied

Yet another version....

makeLayerMask('HdSl');

function makeLayerMask(maskType) {
if( maskType == undefined) maskType = 'RvlS' ; //from selection
//requires a selection 'RvlS'  complete mask 'RvlA' otherThanSelection 'HdSl'
    var desc140 = new ActionDescriptor();
    desc140.putClass( charIDToTypeID('Nw  '), charIDToTypeID('Chnl') );
        var ref51 = new ActionReference();
        ref51.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Msk ') );
    desc140.putReference( charIDToTypeID('At  '), ref51 );
    desc140.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('UsrM'), charIDToTypeID(maskType) );
    executeAction( charIDToTypeID('Mk  '), desc140, DialogModes.NO );
}

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
Participant ,
Apr 13, 2009 Apr 13, 2009

Copy link to clipboard

Copied

Could you tell me how do I use it?

Lets say "CurrLay" is my active layer which I want to add a mask to.

How do I do it?

I add your code at the beginning of my script then...

Thanks.

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
Valorous Hero ,
Apr 13, 2009 Apr 13, 2009

Copy link to clipboard

Copied

Once you have your current layer selected you have three options

You just need to call the function. ie:

makeLayerMask('HdSl'); //This needs a selection and that selection will be hidden

makeLayerMask('RvlS'); //This needs a selection and that selection will be shown

makeLayerMask('RvlA'); //This is a complete mask no selection required

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
Participant ,
Apr 13, 2009 Apr 13, 2009

Copy link to clipboard

Copied

Thank you.

I have 2 problems a problem:

  • Is there a way to make a "Complete Hide" mask.
  • It gives me error at the line:
    desc140.putClass (charIDToTypeID ('Nw'), charIDToTypeID ('Chnl'));
    It says IIlegal Argument.

The way I used it, I det my active layer then wrote this:

makeLayerMask ('RvlA');

Thanks.

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 ,
Apr 13, 2009 Apr 13, 2009

Copy link to clipboard

Copied

Use makeLayerMask ("HdAl") with the above function

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
Participant ,
Apr 13, 2009 Apr 13, 2009

Copy link to clipboard

Copied

Michael L Hale wrote:

Use makeLayerMask ("HdAl") with the above function

Thanks, Works great!

Is there a way to make it work on LayerSets?

It seems to work only on layerArts.

Thank you again.

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 ,
Apr 13, 2009 Apr 13, 2009

Copy link to clipboard

Copied

That function works with layersets as well. With either layers or layersets, the layer you want to apply the mask to needs to be the activeLayer.

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
Participant ,
Apr 14, 2009 Apr 14, 2009

Copy link to clipboard

Copied

Well, it doesn't work for me for some reason.

I'm setting a LayerSet as the active layer by:

app.activeDocument.activeLayer = app.activeDocument.layerSets.getByName("LayerSetName");.

I verified it by:

alert (app.activeDocument.activeLayer);

and got the LayerSetName as a result.

Yet just after making it the active layer I used:

makeLayerMask ('HdSl');

Yet, I got an Erros Message the last line of the function:

executeAction (charIDToTypeID('Mk  '), desc140, DialogModes.NO);

It says: "General Photoshop error occoured. This functionality may not be available in this version of Photoshop".


I have CS4.

How can I fix it?

Thanks.

Managed to make it work.

I just forgot the selection, thanks.

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
Participant ,
May 14, 2016 May 14, 2016

Copy link to clipboard

Copied

@Paul_Riggott,

How can get back the "Focus" to the layer itself after creating the mask?

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
Guide ,
May 14, 2016 May 14, 2016

Copy link to clipboard

Copied

This selects the layer.

function selectRGB() {

var desc25 = new ActionDescriptor();

var ref11 = new ActionReference();

ref11.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('RGB ') );

desc25.putReference( charIDToTypeID('null'), ref11 );

desc25.putBoolean( charIDToTypeID('MkVs'), false );

executeAction( charIDToTypeID('slct'), desc25, DialogModes.NO );

};

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
Participant ,
May 14, 2016 May 14, 2016

Copy link to clipboard

Copied

@SuperMerlin,

It worked!
Anyway to switch between the Mask and the RGB for viewing (Like Ctrl Click)?

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
Guide ,
May 14, 2016 May 14, 2016

Copy link to clipboard

Copied

Ctrl \

Ctrl 2

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
Participant ,
May 14, 2016 May 14, 2016

Copy link to clipboard

Copied

Hi,

I meant using a Script.
I managed to create this:

function SwitchToMask(){

  var idSlct = charIDToTypeID( "slct" );

  var idNull = charIDToTypeID( "null" );

  var idChnl = charIDToTypeID( "Chnl" );

  var idChnl = charIDToTypeID( "Chnl" );

  var idMsk = charIDToTypeID( "Msk " );

  var idMkVs = charIDToTypeID( "MkVs" );

  var switchToMaskDescriptor = new ActionDescriptor();

  var actionRef = new ActionReference();

  actionRef.putEnumerated( idChnl, idChnl, idMsk );

  switchToMaskDescriptor.putReference( idNull, actionRef );

  switchToMaskDescriptor.putBoolean( idMkVs, true );

  executeAction( idSlct, switchToMaskDescriptor, DialogModes.NO );

  }

function SwitchToRgb(){

  var idSlct = charIDToTypeID( "slct" );

  var idNull = charIDToTypeID( "null" );

  var idChnl = charIDToTypeID( "Chnl" );

  var idChnl = charIDToTypeID( "Chnl" );

  var idRGB = charIDToTypeID( "RGB " );

  var idMkVs = charIDToTypeID( "MkVs" );

  var switchToRgbDescriptor = new ActionDescriptor();

  var actionRef = new ActionReference();

  actionRef.putEnumerated( idChnl, idChnl, idRGB );

  switchToRgbDescriptor.putReference( idNull, actionRef );

  switchToRgbDescriptor.putBoolean( idMkVs, false );

  executeAction( idSlct, switchToRgbDescriptor, DialogModes.NO );

  }

Maybe it will assist someone.

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
Guide ,
May 14, 2016 May 14, 2016

Copy link to clipboard

Copied

If you want to toggle between you could use this function.

function toggleRGBMask(){

var ref = new ActionReference();

ref.putProperty(charIDToTypeID('Prpr'),stringIDToTypeID('channelName'));

ref.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );

var desc = executeActionGet(ref);

var mask = desc.getString(stringIDToTypeID('channelName'));

if(mask=='RGB') rgbmask = 'Msk ' else rgbmask = 'RGB ';

var desc25 = new ActionDescriptor();

var ref11 = new ActionReference();

ref11.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID(rgbmask) );

desc25.putReference( charIDToTypeID('null'), ref11 );

desc25.putBoolean( charIDToTypeID('MkVs'), false );

executeAction( charIDToTypeID('slct'), desc25, DialogModes.NO );

};

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
Community Beginner ,
Dec 08, 2018 Dec 08, 2018

Copy link to clipboard

Copied

LATEST

Hi! Could someone can help me with this code:

makeLayerMask('HdSl');

function makeLayerMask(maskType) {
if( maskType == undefined) maskType = 'RvlS' ; //from selection
//requires a selection 'RvlS'  complete mask 'RvlA' otherThanSelection 'HdSl'
    var desc140 = new ActionDescriptor();
    desc140.putClass( charIDToTypeID('Nw  '), charIDToTypeID('Chnl') );
        var ref51 = new ActionReference();
        ref51.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Msk ') );
    desc140.putReference( charIDToTypeID('At  '), ref51 );
    desc140.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('UsrM'), charIDToTypeID(maskType) );
    executeAction( charIDToTypeID('Mk  '), desc140, DialogModes.NO );
}

Well in that way i can only make mask after select one layer, but i want to make masks for each layer in my psd file.

Thanks for answers in advance!

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
Contributor ,
Jun 22, 2009 Jun 22, 2009

Copy link to clipboard

Copied

Hi Paul and others,

I'm trying to add your code snippet to my Applescript because there is no other way in Photoshop to work with layer masks.

Your Javascript works perfectly except when the current layer has already a mask.

In this case, a general error 8800 occurs and no matter if I insert the Javascript in a try statement.

Is there a way to test if a mask exists for the current layer and then delete it?

Thank you for your help.

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
Valorous Hero ,
Jun 22, 2009 Jun 22, 2009

Copy link to clipboard

Copied

Here you are...

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

function deleteMask() {
    var desc523 = new ActionDescriptor();
        var ref325 = new ActionReference();
        ref325.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
    desc523.putReference( charIDToTypeID('null'), ref325 );
    executeAction( charIDToTypeID('Dlt '), desc523, DialogModes.NO );
};

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
Contributor ,
Jun 22, 2009 Jun 22, 2009

Copy link to clipboard

Copied

Paul,

Your code helps me a lot.

At last, I'm able to create layer masks the way I wanted to.

Thank you again.

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