also not very fast - converting to a smart object is a rather time memory-consuming operation.
#target photoshop
s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayers'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var sel = executeActionGet(r).hasKey(p) ? executeActionGet(r).getList(p) : null;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('hasBackgroundLayer'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var shift = executeActionGet(r).getBoolean(p) ? 0: 1
if (sel) {
for (var i = 0; i < sel.count; i++) {
(r = new ActionReference()).putIndex(s2t("layer"), sel.getReference(i).getIndex() + shift);
(d = new ActionDescriptor()).putReference(s2t("null"), r);
executeAction(s2t("select"), d, DialogModes.NO)
executeAction(s2t("newPlacedLayer"), undefined, DialogModes.NO);
}
}