Copy link to clipboard
Copied
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.
@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).
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 applyImag
...
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"));
};
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thank you, Stephen's answer below seems to do it, ultimate I'll end up using AM-code instead of DOM
Copy link to clipboard
Copied
@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).
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 );
}
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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"));
};
Copy link to clipboard
Copied
Additional code options here:
There is also code in a couple of Adobe scripts "Delete All Masks.jsx" and "flatten all layer masks.jsx".
Copy link to clipboard
Copied
You are amazing, it did replicate exactly what I wanted to do!
I kind of ended up understanding how MA scripting works... really appreciate your time!
Copy link to clipboard
Copied
Copy/pasting in Scripts is often inefficient compared to more direct methods.
Copy link to clipboard
Copied
@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.
Copy link to clipboard
Copied
Ah, it appears that a moderator has removed the entire post, not just the spam link! Nothing to see here, move along...