Shift layer in Photoshop up or down - photoshop

Whilst shifting a layer up or down in Photoshop is quite easy - just drag it wherever you want in code it's proving to be a little more difficult:
// Switch off any dialog boxes
displayDialogs = DialogModes.ALL; // OFF
shift_layer(-1);
// Set Display Dialogs back to normal
displayDialogs = DialogModes.ALL; // NORMAL
// function SHIFT LAYER (direction)
// --------------------------------------------------------
function shift_layer(direction)
{
// direction = 1 Moves layer up 1 place to top
// direction = -1 Moves layer down 1 place to background
if(direction == undefined) return -1;
var where = (direction > 0) ? ElementPlacement.PLACEBEFORE : ElementPlacement.PLACEAFTER;
var currentActiveLayer = app.activeDocument.activeLayer;
var idx = get_layer_index(currentActiveLayer);
// Get a reference to the active layer
var layerRef = app.activeDocument.layers[idx-direction];
// Move the new layer set to after the previously first layer
currentActiveLayer.move(layerRef, where);
}
function get_layer_index(ref)
{
var numOfArtLayers = app.activeDocument.layers.length;
// work from the top of the stack down!
for (var i = numOfArtLayers -1; i >= 0; i--)
{
var tempLayer = app.activeDocument.layers[i];
if (tempLayer == ref) return i;
}
}
Whilst this works it does do when there are no groups (layer sets) in the PSD file. I'm stumped how to do this as the layer index (when groups are involved) are somewhat convoluted. it's gett the layer reference that's proving difficult
Assuming that you cannot rely on using layer names and group names.
Any ideas?
I've had a look at using scriptlistener code (thanks Sergey Kritskiy)
// =======================================================
var idmove = charIDToTypeID( "move" );
var desc23 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref15 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref15.putEnumerated( idLyr, idOrdn, idTrgt );
desc23.putReference( idnull, ref15 );
var idT = charIDToTypeID( "T " );
var ref16 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
ref16.putIndex( idLyr, 2 );
desc23.putReference( idT, ref16 );
var idAdjs = charIDToTypeID( "Adjs" );
desc23.putBoolean( idAdjs, false );
var idVrsn = charIDToTypeID( "Vrsn" );
desc23.putInteger( idVrsn, 5 );
var idLyrI = charIDToTypeID( "LyrI" );
var list9 = new ActionList();
list9.putInteger( 3 );
desc23.putList( idLyrI, list9 );
executeAction( idmove, desc23, DialogModes.NO );
Which doesn't really get us anywhere. But if we compare it to moving a layer up and then turning it into a function there are three values that seem to control the outcome. What they do? Nobody knows.
function shift(a,b,c)
{
var idmove = charIDToTypeID( "move" );
var desc23 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref15 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref15.putEnumerated( idLyr, idOrdn, idTrgt );
desc23.putReference( idnull, ref15 );
var idT = charIDToTypeID( "T " );
var ref16 = new ActionReference();
var idLyr = charIDToTypeID( "Lyr " );
ref16.putIndex( idLyr, a ); //2
desc23.putReference( idT, ref16 );
var idAdjs = charIDToTypeID( "Adjs" );
desc23.putBoolean( idAdjs, false );
var idVrsn = charIDToTypeID( "Vrsn" );
desc23.putInteger( idVrsn, b ); //2
var idLyrI = charIDToTypeID( "LyrI" );
var list11 = new ActionList();
list11.putInteger( c ); //3
desc23.putList( idLyrI, list11 );
executeAction( idmove, desc23, DialogModes.NO );
}
These where my results:
// Original layer order 0,1,2,3,4,5,6
// shift_down(1, 1, 1) // 3, 0, 1, 2, 4, 5, 6
// shift_down(1, 2, 2) // 3, 0, 1, 2, 4, 5, 6
// shift_down(2, 1, 1) // 0, 3, 1, 2, 4, 5, 6
// shift_down(3, 1, 1) // 0, 1, 3, 2, 4, 5, 6
// shift_down(3, 0, 0) // 0, 1, 3, 2, 4, 5, 6
// shift_down(3, 0, 1) // 0, 1, 3, 2, 4, 5, 6
// shift_down(3, 1, 0) // 0, 1, 3, 2, 4, 5, 6
// shift_down(4, 0, 0) // 0, 1, 2, 3, 4, 5, 6 // does nothing
// shift_down(5, 0, 0) // 0, 1, 2, 3, 4, 5, 6 // does nothing
// shift_down(4, 1, 1) // 0, 1, 2, 3, 4, 5, 6 // does nothing
// shift_down(4, 0, 4) // 0, 1, 2, 3, 4, 5, 6 // does nothing
// shift_down(4, 4, 4) // 0, 1, 2, 3, 4, 5, 6 // does nothing
// shift_down(5, 5, 4) // 0, 1, 2, 3, 4, 5, 6 // does nothing
// shift_down(6, 5, 4) // 0, 1, 2, 4, 3, 5, 6 // Move up
When moving down, the first parameter seems to be the layer index.
However in order to move up, that parameter needs to be 6.
Confused :(

Related

Enable or disable the selected layer mask

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 );
}

Photoshop script to get image layer transform infomation

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.

