function setZoom( zoom ) {
cTID = function(s) { return app.charIDToTypeID(s); };
var docRes = activeDocument.resolution;
activeDocument.resizeImage( undefined, undefined, 72/(zoom), ResampleMethod.NONE );
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( cTID( "Mn " ), cTID( "MnIt" ), cTID( 'PrnS' ) );
desc.putReference( cTID( "null" ), ref );
executeAction( cTID( "slct" ), desc, DialogModes.NO );
activeDocument.resizeImage( undefined, undefined, docRes, ResampleMethod.NONE );
}
function getZoom()
{
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var desc = executeActionGet(ref);
return desc.getDouble(stringIDToTypeID("zoom"));
}
function getBounds(doc,x,y)
{
var deltaX=x-doc.width/2;
var deltaY=y-doc.height/2;
var leftX=0;
var rightX=doc.width+2*deltaX;
var upY=0;
var bottomY=doc.height+2*deltaY;
return [leftX,upY,rightX,bottomY];
}
function fitImage(cmd)
{
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Mn "), charIDToTypeID("MnIt"), charIDToTypeID(cmd));
desc.putReference(charIDToTypeID("null"), ref);
executeAction(stringIDToTypeID("select"), desc, DialogModes.ALL);
}
var bnd=null;
try
{
var doc=activeDocument;
bnd=doc.selection.bounds;
} catch (err)
{
alert("Please, open document and make selection!");
}
if (bnd)
{
var newX=(bnd[2]+bnd[0])/2;
var newY=(bnd[3]+bnd[1])/2;
var bnd1=getBounds(doc,newX,newY)
var st = doc.activeHistoryState;
var id245 = charIDToTypeID( "Crop" );
var desc43 = new ActionDescriptor();
executeAction( id245, desc43, DialogModes.NO );
fitImage("FtOn");
var zm=getZoom();
doc.crop(bnd1);
fitImage("ActP");
setZoom( zm );
doc.activeHistoryState=st;
}