Skip to main content
Participating Frequently
February 7, 2020
Question

How to create masks action/script?

  • February 7, 2020
  • 5 replies
  • 1640 views

Hi,

 

I'm a noob of actions and scripting for PS.

I'd like to automate two processes that I've called "Apply Masks" and "Regenerate Masks" in Ps CS6 and above.

The most important is "Apply Mask", it would be a timesaver for what I've to do.

 

Apply Masks

For each layer,  if it has a mask, duplicate the layer (with the mask) near to the original (presumeably above?). To one version (aka the "duplicate") apply the mask. Then lock and hide the other version (aka the "source").

 

Regenerate Masks

If we have a "duplicate", delete it and unlock and show the respective "source".

 

How can I make that? What I've to study? Automations? Scripting? Both? I know nothing.

 

Thank you

UPDATE:
I realized that actions weren't enough so I wrote a JS Script to iterate through all layers, but I gave up at the step of checking if the layer has a layer mask.

 

// Iterate through top level artLayers
function outputTopLevelArtLayers(layerNode) {    
    for (var i=0; i<layerNode.length; i++) {
		alert("ArtLayer: "+layerNode[i].name);		
		
		// Check if it has a layer mask, then apply it
		// ...
    }
}

// Iterate through layerSets
function outputLayerSets(layerNode) {    
    for (var i=0; i<layerNode.length; i++) {
		alert("LayerSet: "+layerNode[i].name);
        outputLayerSets(layerNode[i].layerSets);
        for(var layerIndex=0; layerIndex < layerNode[i].artLayers.length; layerIndex++) {
            var layer=layerNode[i].artLayers[layerIndex];
            alert("ArtLayer: " + layer.name);
			
			// Check if it has a layer mask, then apply it
			// ...
        }
    }
}

outputTopLevelArtLayers(app.activeDocument.artLayers);
outputLayerSets(app.activeDocument.layerSets);
This topic has been closed for replies.

5 replies

Stephen Marsh
Community Expert
Community Expert
February 10, 2020

So how did the action set download and matching script code that I provided work out?

Stephen Marsh
Community Expert
Community Expert
February 9, 2020

I tried your script code but it does not work for me.

 

I have tested the following with success in a limited testing model (however, I don't know your layer structure so it may not work the same):

 

 

You can download the action in the link below:

https://www.dropbox.com/s/yyum9jvntvlhjmb/Apply%20Mask.atn?dl=0

 

Then using a helper script to run the conditional action on all layers (if the condition is true, then the conditional action plays the action to apply layer masks):

 

 

//forums.adobe.com/message/8895266#8895266
//forums.adobe.com/message/8895401#8895401

var ActionName = "Apply Mask Conditional"; // Replace with your action name
var ActionSet = "Apply Mask"; // Replace with your action set name

if (!documents.length) {
    alert('There are no documents open.', 'No Document');
} else {
    processArtLayers(activeDocument);
}

function processArtLayers(obj) {
    for (var i = obj.artLayers.length - 1; 0 <= i; i--) {
        activeDocument.activeLayer = obj.artLayers[i];
        doAction(ActionName, ActionSet);
    }
    for (var i = obj.layerSets.length - 1; 0 <= i; i--) {
        processArtLayers(obj.layerSets[i]);
    } 
}

 

 

Layers Before:

 

 

Layers After:

 

Participating Frequently
February 9, 2020

Thanks You very much Stephen, great hints

Stephen Marsh
Community Expert
Community Expert
February 8, 2020

As has been mentioned, with actions you can indeed automate much of this for the Apply Masks requirement...

 

You could use a conditional action step to test if the current layer has a pixel mask and if it did the conditional action step would run another action that would duplicate the active layer, apply the mask, select the relative backwards layer hide and lock.

 

Then you could manually select the next layer and run the action again.

 

All of this is possible with actions.

 

That being said, to automatically do this to all layers if the layer count may vary, you would need a helper script to loop the action over all of the layers (or script the entire process where layer names would be better handled).

 

You would not need a script if all of the layers had a consistent count that the action would then work its way through in a linear fashion. You might be able to build the action with more steps than you think you require, then it would simply error and you would cancel the playback if there were no layers left to process... This may or may not work as expected, I have not tested.

 

I have not thought about the Regenerate Masks requirement.

Participating Frequently
February 8, 2020

Thank you for the answer.

Yes, the number of layers can vary a lot.

PECourtejoie
Community Expert
Community Expert
February 7, 2020

Yes, using shortcuts (usually ALT+[ and ] )  to target the layers is the key, so that it ignores the layer names, and just targets the layer above or below. 

lambiloon
Community Expert
Community Expert
February 7, 2020

Hi check this video you will understand all....Regards

 

https://youtu.be/GiiSySY5nN0

Ali Sajjad / Graphic Design Trainer / Freelancer / Adobe Certified Professional