2 Replies Latest reply on Jan 8, 2016 9:40 AM by Jeff Paul gs

    Script to create a clipping mask layer above every layer in my document

    Jeff Paul gs

      I've tried figuring out how to script this, but I'm really new at scripting and couldn't figure it out. I'd like a script that would create a clipping mask layer above every layer in  the document. So, it would create a new blank layer above each existing layer, rename the new layers based on the layer it's on top of + a suffix, and apply a clipping mask to the new layers.

       

      So, if I had a PSD file with the following layers:

      Layer A

      Layer B

      Layer C

       

      the resulting layers would be:

       

           Layer A_clip

      Layer A

           Layer B_clip

      Layer B

           Layer C_clip

      Layer C

       

      Any help would be much appreciated.

        • 1. Re: Script to create a clipping mask layer above every layer in my document
          c.pfaffenbichler Level 9

          // thanks to mike hale and paul riggott;

          // 2015, use it at your own risk;

          #target photoshop

          if (app.documents.length > 0) {

          app.activeDocument.suspendHistory("add clipping masked layers", "theDialog ()");

          };

          function theDialog () {

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

          var theLayers = theLayerNames ();

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

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

          newLayer (theLayers[m][1], theLayers[m][0]+"_add")

          };

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

          $.gc();

          };

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

          function theLayerNames () {

          // the file;

          var myDocument = app.activeDocument;

          // get number of layers;

          var ref = new ActionReference();

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

          var applicationDesc = executeActionGet(ref);

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

          // process the layers;

          var theLayers = new Array;

          for (var m = 0; m <= theNumber; 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"));

          // if not layer group collect values;

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

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

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

          var theOpacity = layerDesc.getInteger(stringIDToTypeID('opacity'));

          theLayers.push([theName, theID, theOpacity])

          };

          }

          catch (e) {};

          };

          return theLayers

          };

          ////// create new layer //////

          function newLayer (theID, theName) {

          var ref = new ActionReference();

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

          var desc = new ActionDescriptor();

          desc.putReference(charIDToTypeID("null"), ref );

          desc.putBoolean( charIDToTypeID( "MkVs" ), false );

          try{

          executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );

          }catch(e){};

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

          var idMk = charIDToTypeID( "Mk  " );

              var desc5 = new ActionDescriptor();

              var idnull = charIDToTypeID( "null" );

                  var ref2 = new ActionReference();

                  var idLyr = charIDToTypeID( "Lyr " );

                  ref2.putClass( idLyr );

              desc5.putReference( idnull, ref2 );

              var idUsng = charIDToTypeID( "Usng" );

                  var desc6 = new ActionDescriptor();

                  var idNm = charIDToTypeID( "Nm  " );

                  desc6.putString( idNm, theName );

                  var idGrup = charIDToTypeID( "Grup" );

                  desc6.putBoolean( idGrup, true );

              desc5.putObject( idUsng, idLyr, desc6 );

          executeAction( idMk, desc5, DialogModes.NO );

          };