Using Extendscript, how can I get the bounds of a layer mask's selection?
Hello! I'm trying to retrieve the bounds of the selection of a layer's mask. User @c.pfaffenbichler already helped me with this amazing code and it works perfectly (thank you!):
// 2024, use it at your own risk;
if (app.documents.length > 0) {
// set to pixels;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var myDocument = app.activeDocument;
var theLayers = collectSelectedLayersBounds ();
// process layers;
var theCount = 0;
for (var m = theLayers.length-1; m >= 0; m--) {
var thisOne = theLayers[m];
alert (thisOne[0]+"\nbounds\n"+thisOne[2].join("\n")+"\nbounds with mask\n"+thisOne[3].join("\n"));
theCount++
};
// reset;
app.preferences.rulerUnits = originalRulerUnits;
};
////////////////////////////////////
////// collect bounds of selected layers //////
function collectSelectedLayersBounds () {
// set to pixels;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
// get selected layers;
var selectedLayers = new Array;
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
if (desc.getBoolean(stringIDToTypeID("hasBackgroundLayer")) == true) {var theAdd =0}
else {var theAdd = 1};
if( desc.hasKey( stringIDToTypeID( 'targetLayers' ) ) ){
desc = desc.getList( stringIDToTypeID( 'targetLayers' ));
var c = desc.count;
var selectedLayers = new Array();
// run through selected layers;
for(var i=0;i<c;i++){
var theIndex = desc.getReference( i ).getIndex()+theAdd;
// get id for solid color layers;
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID("Lyr "), theIndex );
var layerDesc = executeActionGet(ref);
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
var theBoundsNoMask = layerDesc.getObjectValue(stringIDToTypeID("boundsNoMask"));
var theseBoundsNoMask = [theBoundsNoMask.getUnitDoubleValue(stringIDToTypeID("left")), theBoundsNoMask.getUnitDoubleValue(stringIDToTypeID("top")), theBoundsNoMask.getUnitDoubleValue(stringIDToTypeID("right")), theBoundsNoMask.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
selectedLayers.push([theName, theIdentifier, theseBoundsNoMask, theseBounds]);
} catch (e) {};
};
// if only one:
}else{
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
try {
var theName = layerDesc.getString(stringIDToTypeID('name'));
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
selectedLayers = [[theName, theIdentifier, theseBounds]]
} catch (e) {};
};
// reset;
app.preferences.rulerUnits = originalRulerUnits;
return selectedLayers;
};My only problem is that I need it to act on a layer that already exists in an array I've collected elsewhere in the script. So for example, assume I've already successfully retrieved my target layers in the following array:
var layers = getSelectedLayers();I'd like to be able to execute the above code in a statement like this, where the function would just send back the info for the layer passed in an argument, like this:
var theLayers = collectSelectedLayersBounds(layers[3]);What would i need to adjust on the "collectSelectedLayersBounds" function in order to implement this into my existing code?

