Skip to main content
Participant
November 29, 2022
Answered

How to select the mask of a layer through scripting and save it as a new layer

  • November 29, 2022
  • 4 replies
  • 5405 views

Hello, I am using Photoshop's extendscript to try and get the mask of a layer and then save the mask as a new layer (the black/white portions).

 

I used `activeDocument.layers` to get my layers collection just fine, then I used `activeDocument.channels` to get the mask channel, but it only shows the default 3 RGB channels only and not the mask channels.

 

If I duplicate the default mask, it appears in `activeDocument.channels` but it won't show there otherwise.

How can I programmatically get ahold of the mask of a layer? Any help would be appreciated.

This topic has been closed for replies.
Correct answer c.pfaffenbichler

I feel ashamed to ask again, but is it also possible to check if a layer mask even exists?

I want to stop the script from working if a layer is selected but that does not have a mask to it


You could integrate a check via an if-clause. 

alert (hasLayerMask());
// from discussions with Mike Hale
function hasLayerMask () {
  var ref = new ActionReference();
  ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
  var desc = executeActionGet(ref);
  return desc.hasKey(charIDToTypeID("UsrM"));
  };

4 replies

Stephen Marsh
Community Expert
Community Expert
February 28, 2023

@Johansse – where did you get the code for:

 

.mask

 

I ask because there is no such thing and your code just adds a new blank layer.

 

The link is not on topic and appears to be spam.

Stephen Marsh
Community Expert
Community Expert
February 28, 2023

Ah, it appears that a moderator has removed the entire post, not just the spam link! Nothing to see here, move along...

c.pfaffenbichler
Community Expert
Community Expert
February 28, 2023

Copy/pasting in Scripts is often inefficient compared to more direct methods. 

Stephen Marsh
Community Expert
Community Expert
November 29, 2022

@Dida5FDC wrote:

Hello, I am using Photoshop's extendscript to try and get the mask of a layer and then save the mask as a new layer (the black/white portions).

 

 

@Dida5FDC 

 

Try this code to achieve the overall goal as I understand it:

 

// Select the layer with the mask and then run this script

#target photoshop

var sourceLayer = activeDocument.activeLayer.name;
activeDocument.artLayers.add();
activeDocument.activeLayer.name = "Mask from " + sourceLayer;
applyImage();

function applyImage() {
	function s2t(s) {
        return app.stringIDToTypeID(s);
    }
	var descriptor = new ActionDescriptor();
	var descriptor2 = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putEnumerated( s2t( "channel" ), s2t( "channel" ), s2t( "mask" ));
	reference.putName( s2t( "layer" ), sourceLayer );
	descriptor2.putReference( s2t( "to" ), reference );
	descriptor.putObject( s2t( "with" ), s2t( "calculation" ), descriptor2 );
	executeAction( s2t( "applyImageEvent" ), descriptor, DialogModes.NO );
}

 

Stephen Marsh
Community Expert
Community Expert
November 29, 2022
quote

How can I programmatically get ahold of the mask of a layer? Any help would be appreciated.

 

 

To answer this specific portion of the overall question, presuming that the target layer is active:

 

selectLayerCompositeOrMaskChannel("mask");

function selectLayerCompositeOrMaskChannel(chanPara) {
    // Use either "RGB" | "mask"
	var s2t = function (s) {
		return app.stringIDToTypeID(s);
	};
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	reference.putEnumerated( s2t( "channel" ), s2t( "channel" ), s2t( chanPara ));
	descriptor.putReference( s2t( "null" ), reference );
	descriptor.putBoolean( s2t( "makeVisible" ), false );
	executeAction(s2t( "select" ), descriptor, DialogModes.NO);
}

 

However, as my previous post shows, doing this via the apply image command has many benefits.

Dida5FDCAuthor
Participant
November 30, 2022

I feel ashamed to ask again, but is it also possible to check if a layer mask even exists?

I want to stop the script from working if a layer is selected but that does not have a mask to it

c.pfaffenbichler
Community Expert
Community Expert
November 29, 2022

What do you mean exactly? 

Do you want a Layer with the grayscale Layer Mask as RGB-content? 

 

DOM-code is limited (and often slower) and you may have to use AM-code, which you can record with ScriptingListener-plugin. 

Dida5FDCAuthor
Participant
November 30, 2022

Thank you, Stephen's answer below seems to do it, ultimate I'll end up using AM-code instead of DOM