3 Replies Latest reply on Jul 18, 2015 8:18 PM by JJMack

    How does javaScript select an existing layer mask in order to make an alpha channel from the selection?

    Chris Panny Level 1

      Hello javaScripters,

       

      I have hundreds of PSD files that need all existing layer masks made into alpha channels. The PSDs have either a floating base layer or a locked background layer with no mask and 1 - 16 additional layers with corresponding masks. If I manually make a mask into an alpha channel, I select the layer with the mask, This makes the layer active so when I switch to the Channels panel, it appears directly under the blue channel. Then all i have to do is right click over the mask and choose Duplicate Channel. It's pretty easy to do, however, I would like to automate this using JS. When I initially attempted to do this with code, the JS doesn't see this mask in the Channels panel - it only sees the red, green and blue, so duplicating doesn't work. The other way to do this manually is to make a selection of the mask for the active layer by pressing "Cntrl + clicking" and then copy / paste it into a new blank alpha channel. This seems like it would work, but I don't know how to write the code to make the selection of the active layer mask. Does anyone know how to do this? Is there a completely different way to do this? I'm open to suggestions!


      Below is the code that I have so far. It has a lot of comments to explain what's going on. I also included some alerts which helps me figure out how far along the code executes for trouble shooting purposes.


      #target photoshop

      // global variables

      var openDocs = app.documents;

      var docCount = openDocs.length;

      var docActive = app.activeDocument;

       

      var  docLayers = docActive.artLayers;

      var docLayer = docActive.artLayer;

      var docActiveLayer = docActive.activeLayer;

      var layersCount = docLayers.length;

       

      var docChannels = docActive.channels;

      var docActiveChannel = docActive.activeChannel;

       

      var x=docLayers.length -1;  // iteration default value for do - while loop

      var y = 3;                          // iteration value for mask in channels panel - skips past red, green & blue

       

      if (docLayers.length < 2) {     // checks to see if there are at least 2 layers in the document

      alert("There needs to be at least 2 layers in this document");   

          } else {

       

      do {

          docActiveLayer = docLayers[x];

          alert("The active layer is " + docLayers[x].name);

       

              if (docLayers[x] != docLayers[(docLayers.length - 1)] ) {      // checks to see if active layer is the background or base layer. If so, it skips it

       

                  docChannels.add();                                            // make a new blank alpha channel

                  var channName = docActiveLayer.name;              //  active layer name is assigned to variable

                  docChannels[(y+1)].name = channName;           // alpha channel name is changed to match the active layer name

                                                                                   <--------//  code to make selection of mask should go here

                  y+=1;

                  }  // end if active layer is background or base layer

       

          x-=1;

          alert("The active layer is " + docActiveLayer.name + " and x = " + x );

          } while (x > 1 );

       

      // end if doc has less than 2 layers