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

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 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 , Windows

Views

711

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
community guidelines
Adobe
Community Expert ,
Nov 27, 2020 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.

Votes

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
community guidelines
New Here ,
Nov 27, 2020 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);

  

Votes

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
community guidelines
Community Expert ,
Nov 27, 2020 Nov 27, 2020

Copy link to clipboard

Copied

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

Votes

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
community guidelines
New Here ,
Dec 03, 2020 Dec 03, 2020

Copy link to clipboard

Copied

LATEST

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.

Votes

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
community guidelines