Skip to main content
Chris Panny
Inspiring
July 19, 2015
Question

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

  • July 19, 2015
  • 1 reply
  • 1735 views

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;

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

        if (docLayers != 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

This topic has been closed for replies.

1 reply

JJMack
Community Expert
Community Expert
July 19, 2015

Layer Mask are like hidden alpha channels. However  Layer mask only show in the Channels Palette when the layer they are on is the current Photoshop target.   You can save a Layer mask as Alpha channels.  However Photoshop supports up to 8,000 layers all can have Layer mask.  Photoshop only supports 53 Alpha channels.   You may well not be able to save alpha channels for all layer mask in a document.  In fact if your document has 53 Alpha channels no layer mask could also be save as an alpha channel.

JJMack
Chris Panny
Inspiring
July 19, 2015

My PSDs will have up to 16 layers with masks. I never get close to the 53 max limit for alpha channels.

It's only an island if you look at it from the water.
JJMack
Community Expert
Community Expert
July 19, 2015

Here is an old post wheer Paul Poster some code Mike Wote to deal with Layer mask you mah fit it useful....

Re: check if layer has mask

JJMack