Please try this..
main();
function main(){
if(!documents.length) return;
try{
var doc = activeDocument;
if(doc.componentChannels.length != doc.channels.length){
if(doc.activeLayer.isBackgroundLayer) doc.activeLayer.isBackgroundLayer = false;
doc.selection.load(doc.channels[doc.componentChannels.length]);
addMask();
doc.trim(TrimType.TRANSPARENT);
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
doc.resizeCanvas(doc.width+60, doc.height, AnchorPosition.MIDDLECENTER);
doc.resizeCanvas(doc.width, doc.height+10, AnchorPosition.BOTTOMCENTER);
doc.flatten();
doc.channels[doc.componentChannels.length].remove();
app.preferences.rulerUnits = startRulerUnits;
}
}catch(e){alert(e + " - " + e.line);}
}
function addMask(){
var desc = new ActionDescriptor();
desc.putClass( charIDToTypeID('Nw '), charIDToTypeID('Chnl') );
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('Chnl'), charIDToTypeID('Chnl'), charIDToTypeID('Msk ') );
desc.putReference( charIDToTypeID('At '), ref );
desc.putEnumerated( charIDToTypeID('Usng'), charIDToTypeID('UsrM'), charIDToTypeID('RvlS') );
executeAction( charIDToTypeID('Mk '), desc, DialogModes.NO );
}