I have an image which includes some pictures (of different sizes here and there) over a solid white background, all in 1 layer.
Can I isolate/extract all the pictures with photoshop ?
Thank you in advance,
Yes and no. Working out where each of the individual images are would be quite difficult (and could involve a slow complex process of scanning each pixel)
But there is a way around it! Without seeing what these images look like, we assume that
The overall image is all one layer.
The individual images are rectangular
The individual images do not contain any white on the border
With that you can select magic wand in the top left, invert the selection and cut the images out.
//select top left hand corner
magicWand(0,0);
// inverse selection
activeDocument.selection.invert();
//copy image
activeDocument.selection.cut();
function magicWand(x,y)
{
var id4109 = charIDToTypeID( "setd" );
var desc623 = new ActionDescriptor();
var id4110 = charIDToTypeID( "null" );
var ref398 = new ActionReference();
var id4111 = charIDToTypeID( "Chnl" );
var id4112 = charIDToTypeID( "fsel" );
ref398.putProperty( id4111, id4112 );
desc623.putReference( id4110, ref398 );
var id4113 = charIDToTypeID( "T " );
var desc624 = new ActionDescriptor();
var id4114 = charIDToTypeID( "Hrzn" );
var id4115 = charIDToTypeID( "#Pxl" );
desc624.putUnitDouble( id4114, id4115, x );
var id4116 = charIDToTypeID( "Vrtc" );
var id4117 = charIDToTypeID( "#Pxl" );
desc624.putUnitDouble( id4116, id4117, y );
var id4118 = charIDToTypeID( "Pnt " );
desc623.putObject( id4113, id4118, desc624 );
var id4119 = charIDToTypeID( "Tlrn" );
desc623.putInteger( id4119, 0 );
executeAction( id4109, desc623, DialogModes.NO );
}
Related
Can anyone help how I can disable or enable the layer mask of the selected layer?
I wanted to automate it via scrip not with action.
I can't provide any code yet since I don't know yet what to do.
Scriptlistener is your friend here.
// Switch off any dialog boxes
displayDialogs = DialogModes.ERROR; // OFF
layermask(true);
alert("Layer mask enabled");
// or
layermask(false);
alert("Layer mask disabled");
// Switch back on any dialog boxes
displayDialogs = DialogModes.ALL; // ON
function layermask(bool)
{
// =======================================================
var idsetd = charIDToTypeID( "setd" );
var desc22 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref5 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref5.putEnumerated( idLyr, idOrdn, idTrgt );
desc22.putReference( idnull, ref5 );
var idT = charIDToTypeID( "T " );
var desc23 = new ActionDescriptor();
var idUsrM = charIDToTypeID( "UsrM" );
desc23.putBoolean( idUsrM, bool ); // value set here
var idLyr = charIDToTypeID( "Lyr " );
desc22.putObject( idT, idLyr, desc23 );
executeAction( idsetd, desc22, DialogModes.NO );
}
Using a bit of Action manager code to get the state of the layer mask we can use:
// Switch off any dialog boxes
displayDialogs = DialogModes.ERROR; // OFF
var layerMaskState = false;
s2t = stringIDToTypeID;
var r = new ActionReference();
r.putProperty(s2t('property'), s2t('userMaskEnabled'));
r.putEnumerated(s2t('layer'), s2t('ordinal'), s2t('targetEnum'));
// Make sure that the object that executeActionGet
// returns contains this key (which means the layer has a mask):
var d = executeActionGet(r);
if (d.hasKey(s2t('userMaskEnabled')))
{
layerMaskState = d.getBoolean(s2t('userMaskEnabled'));
}
// Toggle it!
layerMaskState = !layerMaskState;
layer_mask(layerMaskState);
// Switch off any dialog boxes
displayDialogs = DialogModes.ALL; // OFF
function layer_mask(bool)
{
// =======================================================
var idsetd = charIDToTypeID( "setd" );
var desc22 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref5 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref5.putEnumerated( idLyr, idOrdn, idTrgt );
desc22.putReference( idnull, ref5 );
var idT = charIDToTypeID( "T " );
var desc23 = new ActionDescriptor();
var idUsrM = charIDToTypeID( "UsrM" );
desc23.putBoolean( idUsrM, bool ); // set here
var idLyr = charIDToTypeID( "Lyr " );
desc22.putObject( idT, idLyr, desc23 );
executeAction( idsetd, desc22, DialogModes.NO );
}
I was scratching my head on how to automate and batch process the next.
I want to auto-create many png files, fill the color of the shape and name it from a list.
I made my psd template (rectangle shape),
and have the following txt list (tab separated) +300 items,
containing the color hex value to be filled in, and the name of the image to be created.
+--------+-----------+
| HEX | name |
+--------+-----------+
| 003366 | A1_003366 |
+--------+-----------+
| 9AB0C7 | B1_9AB0C7 |
+--------+-----------+
| FFFFE5 | A2_FFFFE5 |
+--------+-----------+
| F9F9F3 | B2_F9F9F3 |
+--------+-----------+
| DC143C | A3_DC143C |
+--------+-----------+
| 006866 | B3_006866 |
+--------+-----------+
I want my png files named and with their corresponding color. Something like:
A1_003366.png
B1_9AB0C7.png
A2_FFFFE5.png
and so on
I tried to use variables but cannot see how to change the color and the name of each image to be created.
Is this possible with Photoshop?
Thank you so much for your help.
I think you're after a way of changing the colour of a live shape, in which case you can use this scriptlistener code:
var myHex = "003366";
//alert(hex_to_dec(myHex));
fill_rectangle(hex_to_dec(myHex));
function hex_to_dec(hexstring)
{
var hex = hexstring.match(/.{1,2}/g);
return [
parseInt(hex[0], 16),
parseInt(hex[1], 16),
parseInt(hex[2], 16)
]
}
function fill_rectangle(rgb)
{
var red = rgb[0];
var green = rgb[1];
var blue = rgb[2];
// =======================================================
var idsetd = charIDToTypeID( "setd" );
var desc23 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref3 = new ActionReference();
var idcontentLayer = stringIDToTypeID( "contentLayer" );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref3.putEnumerated( idcontentLayer, idOrdn, idTrgt );
desc23.putReference( idnull, ref3 );
var idT = charIDToTypeID( "T " );
var desc24 = new ActionDescriptor();
var idFlCn = charIDToTypeID( "FlCn" );
var desc25 = new ActionDescriptor();
var idClr = charIDToTypeID( "Clr " );
var desc26 = new ActionDescriptor();
var idRd = charIDToTypeID( "Rd " );
desc26.putDouble( idRd, red ); //red
var idGrn = charIDToTypeID( "Grn " );
desc26.putDouble( idGrn, green ); //green
var idBl = charIDToTypeID( "Bl " );
desc26.putDouble( idBl, blue ); //blue
var idRGBC = charIDToTypeID( "RGBC" );
desc25.putObject( idClr, idRGBC, desc26 );
var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
desc24.putObject( idFlCn, idsolidColorLayer, desc25 );
var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
var desc27 = new ActionDescriptor();
var idstrokeStyleVersion = stringIDToTypeID( "strokeStyleVersion" );
desc27.putInteger( idstrokeStyleVersion, 2 );
var idfillEnabled = stringIDToTypeID( "fillEnabled" );
desc27.putBoolean( idfillEnabled, true );
var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
desc24.putObject( idstrokeStyle, idstrokeStyle, desc27 );
var idshapeStyle = stringIDToTypeID( "shapeStyle" );
desc23.putObject( idT, idshapeStyle, desc24 );
executeAction( idsetd, desc23, DialogModes.NO );
}
As show in the image below,
How to get the width, height and rotation angle in Photoshop scripts.
Get Layer width, height and rotation angle
You need two things. First a function to grab the layer as a selection. Secondly, you need to get the layer bounds - from which you can work out the height and width.
var currentLayer = app.activeDocument.activeLayer.name;
var currentLayerSelection = layer_selection();
var lb = get_selection_bounds();
alert(currentLayer + " layer bounds:\n" + lb[0] + ", " + lb[1] + ", " + lb[2] + ", " + lb[3]);
// function GET SELECTION BOUNDS ()
// ----------------------------------------------------------------
function get_selection_bounds()
{
try
{
var x = parseFloat(app.activeDocument.selection.bounds[0]);
var y = parseFloat(app.activeDocument.selection.bounds[1]);
var x1 = parseFloat(app.activeDocument.selection.bounds[2]);
var y1 = parseFloat(app.activeDocument.selection.bounds[3]);
var selW = parseFloat(x1-x);
var selH = parseFloat(y1-y);
// return the results as an array
return [x, y, x1, y1];
}
catch(e)
{
alert("Oops " + e)
}
}
function layer_selection()
{
var id1268 = charIDToTypeID( "setd" );
var desc307 = new ActionDescriptor();
var id1269 = charIDToTypeID( "null" );
var ref257 = new ActionReference();
var id1270 = charIDToTypeID( "Chnl" );
var id1271 = charIDToTypeID( "fsel" );
ref257.putProperty( id1270, id1271 );
desc307.putReference( id1269, ref257 );
var id1272 = charIDToTypeID( "T " );
var ref258 = new ActionReference();
var id1273 = charIDToTypeID( "Chnl" );
var id1274 = charIDToTypeID( "Chnl" );
var id1275 = charIDToTypeID( "Trsp" );
ref258.putEnumerated( id1273, id1274, id1275 );
desc307.putReference( id1272, ref258 );
executeAction( id1268, desc307, DialogModes.NO )
}
You can work out the height and width:
var layerWidth = lb[2] - lb[0];
var layerHeight = lb[3] - lb[1];
alert("Width, height: " + layerWidth + ", " + layerHeight);
As for the angle.. Let's just say it's much easier to do that yourself.
Use the ruler tool - then Photoshop will work out the angle. Look in Image > Image Rotation > Arbitrary and it'll show you the angle to rotate the canvas to get it back to horizontal.
this script create a simple horizontal line from the center of my image on a new layer to the right at 0 degree. I would like to have a dialog box that appear asking me to how much degree I want to rotate that line from the center. A little like a watch.
Can you help me on that please? I really don't know how to do that. I have created that JSX file from Photoshop Script Listener, but when I try to create rotation, it seems that the listener does not work. I use Photoshop CC 2019
tks
Seby
// =======================================================
var idslct = charIDToTypeID( "slct" );
var desc5 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1 = new ActionReference();
var idmoveTool = stringIDToTypeID( "moveTool" );
ref1.putClass( idmoveTool );
desc5.putReference( idnull, ref1 );
var iddontRecord = stringIDToTypeID( "dontRecord" );
desc5.putBoolean( iddontRecord, true );
var idforceNotify = stringIDToTypeID( "forceNotify" );
desc5.putBoolean( idforceNotify, true );
executeAction( idslct, desc5, DialogModes.NO );
// =======================================================
var idslct = charIDToTypeID( "slct" );
var desc6 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref2 = new ActionReference();
var idlineTool = stringIDToTypeID( "lineTool" );
ref2.putClass( idlineTool );
desc6.putReference( idnull, ref2 );
var iddontRecord = stringIDToTypeID( "dontRecord" );
desc6.putBoolean( iddontRecord, true );
var idforceNotify = stringIDToTypeID( "forceNotify" );
desc6.putBoolean( idforceNotify, true );
executeAction( idslct, desc6, DialogModes.NO );
// =======================================================
var idMk = charIDToTypeID( "Mk " );
var desc7 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref3 = new ActionReference();
var idcontentLayer = stringIDToTypeID( "contentLayer" );
ref3.putClass( idcontentLayer );
desc7.putReference( idnull, ref3 );
var idUsng = charIDToTypeID( "Usng" );
var desc8 = new ActionDescriptor();
var idType = charIDToTypeID( "Type" );
var desc9 = new ActionDescriptor();
var idClr = charIDToTypeID( "Clr " );
var desc10 = new ActionDescriptor();
var idRd = charIDToTypeID( "Rd " );
desc10.putDouble( idRd, 0.000000 );
var idGrn = charIDToTypeID( "Grn " );
desc10.putDouble( idGrn, 0.000000 );
var idBl = charIDToTypeID( "Bl " );
desc10.putDouble( idBl, 0.000000 );
var idRGBC = charIDToTypeID( "RGBC" );
desc9.putObject( idClr, idRGBC, desc10 );
var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
desc8.putObject( idType, idsolidColorLayer, desc9 );
var idShp = charIDToTypeID( "Shp " );
var desc11 = new ActionDescriptor();
var idStrt = charIDToTypeID( "Strt" );
var desc12 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idRlt = charIDToTypeID( "#Rlt" );
desc12.putUnitDouble( idHrzn, idRlt, 126.000000 );
var idVrtc = charIDToTypeID( "Vrtc" );
var idRlt = charIDToTypeID( "#Rlt" );
desc12.putUnitDouble( idVrtc, idRlt, 126.000000 );
var idPnt = charIDToTypeID( "Pnt " );
desc11.putObject( idStrt, idPnt, desc12 );
var idEnd = charIDToTypeID( "End " );
var desc13 = new ActionDescriptor();
var idHrzn = charIDToTypeID( "Hrzn" );
var idRlt = charIDToTypeID( "#Rlt" );
desc13.putUnitDouble( idHrzn, idRlt, 247.920000 );
var idVrtc = charIDToTypeID( "Vrtc" );
var idRlt = charIDToTypeID( "#Rlt" );
desc13.putUnitDouble( idVrtc, idRlt, 126.000000 );
var idPnt = charIDToTypeID( "Pnt " );
desc11.putObject( idEnd, idPnt, desc13 );
var idWdth = charIDToTypeID( "Wdth" );
var idPxl = charIDToTypeID( "#Pxl" );
desc11.putUnitDouble( idWdth, idPxl, 6.000000 );
var idLn = charIDToTypeID( "Ln " );
desc8.putObject( idShp, idLn, desc11 );
var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
var desc14 = new ActionDescriptor();
var idstrokeStyleVersion = stringIDToTypeID( "strokeStyleVersion" );
desc14.putInteger( idstrokeStyleVersion, 2 );
var idstrokeEnabled = stringIDToTypeID( "strokeEnabled" );
desc14.putBoolean( idstrokeEnabled, false );
var idfillEnabled = stringIDToTypeID( "fillEnabled" );
desc14.putBoolean( idfillEnabled, true );
var idstrokeStyleLineWidth = stringIDToTypeID( "strokeStyleLineWidth" );
var idPxl = charIDToTypeID( "#Pxl" );
desc14.putUnitDouble( idstrokeStyleLineWidth, idPxl, 1.000000 );
var idstrokeStyleLineDashOffset = stringIDToTypeID( "strokeStyleLineDashOffset" );
var idPnt = charIDToTypeID( "#Pnt" );
desc14.putUnitDouble( idstrokeStyleLineDashOffset, idPnt, 0.000000 );
var idstrokeStyleMiterLimit = stringIDToTypeID( "strokeStyleMiterLimit" );
desc14.putDouble( idstrokeStyleMiterLimit, 100.000000 );
var idstrokeStyleLineCapType = stringIDToTypeID( "strokeStyleLineCapType" );
var idstrokeStyleLineCapType = stringIDToTypeID( "strokeStyleLineCapType" );
var idstrokeStyleButtCap = stringIDToTypeID( "strokeStyleButtCap" );
desc14.putEnumerated( idstrokeStyleLineCapType, idstrokeStyleLineCapType, idstrokeStyleButtCap );
var idstrokeStyleLineJoinType = stringIDToTypeID( "strokeStyleLineJoinType" );
var idstrokeStyleLineJoinType = stringIDToTypeID( "strokeStyleLineJoinType" );
var idstrokeStyleMiterJoin = stringIDToTypeID( "strokeStyleMiterJoin" );
desc14.putEnumerated( idstrokeStyleLineJoinType, idstrokeStyleLineJoinType, idstrokeStyleMiterJoin );
var idstrokeStyleLineAlignment = stringIDToTypeID( "strokeStyleLineAlignment" );
var idstrokeStyleLineAlignment = stringIDToTypeID( "strokeStyleLineAlignment" );
var idstrokeStyleAlignInside = stringIDToTypeID( "strokeStyleAlignInside" );
desc14.putEnumerated( idstrokeStyleLineAlignment, idstrokeStyleLineAlignment, idstrokeStyleAlignInside );
var idstrokeStyleScaleLock = stringIDToTypeID( "strokeStyleScaleLock" );
desc14.putBoolean( idstrokeStyleScaleLock, false );
var idstrokeStyleStrokeAdjust = stringIDToTypeID( "strokeStyleStrokeAdjust" );
desc14.putBoolean( idstrokeStyleStrokeAdjust, false );
var idstrokeStyleLineDashSet = stringIDToTypeID( "strokeStyleLineDashSet" );
var list1 = new ActionList();
desc14.putList( idstrokeStyleLineDashSet, list1 );
var idstrokeStyleBlendMode = stringIDToTypeID( "strokeStyleBlendMode" );
var idBlnM = charIDToTypeID( "BlnM" );
var idNrml = charIDToTypeID( "Nrml" );
desc14.putEnumerated( idstrokeStyleBlendMode, idBlnM, idNrml );
var idstrokeStyleOpacity = stringIDToTypeID( "strokeStyleOpacity" );
var idPrc = charIDToTypeID( "#Prc" );
desc14.putUnitDouble( idstrokeStyleOpacity, idPrc, 100.000000 );
var idstrokeStyleContent = stringIDToTypeID( "strokeStyleContent" );
var desc15 = new ActionDescriptor();
var idClr = charIDToTypeID( "Clr " );
var desc16 = new ActionDescriptor();
var idRd = charIDToTypeID( "Rd " );
desc16.putDouble( idRd, 0.000000 );
var idGrn = charIDToTypeID( "Grn " );
desc16.putDouble( idGrn, 0.000000 );
var idBl = charIDToTypeID( "Bl " );
desc16.putDouble( idBl, 0.000000 );
var idRGBC = charIDToTypeID( "RGBC" );
desc15.putObject( idClr, idRGBC, desc16 );
var idsolidColorLayer = stringIDToTypeID( "solidColorLayer" );
desc14.putObject( idstrokeStyleContent, idsolidColorLayer, desc15 );
var idstrokeStyleResolution = stringIDToTypeID( "strokeStyleResolution" );
desc14.putDouble( idstrokeStyleResolution, 300.000000 );
var idstrokeStyle = stringIDToTypeID( "strokeStyle" );
desc8.putObject( idstrokeStyle, idstrokeStyle, desc14 );
var idcontentLayer = stringIDToTypeID( "contentLayer" );
desc7.putObject( idUsng, idcontentLayer, desc8 );
var idLyrI = charIDToTypeID( "LyrI" );
desc7.putInteger( idLyrI, 126 );
executeAction( idMk, desc7, DialogModes.NO );
You need to learn Photoshop Script UI. Though it is notoriously badly supported by Adobe. There are a few websites that tell you. Do a search for "ScriptUI for Dummies"; It's a good place to start.
Here's a simple slider that will rotate the layer to the value as required. You can also use the transform function taken from Scriptlistener.
Even if you don't know what code to use you can always ask and show an example in pseudo code.
// your code
// do something
//
// dialog
var dlg = new Window("dialog");
dlg.text = "Something";
dlg.preferredSize.width = 360;
dlg.orientation = "column";
dlg.alignChildren = ["center","top"];
dlg.spacing = 10;
dlg.margins = 16;
var slider1 = dlg.add("slider", undefined, undefined, undefined, undefined, {name: "slider1"});
slider1.minvalue = 0;
slider1.maxvalue = 360;
slider1.value = 0;
slider1.preferredSize.width = 300;
var statictext1 = dlg.add("statictext", undefined, undefined, {name: "statictext1"});
statictext1.text = slider1.value;
statictext1.preferredSize.width = 80;
statictext1.justify = "center";
// add buttons
dlg.add ("button", undefined, "OK");
dlg.add ("button", undefined, "Cancel");
// Define behavior for when the slider1 value changes
dlg.slider1.onChanging = function()
{
var valSlider1 = Math.round(dlg.slider1.value);
// Update the label text with the current slider value.
dlg.statictext1.text = valSlider1;
}
// show the dlg;
dlg.center();
var myReturn = dlg.show();
if (myReturn)
{
var myAngle = parseInt(dlg.statictext1.text);
var msg = dlg.statictext1.text + " degrees rotation";
alert(msg);
transform (myAngle);
}
function transform (rot)
{
if (rot == undefined) return;
//transform
// =======================================================
var id769 = charIDToTypeID( "Trnf" );
var desc98 = new ActionDescriptor();
var id770 = charIDToTypeID( "null" );
var ref59 = new ActionReference();
var id771 = charIDToTypeID( "Lyr " );
var id772 = charIDToTypeID( "Ordn" );
var id773 = charIDToTypeID( "Trgt" );
ref59.putEnumerated( id771, id772, id773 );
desc98.putReference( id770, ref59 );
var id774 = charIDToTypeID( "FTcs" );
var id775 = charIDToTypeID( "QCSt" );
var id776 = charIDToTypeID( "Qcsa" );
desc98.putEnumerated( id774, id775, id776 );
var id777 = charIDToTypeID( "Ofst" );
var desc99 = new ActionDescriptor();
var id778 = charIDToTypeID( "Hrzn" );
var id779 = charIDToTypeID( "#Pxl" );
desc99.putUnitDouble( id778, id779, 0); // dX
var id780 = charIDToTypeID( "Vrtc" );
var id781 = charIDToTypeID( "#Pxl" );
desc99.putUnitDouble( id780, id781, 0 ); // dY
var id782 = charIDToTypeID( "Ofst" );
desc98.putObject( id777, id782, desc99 );
var id783 = charIDToTypeID( "Angl" );
var id784 = charIDToTypeID( "#Ang" );
desc98.putUnitDouble( id783, id784, rot );
executeAction( id769, desc98, DialogModes.NO );
}
I need to create a Photoshop script (I'm using Java Script) that takes a few images and applies the same mask to all of them.
(Here is what I mean by applying a mask)
Once I've loaded the images using this code
var sourceImageDoc = app.open(new File("./image.png"))
var maskImageDoc = app.open(new File("./mask.png"))
how I can set maskImageDoc to be the mask for sourceImageDoc?
Here is some snippets out of one of my scripts that works CS3 +. As you can see it uses the ugly script listener code - if you're applying it to a layer rather than a group, you may need to adjust that part? I'm not sure I remember ever using that function on a layer rather than a layerset.
//mask group to paper area
app.activeDocument.activeLayer = lyr;
selectRasterLayerContents();
app.activeDocument.activeLayer = grp;
AddMaskToGroup();
//Selects the contents of the active layer.
function selectRasterLayerContents() {
var id47 = charIDToTypeID( "setd" );
var desc11 = new ActionDescriptor();
var id48 = charIDToTypeID( "null" );
var ref11 = new ActionReference();
var id49 = charIDToTypeID( "Chnl" );
var id50 = charIDToTypeID( "fsel" );
ref11.putProperty( id49, id50 );
desc11.putReference( id48, ref11 );
var id51 = charIDToTypeID( "T " );
var ref12 = new ActionReference();
var id52 = charIDToTypeID( "Chnl" );
var id53 = charIDToTypeID( "Chnl" );
var id54 = charIDToTypeID( "Trsp" );
ref12.putEnumerated( id52, id53, id54 );
desc11.putReference( id51, ref12 );
executeAction( id47, desc11, DialogModes.NO );
}
//adds a mask revealing the selection to the active group
function AddMaskToGroup() {
var id42 = charIDToTypeID( "Mk " );
var desc8 = new ActionDescriptor();
var id43 = charIDToTypeID( "Nw " );
var id44 = charIDToTypeID( "Chnl" );
desc8.putClass( id43, id44 );
var id45 = charIDToTypeID( "At " );
var ref10 = new ActionReference();
var id46 = charIDToTypeID( "Chnl" );
var id47 = charIDToTypeID( "Chnl" );
var id48 = charIDToTypeID( "Msk " );
ref10.putEnumerated( id46, id47, id48 );
desc8.putReference( id45, ref10 );
var id49 = charIDToTypeID( "Usng" );
var id50 = charIDToTypeID( "UsrM" );
var id51 = charIDToTypeID( "RvlS" );
desc8.putEnumerated( id49, id50, id51 );
executeAction( id42, desc8, DialogModes.NO );
}
Applying a layer mask is a pain, because script listener won't hear it directly.
You'll want this then
function applyLayerMask()
{
var id1949 = charIDToTypeID( "Dlt " );
var desc398 = new ActionDescriptor();
var id1950 = charIDToTypeID( "null" );
var ref291 = new ActionReference();
var id1951 = charIDToTypeID( "Chnl" );
var id1952 = charIDToTypeID( "Chnl" );
var id1953 = charIDToTypeID( "Msk " );
ref291.putEnumerated( id1951, id1952, id1953 );
desc398.putReference( id1950, ref291 );
var id1954 = charIDToTypeID( "Aply" );
desc398.putBoolean( id1954, true );
executeAction( id1949, desc398, DialogModes.NO );
}
Here's my code to apply a layer mask to the currently selected object (provided it has a selection).
const layerMask = function () {
const makeID = stringIDToTypeID('make')
const newID = stringIDToTypeID('new')
const channelID = stringIDToTypeID('channel')
const atID = stringIDToTypeID('at')
const usingID = stringIDToTypeID('using')
const userMaskEnabledID = stringIDToTypeID('userMaskEnabled')
const revealSelectionID = stringIDToTypeID('revealSelection')
const actionDesc = new ActionDescriptor()
const actionRef = new ActionReference()
actionDesc.putClass(newID, channelID)
actionRef.putEnumerated(channelID, channelID, maskID)
actionDesc.putReference(atID, actionRef)
actionDesc.putEnumerated(usingID, userMaskEnabledID, revealSelectionID)
executeAction(makeID, actionDesc, DialogModes.NO)
}
Tested in Photoshop CC 2021
I'm late to the party, but here's my simple method for adding a mask to a layer in ExtendScript. The trick here is to place a copy of the mask just below the image, then merge the image into it. Note that the merge will destroy the original layer object and create a new one, so I always assign the result back to my original variable; otherwise it will become undefined.
// imageLayer: an artLayer in imageDoc that I want to mask
// maskLayer: an artLayer in maskDoc w/ just the mask to be added, no image data
// if both are in same file, you may not need the activeDocument assignments
app.activeDocument = maskDoc;
maskLayer.duplicate(imageLayer,ElementPlacement.PLACEAFTER);
app.activeDocument = imageDoc;
imageLayer = imageLayer.merge();
Now, this just adds the mask to the image layer non-destructively (i.e., it can be removed layer if you like). If you want to truly apply the mask (permanently conform the image to the mask while deleting the mask), then just duplicate a completely blank layer (no mask) below the masked merge-result from the first step above, and then merge that masked result one more time down into the blank layer.