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 );
}
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 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 );
}
I have a PSD that contains 100s of layers. Most are grouped with individual elements within the groups (nested).
How do I Export to PNG certain individual groups (with nested layers inside of them)?
I don't want to run the Export All Layers to PNG Script, just need to export individual nested groups (layers) to PNG. I've found a few other 3rd party scripts, but they export everything. Only need to export the ones I've selected.
Thanks in advance
This script should do what you want. It works only when you have the group that you want selected (not a layer within that group) I've not extensively tested it and I'm assuming you don't have groups within groups (which becomes a headache)
// export png from group
srcDoc = app.activeDocument;
var allLayers = new Array();
var selectedLayer = srcDoc.activeLayer;
var theLayers = collectAllLayers(app.activeDocument, 0);
var groupName = "";
// function collect all layers
function collectAllLayers (theParent, level)
{
for (var m = theParent.layers.length - 1; m >= 0; m--)
{
var theLayer = theParent.layers[m];
// apply the function to layersets;
if (theLayer.typename == "LayerSet")
{
var goCode = false;
allLayers.push(level + theLayer.name);
groupName = theLayer.name
// alert(level + " " + theLayer.name)
collectAllLayers(theLayer, level + 1)
var subDoc = srcDoc.layers[m];
//alert(subDoc)
var numOfSubLayers = subDoc.layers.length;
//alert(numOfSubLayers)
for (var j = numOfSubLayers -1; j >= 0 ; j--)
{
if (subDoc.layers[j].typename == "ArtLayer")
{
// if the selected (active) layer is in the group
if (theLayer == selectedLayer)
{
var mylayerName = subDoc.layers[j].name
srcDoc.activeLayer = subDoc.layers[j];
// alert("Selected: " + mylayerName + " in " + groupName)
processLayer(mylayerName)
//selectAllLayersInGroup(groupName)
}
}
}
}
}
}
function processLayer(alayername)
{
// duplicate image into new document
// =======================================================
var id2784 = charIDToTypeID( "Mk " );
var desc707 = new ActionDescriptor();
var id2785 = charIDToTypeID( "null" );
var ref508 = new ActionReference();
var id2786 = charIDToTypeID( "Dcmn" );
ref508.putClass( id2786 );
desc707.putReference( id2785, ref508 );
var id2787 = charIDToTypeID( "Nm " );
desc707.putString( id2787, alayername );
var id2788 = charIDToTypeID( "Usng" );
var ref509 = new ActionReference();
var id2789 = charIDToTypeID( "Lyr " );
var id2790 = charIDToTypeID( "Ordn" );
var id2791 = charIDToTypeID( "Trgt" );
ref509.putEnumerated( id2789, id2790, id2791 );
desc707.putReference( id2788, ref509 );
executeAction( id2784, desc707, DialogModes.NO );
// create new layer
var layerRef = app.activeDocument.artLayers.add()
layerRef.name = "temp"
layerRef.blendMode = BlendMode.NORMAL
//merge visible
// =======================================================
var id1435 = charIDToTypeID( "MrgV" );
executeAction( id1435, undefined, DialogModes.NO );
// Set filePath and fileName to source path
filePath = srcDoc.path + '/' + app.activeDocument.name + '.png';
// save out the image
var pngFile = new File(filePath);
pngSaveOptions = new PNGSaveOptions();
pngSaveOptions.embedColorProfile = true;
pngSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
pngSaveOptions.matte = MatteType.NONE; pngSaveOptions.quality = 1;
activeDocument.saveAs(pngFile, pngSaveOptions, false, Extension.LOWERCASE);
// alert(filePath)
// close that save png
app.activeDocument.close()
// selects document that's been open the longest
app.activeDocument = srcDoc;
}