function resizeWithMargin(margin, layer) {
// Margin in percent from the edge
// Layer should be centered before resizing
if (!layer) layer = activeDocument.activeLayer;
var lyrWidth = layer.bounds[2].value - layer.bounds[0].value;
var onePercentDoc = activeDocument.width.value / 100;
var onePercentLyr = lyrWidth / 100;
var scaly = (onePercentDoc / onePercentLyr) * (100 - (margin * 2));
layer.resize(scaly, scaly, AnchorPosition.MIDDLECENTER);
}