P
P
Plaza2014-07-21 10:50:28
JavaScript
Plaza, 2014-07-21 10:50:28

How to add a mask to each layer in Photoshop Scripts?

Hello.
It is necessary to add an empty mask to each layer of the document (except for the background):
3f0b3bca0b2749368d2d6ed8d23f4e3f.png
There is such a script:

if(app.documents.length>0){
    var docRef = activeDocument;
    var layerNum = docRef.layers.length;
 
    for(var i=0;i<layerNum;i++){
        docRef.activeLayer = docRef.layers[i];
        if(!docRef.activeLayer.isBackgroundLayer){
            try{addMask ()}
            catch(e){}
            }
        }
    }
else{alert('There are no open files')};
 
function addMask(){
    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 idHdAl = charIDToTypeID( "HdAl" );
        desc2.putEnumerated( idUsng, idUsrM, idHdAl );
    executeAction( idMk, desc2, DialogModes.NO );
}

It runs and adds a mask, but it's filled with black:
7dcfa4d0ed8f47a39c9cc0f7056514d2.png
Maybe someone knows how to change the code so that the mask is created empty?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Plaza, 2014-07-21
@Plaza

Figured it out myself.
String:
var idHdAl = charIDToTypeID( "HdAl" );
Replace with:
var idHdAl = charIDToTypeID( "RvlA" );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question