I changed the function getSelectedLayers to include Groups and Layers via a mixture of AM and DOm code.
var aaa = getSelectedLayers();
for (var m = 0; m < aaa.length; m++) {
activeDocument.activeLayer = aaa[m];
var theBounds = getBoundsOfActiveLayer();
alert (theBounds.join("\n\n"));
};
////// bounds of active layer //////
function getBoundsOfActiveLayer () {
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
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"))];
return [theName, theIdentifier, theseBoundsNoMask, theseBounds]
};
////// collect bounds of selected layers //////
function getSelectedLayers () {
// 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 theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
selectedLayers.push(theIdentifier);
} catch (e) {};
};
// if only one:
}else{
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
try {
var theIdentifier = layerDesc.getInteger(stringIDToTypeID ("layerID"));
selectedLayers = [theIdentifier]
} catch (e) {};
};
//
var theResult = new Array;
for (var m = 0; m < selectedLayers.length; m++) {
selectLayerByID(selectedLayers[m], false);
theResult.push (activeDocument.activeLayer)
};
// reset;
app.preferences.rulerUnits = originalRulerUnits;
return theResult;
};
// based on code by mike hale, via paul riggott;
function selectLayerByID(id,add){
add = undefined ? add = false:add
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID("Lyr "), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){
alert(e.message);
}
};
... View more