Answered
// create png of layer mask of active layer;
// 2025, use it at your own risk;
main();
function main() {
if (app.documents.length > 0) {
if (hasLayerMask () == true) {
//app.togglePalettes();
var myDocument = app.activeDocument;
var theName = myDocument.name.replace(/.psd|.psb/, "");
try {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}
catch (e) {var basename = theName};
try {
var docPath = myDocument.path;
}
catch (e) {
var basename = myDocument.name;
var docPath = "~/Desktop"
};
var theId = getLayerId ();
layerMaskCalculationNewFile (theId);
var theCopy = app.activeDocument;
convertToGrayscale ();
//theCopy.convertProfile ("sRGB IEC61966-2.1", Intent.RELATIVECOLORIMETRIC, true, true);
if (File (docPath+"/"+basename+"_"+myDocument.activeLayer.name+".png").exists == false) {
savePNG (docPath, basename, myDocument.activeLayer.name);
theCopy.close(SaveOptions.DONOTSAVECHANGES)
}
else {alert ("file of that name already exists")};
}
}
};
////// channel calculation //////
function layerMaskCalculationNewFile (theId) {
var idchannel = stringIDToTypeID( "channel" );
var idmask = stringIDToTypeID( "mask" );
var idlayer = stringIDToTypeID( "layer" );
var iddocument = stringIDToTypeID( "document" );
var desc218 = new ActionDescriptor();
desc218.putClass( stringIDToTypeID( "new" ), iddocument );
var desc219 = new ActionDescriptor();
var ref1 = new ActionReference();
ref1.putEnumerated( idchannel, idchannel, idmask );
ref1.putIdentifier( idlayer, theId );
ref1.putName( iddocument , app.activeDocument.name );
desc219.putReference( stringIDToTypeID( "to" ), ref1 );
var idsourcetwo = stringIDToTypeID( "source2" );
var ref2 = new ActionReference();
ref2.putEnumerated( idchannel, idchannel, idmask );
ref2.putIdentifier( idlayer, theId );
ref2.putName( iddocument, app.activeDocument.name );
desc219.putReference( idsourcetwo, ref2 );
var idcalculation = stringIDToTypeID( "calculation" );
desc218.putObject( stringIDToTypeID( "using" ), idcalculation, desc219 );
executeAction( stringIDToTypeID( "make" ), desc218, DialogModes.NO );
};
////// has layer mask //////
function hasLayerMask () {
var m_Dsc01, m_Ref01;
m_Ref01 = new ActionReference();
m_Ref01.putEnumerated(stringIDToTypeID("layer"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
m_Dsc01 = executeActionGet(m_Ref01);
return m_Dsc01.hasKey(charIDToTypeID("Usrs"));
};
////// function to png //////
function savePNG (docPath, basename, theSuffix) {
var desc10 = new ActionDescriptor();
var desc11 = new ActionDescriptor();
desc11.putEnumerated( stringIDToTypeID( "method" ), stringIDToTypeID( "PNGMethod" ), stringIDToTypeID( "quick" ) );
desc11.putEnumerated( stringIDToTypeID( "PNGInterlaceType" ), stringIDToTypeID( "PNGInterlaceType" ), stringIDToTypeID( "PNGInterlaceNone" ) );
desc11.putEnumerated( stringIDToTypeID( "PNGFilter" ), stringIDToTypeID( "PNGFilter" ), stringIDToTypeID( "PNGFilterAdaptive" ) );
desc11.putInteger( stringIDToTypeID( "compression" ), 6 );
desc11.putEnumerated( stringIDToTypeID( "embedIccProfileLastState" ), stringIDToTypeID( "embedOff" ), stringIDToTypeID( "embedOff" ) );
var idPNGFormat = stringIDToTypeID( "PNGFormat" );
desc10.putObject( stringIDToTypeID( "as" ), idPNGFormat, desc11 );
desc10.putPath( stringIDToTypeID( "in" ), new File( docPath+"/"+basename+"_"+theSuffix+".png" ) );
desc10.putBoolean( stringIDToTypeID( "copy" ), true );
desc10.putBoolean( stringIDToTypeID( "lowerCase" ), true );
desc10.putBoolean( stringIDToTypeID( "embedProfiles" ), true );
desc10.putEnumerated( stringIDToTypeID( "saveStage" ), stringIDToTypeID( "saveStageType" ), stringIDToTypeID( "saveSucceeded" ) );
executeAction( stringIDToTypeID( "save" ), desc10, DialogModes.NO );
};
////// by mike hale, via paul riggott //////
function getLayerId (){
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
d = executeActionGet(ref);
return d.getInteger(charIDToTypeID('LyrI'));
};
////// convert to graychannel //////
function convertToGrayscale () {
var desc5 = new ActionDescriptor();
desc5.putClass( stringIDToTypeID( "to" ), stringIDToTypeID( "grayscaleMode" ) );
executeAction( stringIDToTypeID( "convertMode" ), desc5, DialogModes.NO );
};Sign up
Already have an account? Login
To post, reply, or follow discussions, please sign in with your Adobe ID.
Sign inSign in to Adobe Community
To post, reply, or follow discussions, please sign in with your Adobe ID.
Sign inEnter your E-mail address. We'll send you an e-mail with instructions to reset your password.


