Yes, it's possible.
Firstly you need to find a way of telling Photoshop what layers are currently selected. There's no built in method for this, so you have to put them in a group, add them to an array and then put everything back to normal.
Then it's just a matter of looping over that little list and performing your function above. I've renamed it to do_that_thing()
So the whole thing looks like this:
// Switch off any dialog boxes
displayDialogs = DialogModes.NO; // OFF
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var mySelection = [];
get_selected_layers(); // selected layers
// alert(selected_layers);
// loop over the selected layers
for (var i=0; i < mySelection.length; i++)
{
do_that_thing();
}
//Reset units
app.preferences.rulerUnits = startRulerUnits;
// Set Display Dialogs back to normal
displayDialogs = DialogModes.ALL; // NORMAL
//functions here:
function do_that_thing()
{
var targetSize = 300;
var bounds = activeDocument.activeLayer.bounds;
var width = bounds[2].value - bounds[0].value;
var height = bounds[3].value - bounds[1].value;
if(width > height)
{
var newSize = (100 / width) * targetSize;
}
else
{
var newSize = (100 / height) * targetSize;
}
activeDocument.activeLayer.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);
}
function get_selected_layers()
{
// taken from
// https://stackoverflow.com/questions/27255364/
var resultLayers = [];
try
{
// create a temp group
var idGrp = stringIDToTypeID( "groupLayersEvent" );
var descGrp = new ActionDescriptor();
var refGrp = new ActionReference();
refGrp.putEnumerated(charIDToTypeID( "Lyr " ),charIDToTypeID( "Ordn" ),charIDToTypeID( "Trgt" ));
descGrp.putReference(charIDToTypeID( "null" ), refGrp );
executeAction( idGrp, descGrp, DialogModes.NO );
for (var ix=0; ix < app.activeDocument.activeLayer.layers.length; ix++)
{
resultLayers.push(app.activeDocument.activeLayer.layers[ix]);
mySelection.push(app.activeDocument.activeLayer.layers[ix]);
}
// get rid of the temp group
// delete history state
// =======================================================
var idDlt = charIDToTypeID( "Dlt " );
var desc103 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref27 = new ActionReference();
var idHstS = charIDToTypeID( "HstS" );
var idCrnH = charIDToTypeID( "CrnH" );
ref27.putProperty( idHstS, idCrnH );
desc103.putReference( idnull, ref27 );
executeAction( idDlt, desc103, DialogModes.NO );
}
catch
(eeek)
{
alert("Something went wrong")
};
return resultLayers;
}