Skip to main content
Participant
November 27, 2020
Question

How to Load channel as selection, add it as a layer mask and save layer as PNG with the mask applied

  • November 27, 2020
  • 1 reply
  • 1162 views

So my PSD files have multiple layers, and some of them have a channel with a alpha mask, that needs to be applied to them before saving.

My script is at the point, that it's rasterizing all the layers in the beginning and later, in a loop, it's going through all the layers that should be saved. Before saving I'm checking if the current layer has a channel (I can figure that out by their names), that needs to be applied as a layer mask to the current layer:

 

My problem at the moment: How can I add the channel as a layer mask? 

I already tried to record the action with ScriptingListener and got this Code:

Before I call this function, I thought it would be enough to just load the layer to the selection like so:

 

var alphaChannel = activeDocument.channels.getByName(layer.name + "_alpha");
activeDocument.selection.load(alphaChannel);
 

 

function MaskToLayer() {
	var idMk = charIDToTypeID( "Mk  " );
	var desc11 = new ActionDescriptor();
	var idNw = charIDToTypeID( "Nw  " );
	var idChnl = charIDToTypeID( "Chnl" );
	desc11.putClass( idNw, idChnl );
	var idAt = charIDToTypeID( "At  " );
	var ref3 = new ActionReference();
	var idChnl = charIDToTypeID( "Chnl" );
	var idChnl = charIDToTypeID( "Chnl" );
	var idMsk = charIDToTypeID( "Msk " );
	ref3.putEnumerated( idChnl, idChnl, idMsk );
	desc11.putReference( idAt, ref3 );
	var idUsng = charIDToTypeID( "Usng" );
	var idUsrM = charIDToTypeID( "UsrM" );
	var idRvlA = charIDToTypeID( "RvlA" );
	desc11.putEnumerated( idUsng, idUsrM, idRvlA );
	executeAction( idMk, desc11, DialogModes.NO );
}

 

This topic has been closed for replies.

1 reply

Stephen Marsh
Community Expert
Community Expert
November 27, 2020

The following Clean SL code to load a selection from a channel named "Alpha 1" and create a layer mask (presumes that the appropriate layer is targeted).

 

 

selectionFromAlpha1();
function selectionFromAlpha1() {
	var c2t = function (s) {
		return app.charIDToTypeID(s);
	};

	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};

	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	var reference2 = new ActionReference();

	reference.putProperty( s2t( "channel" ), s2t( "selection" ));
	descriptor.putReference( c2t( "null" ), reference );
	reference2.putName( s2t( "channel" ), "Alpha 1" ); // Alpha channel name
	descriptor.putReference( s2t( "to" ), reference2 );
	executeAction( s2t( "set" ), descriptor, DialogModes.NO );
}

layerMaskFromSelection();
function layerMaskFromSelection() {
	var c2t = function (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( "revealSelection" ));
	executeAction( s2t( "make" ), descriptor, DialogModes.NO );
}

 

 

Or perhaps load the active layer's transparency channel as a selection.

Lara5FB5Author
Participant
November 27, 2020

Hey, this is great, thanks for your fast reply! 🙂 

Somehow I seem to have problems with my saving function😅

I just want to do same thing in my code equivalent to the: "right click on layer -> "export as" -> png -> and then just export it" -in photoshop. 
I thought this would do the trick, but well, it does not:

var options = new ExportOptionsSaveForWeb();
options.format = SaveDocumentType.PNG;
options.PNG8 = true;
doc.exportDocument(file, ExportType.SAVEFORWEB, options);

  

Stephen Marsh
Community Expert
Community Expert
November 27, 2020

It would probably help to see the full code...