PS Extendscript script to detect mask on layer and if there's a mask then select a different layer?
Scripting amateur and I've tried many things, but they have not worked yet. I want the script to randomly select and make a layer or layer group active, detect if there is a layer mask and if so then select another layer until it gets to one which does not have a mask on it, then make that the active layer or layer group.
// Function to check if a layer has a mask
function hasMask(layer) {
return layer.hasMask;
}
// Function to recursively check all child layers for a mask
function checkLayersForMask(layers) {
for (var i = 0; i < layers.length; i++) {
var layer = layers[i];
// If the current layer is a group or set, recursively check its child layers for a mask
if (layer.typename == "LayerSet" || layer.typename == "ArtLayer") {
var childLayers = layer.layers;
if (childLayers.length > 0) {
var activeLayer = checkLayersForMask(childLayers);
if (activeLayer) {
return activeLayer;
}
}
}
// If the current layer does not have a mask, return it
if (!hasMask(layer)) {
return layer;
}
}
}
// Get the active document and its layers
var doc = app.activeDocument;
var layers = doc.layers;
// Generate a random number between 0 and the number of layers
var randomLayerIndex = Math.floor(Math.random() * layers.length);
// Make the layer at the random index the active layer
app.activeDocument.activeLayer = layers[randomLayerIndex];
// Check all the child layers for a mask
var activeLayer = checkLayersForMask(layers);
// Make the first layer without a mask active
if (activeLayer) {
app.activeDocument.activeLayer = activeLayer;
}