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

check if layer has mask

New Here ,
Oct 04, 2011 Oct 04, 2011

is it by any means possible to check if a layer has a mask?

i'm using javascript, and tried counting the channels, but it just ignores the mask channel for some reason

this is what i got

var doc = activeDocument;

//Parcours channels = get list of channels

for(a=0;a<app.activeDocument.channels.length;a++)

{

  channelName = doc.channels.name;

  alert(channelName);

};

alert(a);

TOPICS
Actions and scripting
6.5K
Translate
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
Community Expert ,
Oct 04, 2011 Oct 04, 2011

I'm just a script hacker I do not actually know javascript or Adobe Document Object Model.   I just modify code and hack some. I also notice what goes on in Photoshop, In the channels pallette the only time I see Layer mask is when a layer the has a layer mask is the active layer. Other then the Photoshop has a limit of 53 alpha channels Layer mask channels seem to be something else and more or less hidden unless the layer is active.

I notice in Adobe javascript scripting guides a section on Enable-info grammar The <enableinfo> tag provides a boolean expression that, when evaluated, indicates whether the command is enabled in the menu. You can use this expression to enable or disable the menu item based on various characteristics of the document.

The following table show the set of variables you can use in the <enableinfo> expression. The value of these variables is set based on the properties of the active document.

Variable Name Description

PSHOP_HasLayerMask Boolean indicating presence of layer mask.

Hope this help you it over my head..

You may also be able to loop through the artlayers to make each the active layer then check the channels  to see if there is one name "LayerName Mask"...

By the way Vector layer mask are somewhat like raster lay mask they are onle present in the Path palette when a layer with a vector layer mask is the active layer and the path name is "LayerName Vector Mask"

I did not find anything like PSHOP_HasLayerVectorMask so the PSHOP_HasLayerMask Boolean might be used  so your loop man need to check both Channels  and Paths.

JJMack
Translate
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 ,
Oct 04, 2011 Oct 04, 2011

The additional channels and paths produced by layer masks do not reveal themselves to script like they do in the GUI (in italic)… Neither getByName() or collection.length are going to help… The best way would be to use action manager code and get a relevant descriptor… It should be possible using the regular DOM to try change the mask density and catch the error but you would need to consider both vector and regular pixel masks…

Translate
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 ,
Oct 04, 2011 Oct 04, 2011

I found you append very interisting I did not want to test out mask density feel that would be correct for Layer mask don't know about Vector Layer Mask I thought they are 0 or 255.

I wrote a little script to check out the collections.  Turns out the when a layer the has a vector mask is the active layer its vector layer mask shows up in the pathItems collection.

Your right about layer mask they never show up in the channels collection.

var saveactivelayer = activeDocument.activeLayer;

var layers = activeDocument.layers;

for (var i = layers.length-1; i>=0; i--) { // loop through layers

    activeDocument.activeLayer = layers;

    alert("Active Layer = " + activeDocument.activeLayer.name );

    var channels = activeDocument.channels;

    var pathItems = activeDocument.pathItems;

    for (var j = channels.length-1; j>=0; j--) { // loop through channels for each layer

        activeDocument.activeChannel = channels;

        alert("Channel name = " + activeDocument.activeChannel.name );

        }

    if (pathItems != undefined ) {

        for (var k = pathItems.length-1; k>=0; k--) {  // loop through pathItems for each layer

            alert("Path name = " +  pathItems );

            }

        }

    }

activeDocument.activeLayer = saveactivelayer;

JJMack
Translate
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 ,
Oct 04, 2011 Oct 04, 2011

Masks, vector or raster can have an density and feather of their own… As adjusted in the GUI masks window…

Translate
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 ,
Oct 04, 2011 Oct 04, 2011

Here are some functions that Mike has made to test for masks...

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

