Sorry, my mistake, the parent group wasn't selected! :]
Try this, it doesn't rename a selected Background image layer, but it could. It uses a space separator between the entered prefix and the existing layer name. A single undo history step is provided:
#target photoshop
function main() {
(function () {
var prefix = prompt("Enter the layer name prefix:", "");
if (prefix === null) {
return;
}
// Get the selected layers: courtesy of jazz-y
var s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayersIDs'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
var lrs = executeActionGet(r).getList(p),
sel = new ActionReference();
// Loop over the selected layers: courtesy of jazz-y
for (var i = 0; i < lrs.count; i++) {
sel.putIdentifier(s2t('layer'), p = lrs.getReference(i).getIdentifier(s2t('layerID')));
(r = new ActionReference()).putIdentifier(s2t('layer'), p);
(d = new ActionDescriptor()).putReference(s2t("target"), r);
executeAction(s2t('select'), d, DialogModes.NO);
var layerName = activeDocument.activeLayer.name;
// Rename the selected layers
if (activeDocument.activeLayer.isBackgroundLayer === false) {
activeDocument.activeLayer.name = prefix + " " + layerName;
}
}
})();
}
// Single history stage undo
activeDocument.suspendHistory("Undo script...", "main()");