how to create a Photoshop Script dialog box for rotation on 360 on layer

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 );
}

Photoshop Scripting (JSX): How to contract a layerSet?

I searched everywhere by I'm unable to find the syntax/property to have a layerSet closed with a script.
I have this, my layerSet is expanded: http://i.imgur.com/uVBiPa4.jpg
v Group 1
- Layer 1
- Layer 2
I'd like this, where my layerSet is closed: http://i.imgur.com/j6BVyYu.jpg
> Group 1
At first, I was trying some things, as I know the layerSet name, but nothing ever happens.
doc.layerSets.getByName('Group 1').groupContract = true;
I tried with this other one, but it must not be the right one either.
doc.layerSets.getByName('Group 1').groupExpand = false;
If anyone has an idea...
Found my answer there, from JJMack:
https://forums.adobe.com/thread/1315645?start=0&tstart=0
It's a bit tricky, but it does the job.
Here's a simplified version:
//For code readability
function cTID(s){return charIDToTypeID(s)}
function sTID(s){return stringIDToTypeID(s)}
// =============================
function closeAllLayerSets(ref) {
var layers = ref.layers;
var len = layers.length;
for ( var i = 0; i < len; i ++) {
var layer = layers[i];
if (layer.typename == 'LayerSet') {app.activeDocument.activeLayer = layer; closeGroup(layer); var layer = layers[i]; closeAllLayerSets(layer);};
}
}
function hasLayerMask() {
var m_Ref01 = new ActionReference();
m_Ref01.putEnumerated( sTID( "layer" ), cTID( "Ordn" ), cTID( "Trgt" ));
var m_Dsc01= executeActionGet( m_Ref01 );
return m_Dsc01.hasKey(cTID('Usrs'));
}
function addLayer() {
var m_ActiveLayer = activeDocument.activeLayer;
var m_NewLayer = activeDocument.artLayers.add();
m_NewLayer.move(m_ActiveLayer, ElementPlacement.PLACEBEFORE);
return m_NewLayer;
}
function addToSelection(layerName) {
var m_Dsc01 = new ActionDescriptor();
var m_Ref01 = new ActionReference();
m_Ref01.putName( cTID( "Lyr " ), layerName );
m_Dsc01.putReference( cTID( "null" ), m_Ref01 );
m_Dsc01.putEnumerated( sTID( "selectionModifier" ), sTID( "selectionModifierType" ), sTID( "addToSelection" ) );
m_Dsc01.putBoolean( cTID( "MkVs" ), false );
try {
executeAction( cTID( "slct" ), m_Dsc01, DialogModes.NO );
} catch(e) {}
}
function groupSelected(name) {
var m_Dsc01 = new ActionDescriptor();
var m_Ref01 = new ActionReference();
m_Ref01.putClass( sTID( "layerSection" ) );
m_Dsc01.putReference( cTID( "null" ), m_Ref01 );
var m_Ref02 = new ActionReference();
m_Ref02.putEnumerated( cTID( "Lyr " ), cTID( "Ordn" ), cTID( "Trgt" ) );
m_Dsc01.putReference( cTID( "From" ), m_Ref02 );
var m_Dsc02 = new ActionDescriptor();
m_Dsc02.putString( cTID( "Nm " ), name);
m_Dsc01.putObject( cTID( "Usng" ), sTID( "layerSection" ), m_Dsc02 );
executeAction( cTID( "Mk " ), m_Dsc01, DialogModes.NO );
return activeDocument.activeLayer;
}
function closeGroup(layerSet) {
var m_Name = layerSet.name;
var m_Opacity = layerSet.opacity;
var m_BlendMode = layerSet.blendMode;
var m_LinkedLayers = layerSet.linkedLayers;
var m_bHasMask = hasLayerMask();
if(m_bHasMask) loadSelectionOfMask();
if(layerSet.layers.length <= 1) {
addLayer();
var m_Tmp = activeDocument.activeLayer;
m_Tmp.name = "dummy - feel free to remove me";
activeDocument.activeLayer = layerSet;
ungroup();
addToSelection("dummy - feel free to remove me");
groupSelected(m_Name);
} else {
activeDocument.activeLayer = layerSet;
ungroup();
groupSelected(m_Name);
}
var m_Closed = activeDocument.activeLayer;
m_Closed.opacity = m_Opacity;
m_Closed.blendMode = m_BlendMode;
for(x in m_LinkedLayers) {
if(m_LinkedLayers[x].typename == "LayerSet")
activeDocument.activeLayer.link(m_LinkedLayers[x]);
}
if(m_bHasMask) maskFromSelection();
return m_Closed;
}
function ungroup() {
var m_Dsc01 = new ActionDescriptor();
var m_Ref01 = new ActionReference();
m_Ref01.putEnumerated( cTID( "Lyr " ), cTID( "Ordn" ), cTID( "Trgt" ) );
m_Dsc01.putReference( cTID( "null" ), m_Ref01 );
try {
executeAction( sTID( "ungroupLayersEvent" ), m_Dsc01, DialogModes.NO );
} catch(e) {}
}
closeAllLayerSets( app.activeDocument );

Photoshop CS6 Export Nested Layers to PNG?

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;
}