try this script try {
// add levels
var d = new ActionDescriptor();
var r = new ActionReference();
r.putClass(stringIDToTypeID("adjustmentLayer"));
d.putReference(stringIDToTypeID("null"), r);
var d1 = new ActionDescriptor();
var d2 = new ActionDescriptor();
d2.putEnumerated(stringIDToTypeID("presetKind"), stringIDToTypeID("presetKindType"), stringIDToTypeID("presetKindDefault"));
var list = new ActionList();
var d3 = new ActionDescriptor();
var r1 = new ActionReference();
r1.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("composite"));
d3.putReference(stringIDToTypeID("channel"), r1);
var list1 = new ActionList();
list1.putInteger(0);
list1.putInteger(128);
d3.putList(stringIDToTypeID("input"), list1);
list.putObject(stringIDToTypeID("levelsAdjustment"), d3);
d2.putList(stringIDToTypeID("adjustment"), list);
d1.putObject(stringIDToTypeID("type"), stringIDToTypeID("levels"), d2);
d.putObject(stringIDToTypeID("using"), stringIDToTypeID("adjustmentLayer"), d1);
executeAction(stringIDToTypeID("make"), d, DialogModes.NO);
// trim
var d = new ActionDescriptor();
d.putEnumerated(stringIDToTypeID("trimBasedOn"), stringIDToTypeID("trimBasedOn"), stringIDToTypeID("topLeftPixelColor"));
d.putBoolean(stringIDToTypeID("top"), true);
d.putBoolean(stringIDToTypeID("bottom"), true);
d.putBoolean(stringIDToTypeID("left"), true);
d.putBoolean(stringIDToTypeID("right"), true);
executeAction(stringIDToTypeID("trim"), d, DialogModes.NO);
// delete levels layer
var d = new ActionDescriptor();
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
d.putReference(stringIDToTypeID("null"), r);
executeAction(stringIDToTypeID("delete"), d, DialogModes.NO);
// expand canvas +3px, white color
var d = new ActionDescriptor();
d.putBoolean(stringIDToTypeID("relative"), true);
d.putUnitDouble(stringIDToTypeID("width"), stringIDToTypeID("pixelsUnit"), 6);
d.putUnitDouble(stringIDToTypeID("height"), stringIDToTypeID("pixelsUnit"), 6);
d.putEnumerated(stringIDToTypeID("horizontal"), stringIDToTypeID("horizontalLocation"), stringIDToTypeID("center"));
d.putEnumerated(stringIDToTypeID("vertical"), stringIDToTypeID("verticalLocation"), stringIDToTypeID("center"));
d.putEnumerated(stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("white"));
executeAction(stringIDToTypeID("canvasSize"), d, DialogModes.NO);
app.preferences.rulerUnits = Units.PIXELS;
var w = activeDocument.width.value;
var h = activeDocument.height.value;
if (w/h > 1.5)
{
var d = new ActionDescriptor();
d.putUnitDouble(stringIDToTypeID("height"), stringIDToTypeID("pixelsUnit"), Math.round(w/1.5));
d.putEnumerated(stringIDToTypeID("horizontal"), stringIDToTypeID("horizontalLocation"), stringIDToTypeID("center"));
d.putEnumerated(stringIDToTypeID("vertical"), stringIDToTypeID("verticalLocation"), stringIDToTypeID("center"));
d.putEnumerated(stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("white"));
executeAction(stringIDToTypeID("canvasSize"), d, DialogModes.NO);
}
else
{
var d = new ActionDescriptor();
d.putUnitDouble(stringIDToTypeID("width"), stringIDToTypeID("pixelsUnit"), Math.round(h*1.5));
d.putEnumerated(stringIDToTypeID("horizontal"), stringIDToTypeID("horizontalLocation"), stringIDToTypeID("center"));
d.putEnumerated(stringIDToTypeID("vertical"), stringIDToTypeID("verticalLocation"), stringIDToTypeID("center"));
d.putEnumerated(stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("canvasExtensionColorType"), stringIDToTypeID("white"));
executeAction(stringIDToTypeID("canvasSize"), d, DialogModes.NO);
}
} catch (e) { alert(e.line+ "\n\n" +e); }
... View more