tell application id "com.adobe.photoshop"
tell current document
do javascript "// 2016, use it at your own risk;
#target photoshop
/*var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID('capp'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
var applicationDesc = executeActionGet(ref);
var currentToolOpt = applicationDesc.getObjectValue(stringIDToTypeID('currentToolOptions'));
var settings = currentToolOpt.getObjectValue(currentToolOpt.getKey(0));
var defaultwidth (settings.getUnitDoubleValue(settings.getKey(3)));
var defaultheight (settings.getUnitDoubleValue(settings.getKey(5)));*/
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var width = myDocument.width;
var height = myDocument.height;
var rel = " & mainRatio & ";
// =======================================================
var idslct = charIDToTypeID( 'slct' );
var desc5 = new ActionDescriptor();
var ref2 = new ActionReference();
ref2.putClass( stringIDToTypeID( 'cropTool' ) );
desc5.putReference( charIDToTypeID( 'null' ), ref2 );
desc5.putBoolean( stringIDToTypeID( 'dontRecord' ), true );
desc5.putBoolean( stringIDToTypeID( 'forceNotify' ), true );
executeAction( idslct, desc5, DialogModes.NO );
try {
// =======================================================
var idCrop = charIDToTypeID( 'Crop' );
var desc3 = new ActionDescriptor();
var idT = charIDToTypeID( 'T ' );
var desc4 = new ActionDescriptor();
var idTop = charIDToTypeID( 'Top ' );
var idPxl = charIDToTypeID( '#Pxl' );
desc4.putUnitDouble( idTop, idPxl, 0 );
var idLeft = charIDToTypeID( 'Left' );
var idPxl = charIDToTypeID( '#Pxl' );
desc4.putUnitDouble( idLeft, idPxl, (width - height * rel) / 2 );
var idBtom = charIDToTypeID( 'Btom' );
var idPxl = charIDToTypeID( '#Pxl' );
desc4.putUnitDouble( idBtom, idPxl, height );
var idRght = charIDToTypeID( 'Rght' );
var idPxl = charIDToTypeID( '#Pxl' );
desc4.putUnitDouble( idRght, idPxl, (width - height * rel) / 2 + height * rel );
var idRctn = charIDToTypeID( 'Rctn' );
desc3.putObject( idT, idRctn, desc4 );
var idAngl = charIDToTypeID( 'Angl' );
var idAng = charIDToTypeID( '#Ang' );
desc3.putUnitDouble( idAngl, idAng, 0.000000 );
var idDlt = charIDToTypeID( 'Dlt ' );
desc3.putBoolean( idDlt, false );
var idcropAspectRatioModeKey = stringIDToTypeID( 'cropAspectRatioModeKey' );
var idcropAspectRatioModeClass = stringIDToTypeID( 'cropAspectRatioModeClass' );
var idpureAspectRatio = stringIDToTypeID( 'pureAspectRatio' );
desc3.putEnumerated( idcropAspectRatioModeKey, idcropAspectRatioModeClass, idpureAspectRatio );
var idCnsP = charIDToTypeID( 'CnsP' );
desc3.putBoolean( idCnsP, true );
executeAction( idCrop, desc3, DialogModes.ALL );
} catch (e) {};
//
app.preferences.rulerUnits = originalRulerUnits;
};"
end tell
end tell