2 Replies Latest reply on Sep 30, 2015 11:57 PM by andreasf

    Flatten all layers including background (but not locked layers)

    andreasf Level 1

      Is there a way to

       

      Flatten all layers including the background layer excluding all locked layers

      and then turning visibility off for locked layers

       

      Any help most welcome !

        • 1. Re: Flatten all layers including background (but not locked layers)
          c.pfaffenbichler Level 9

          After my previous mistake I tried a solution and you can give it a try:

          // 2015, use it at your own risk;

          #target photoshop

          if (app.documents.length > 0) {

          var myDocument = app.activeDocument;

          var theLayers = getLayersIndexAndID ();

          // hide locked;

          for (var m = 0; m < theLayers.length; m++) {

          if (theLayers[m][3] == true) {hideByID (theLayers[m][2])}

          };

          // merge visible;

          myDocument.activeLayer = myDocument.layers[myDocument.layers.length-1];

          myDocument.layers[myDocument.layers.length-1].visible = true;

          // =======================================================

          var idMrgV = charIDToTypeID( "MrgV" );

          executeAction( idMrgV, undefined, DialogModes.NO );

          };

          ////////////////////////////////////

          function getLayersIndexAndID () {

          var ref = new ActionReference();

          ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );

          var applicationDesc = executeActionGet(ref);

          var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));

          var theArray = new Array;

          ////// work through layers //////

          for (var m = theNumber; m >= 0; m--) {

          try {

          var ref = new ActionReference();

          ref.putIndex( charIDToTypeID( "Lyr " ), m);

          var layerDesc = executeActionGet(ref);

          var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));

          var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));

          var theName = layerDesc.getString(stringIDToTypeID('name'));

          var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));

          var locking = layerDesc.getObjectValue(stringIDToTypeID("layerLocking"));

          var anyLock = false;

          for (var a = 0; a < locking.count; a++) {

            var thisLock = locking.getBoolean(locking.getKey(a));

            if (thisLock == true) {anyLock = true}

            };

          ////////////////////////////////////

          // if group start:

          if (layerSet != "layerSectionEnd" && isBackground != true) {

          theArray.push([theName, m, theID, anyLock])

          };

          ////////////////////////////////////

          }

          catch (e) {};

          };

          // the results;

          return theArray

          };

          ////// hide layer //////

          function hideByID (theID) {

          // =======================================================

          var idHd = charIDToTypeID( "Hd  " );

              var desc2 = new ActionDescriptor();

              var idnull = charIDToTypeID( "null" );

                  var list1 = new ActionList();

                      var ref1 = new ActionReference();

                      ref1.putIdentifier( charIDToTypeID( "Lyr " ), theID );

                  list1.putReference( ref1 );

              desc2.putList( idnull, list1 );

          executeAction( idHd, desc2, DialogModes.NO );

          };