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

New Here ,
Nov 26, 2020

Copy link to clipboard

Copied

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

 

TOPICS
Actions and scripting, How to, Problem or error, Windows

Views

100

Likes

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

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

New Here ,
Nov 26, 2020

Copy link to clipboard

Copied

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

 

TOPICS
Actions and scripting, How to, Problem or error, Windows

Views

101

Likes

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
Nov 26, 2020 0
Adobe Community Professional ,
Nov 27, 2020

Copy link to clipboard

Copied

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.

Likes

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
Reply
Loading...
Nov 27, 2020 1
New Here ,
Nov 27, 2020

Copy link to clipboard

Copied

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);

  

Likes

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
Reply
Loading...
Nov 27, 2020 0
Adobe Community Professional ,
Nov 27, 2020

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
Nov 27, 2020 0
Lara5FB5 LATEST
New Here ,
Dec 03, 2020

Copy link to clipboard

Copied

Hey! Nevermind my save function, I changend something in my PSD files and it works again.

But now I need some help with applying an existing layer mask to the current layer, again.

All I want to do is this:Screenshot (118)_LI.jpg

I tried to record the action with ScriptingListener but it just does not work.

Likes

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
Reply
Loading...
Dec 03, 2020 0