///////////////////////////////////////////////////////////////////////////////
// Function: hasVectorMask
// Usage: see if there is a vector layer mask
// Input: <none> Must have an open document
// Return: true if there is a vector mask
///////////////////////////////////////////////////////////////////////////////
function hasVectorMask() {
var hasVectorMask = false;
try {
var ref = new ActionReference();
var keyVectorMaskEnabled = app.stringIDToTypeID( 'vectorMask' );
var keyKind = app.charIDToTypeID( 'Knd ' );
ref.putEnumerated( app.charIDToTypeID( 'Path' ), app.charIDToTypeID( 'Ordn' ), keyVectorMaskEnabled );
var desc = executeActionGet( ref );
if ( desc.hasKey( keyKind ) ) {
var kindValue = desc.getEnumerationValue( keyKind );
if (kindValue == keyVectorMaskEnabled) {
hasVectorMask = true;
}
}
}catch(e) {
hasVectorMask = false;
}
return hasVectorMask;
}

///////////////////////////////////////////////////////////////////////////////
// Function: hasFilterMask
// Usage: see if there is a Smart Filter mask
// Input: <none> Must have an open document
// Return: true if there is a Smart Filter mask
///////////////////////////////////////////////////////////////////////////////
function hasFilterMask() {
var hasFilterMask = false;
try {
var ref = new ActionReference();
var keyFilterMask = app.stringIDToTypeID("hasFilterMask");
ref.putProperty( app.charIDToTypeID( 'Prpr' ), keyFilterMask);
ref.putEnumerated( app.charIDToTypeID( 'Lyr ' ), app.charIDToTypeID( 'Ordn' ), app.charIDToTypeID( 'Trgt' ) );
var desc = executeActionGet( ref );
if ( desc.hasKey( keyFilterMask ) && desc.getBoolean( keyFilterMask )) {
hasFilterMask = true;
}
}catch(e) {
hasFilterMask = false;
}
return hasFilterMask;
}

Translate
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 ,
Oct 04, 2011 Oct 04, 2011

Paul, you are just too quick… Here's my quick hash trying to use the DOM… The scripting guide is wrong mask density is 0-100 NOT 0-250 thats a C&P error from feather… Anyhows…

#target photoshop

var doc = app.activeDocument;

for ( var i = 0; i < doc.layers.length; i++ ) {

 

          doc.activeLayer = doc.layers;

 

          alert( doc.layers.name+' has mask = '+hasMask() );

 

          alert( doc.layers.name+' has vector mask = '+hasVectorMask() );

 

};

function hasMask() {

          var lm = true, pmd;

          try {

                    pmd = doc.activeLayer.layerMaskDensity;

                    doc.activeLayer.layerMaskDensity = 50.0;

                    doc.activeLayer.layerMaskDensity = pmd;

          } catch(e) { lm = false };

          return lm;

};

function hasVectorMask() {

          var lvm = true, pmd;

          try {

                    pmd = doc.activeLayer.vectorMaskDensity;

                    doc.activeLayer.vectorMaskDensity = 50.0;

                    doc.activeLayer.vectorMaskDensity = pmd;

          } catch(e) { lvm = false };

          return lvm;

};

Translate
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 ,
Oct 05, 2011 Oct 05, 2011

Yet another way, these function require CS4 or higher and unique layer names.

function hasChannelMaskByName( layerName ){

    var ref = new ActionReference();

    ref.putName( charIDToTypeID( "Lyr " ), layerName );

    return executeActionGet( ref ).getBoolean( stringIDToTypeID( 'hasUserMask' ) );

}

function hasVectorMaskByName( layerName ){

    var ref = new ActionReference();

    ref.putName( charIDToTypeID( "Lyr " ), layerName );

    return executeActionGet( ref ).getBoolean( stringIDToTypeID( 'hasVectorMask' ) );

};

Translate
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 ,
Jan 16, 2017 Jan 16, 2017
LATEST

Handling exception when the Vector mask is present but disabled.

function hasVectorMask() { 

  lm = true;

        try { 

                   pmd = doc.activeLayer.vectorMaskDensity; 

                   doc.activeLayer.vectorMaskDensity = 50.0; 

                   doc.activeLayer.vectorMaskDensity = pmd; 

          } catch ( e ) {

  lm = parseMaskError ( e.message ); 

   }; 

  return ( lm );

function parseMaskError ( msg ) {

  if ( msg.indexOf('The command "Set"', 0 ) >= 0 ) return true;  // has disabled Vector Mask

  if ( msg.indexOf('You cannot change', 0 ) >= 0 ) return false; // hasn't Vector Mask

  return false;

}

Translate
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