Please try this.
// apply magic wand tool at center of active layer’s bounds;
// 2022, use it at your own risk;
if (app.documents.length > 0) {
var theLayer = activeDocument.activeLayer;
var theBounds = getBounds();
theLayer.visible = false;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.POINTS;
useMagicWand2022 ([theBounds[3], theBounds[4]]);
// reset;
theLayer.visible = true;
app.preferences.rulerUnits = originalRulerUnits;
};
////////////////////////////////////
////// use magic wand tool //////
function useMagicWand2022 (theCoord) {
var idnull = charIDToTypeID( "null" );
var idPxl = charIDToTypeID( "#Pxl" );
// select tool;
var desc2 = new ActionDescriptor();
var ref2 = new ActionReference();
ref2.putClass( stringIDToTypeID( "magicWandTool" ) );
desc2.putReference( idnull, ref2 );
desc2.putBoolean( stringIDToTypeID( "dontRecord" ), true );
desc2.putBoolean( stringIDToTypeID( "forceNotify" ), true );
executeAction( charIDToTypeID( "slct" ), desc2, DialogModes.NO );
// apply tool;
var desc2 = new ActionDescriptor();
var ref2 = new ActionReference();
ref2.putProperty( charIDToTypeID( "Chnl" ), charIDToTypeID( "fsel" ) );
desc2.putReference( idnull, ref2 );
var desc3 = new ActionDescriptor();
desc3.putUnitDouble( charIDToTypeID( "Hrzn" ), idPxl, theCoord[0] );
desc3.putUnitDouble( charIDToTypeID( "Vrtc" ), idPxl, theCoord[1] );
desc2.putObject( charIDToTypeID( "T " ), charIDToTypeID( "Pnt " ), desc3 );
desc2.putInteger( charIDToTypeID( "Tlrn" ), 30 );
desc2.putBoolean( charIDToTypeID( "AntA" ), true );
desc2.putBoolean( stringIDToTypeID("merged"), true);
executeAction( charIDToTypeID( "setd" ), desc2, DialogModes.NO );
};
////// bounds of active layer //////
function getBounds (theIndex) {
var ref = new ActionReference();
ref.putProperty (stringIDToTypeID ("property"), stringIDToTypeID ("bounds"));
/*ref.putIndex(stringIDToTypeID("layer"), theIndex);*/
ref.putEnumerated( charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var layerDesc = executeActionGet(ref);
var theBounds = layerDesc.getObjectValue(stringIDToTypeID("bounds"));
var theseBounds = [theBounds.getUnitDoubleValue(stringIDToTypeID("left")), theBounds.getUnitDoubleValue(stringIDToTypeID("top")), theBounds.getUnitDoubleValue(stringIDToTypeID("right")), theBounds.getUnitDoubleValue(stringIDToTypeID("bottom"))];
var theW = theBounds.getUnitDoubleValue(stringIDToTypeID("right")) - theBounds.getUnitDoubleValue(stringIDToTypeID("left"));
var theH = theBounds.getUnitDoubleValue(stringIDToTypeID("bottom")) - theBounds.getUnitDoubleValue(stringIDToTypeID("top"));
var horCenter = theseBounds[0] + theW / 2;
var verCenter = theseBounds[1] + theH / 2;
return ([theseBounds, theW, theH, horCenter, verCenter])
};
