The best way is to designate two points on the upper vertical boundary of the pixels of the layer and two points on the lower border of the pixels of the layer, for example, using the pen tool. Then it is easy to calculate the angle of rotation so that the points become vertically symmetric after the transformation.
In your simple case, you can set one guide as in the picture and run the script.
In a more complex case, you need to set up two guides roughly like in the figure and run the script.
Since the calculation of symmetric points using guides may not be accurate enough for a strong rotation, it is possible that in the second case it will be necessary to run the script twice for a more accurate result.
app.activeDocument.suspendHistory("Straighten","straighten_hor_by_guides()");
function straighten_hor_by_guides()
{
try {
var doc = app.activeDocument;
var layer = app.activeDocument.activeLayer;
var g0, g1;
if (!doc.guides.length) { alert("No guides!"); return; }
for (var i = 0; i < doc.guides.length; i++)
{
if (doc.guides.direction == Direction.HORIZONTAL)
{
if (g0 == undefined) g0 = doc.guides;
else if (g1 == undefined) g1 = doc.guides;
else { alert("Too many horizontal guides!"); return; }
}
}
if (g0 == undefined && g1 == undefined)
{
alert("No horizontal guides!"); return;
}
else if (g0 != undefined && g1 == undefined)
{
app.preferences.rulerUnits = Units.PIXELS;
var y0 = Number(layer.bounds[1].value);
var x0 = Number(layer.bounds[2].value + layer.bounds[0].value)/2;
var y1 = Number(g0.coordinate.value);
selection_from_layer();
select_single_line(y1, true, stringIDToTypeID("intersectWith"));
try { var x1 = Number(doc.selection.bounds[2].value + doc.selection.bounds[0].value)/2; } catch(e) { alert("Bad guide!"); return; }
var w = x1 - x0;
var h = y1 - y0;
var angle;
if (h != 0) angle = Math.atan(w/h) * 180.0 / Math.PI;
doc.selection.deselect();
if (angle != undefined) rotate(angle, x0, y0);
}
else if (g0 != undefined && g1 != undefined)
{
app.preferences.rulerUnits = Units.PIXELS;
var y0 = Number(g0.coordinate.value);
var y1 = Number(g1.coordinate.value);
if (y0 > y1) { var tmp = y1; y1 = y0; y0 = tmp; }
selection_from_layer();
select_single_line(y0, true, stringIDToTypeID("intersectWith"));
var x0 = Number(doc.selection.bounds[2].value + doc.selection.bounds[0].value)/2;
selection_from_layer();
select_single_line(y1, true, stringIDToTypeID("intersectWith"));
var x1 = Number(doc.selection.bounds[2].value + doc.selection.bounds[0].value)/2;
var w = x1 - x0;
var h = y1 - y0;
var angle;
if (h != 0) angle = Math.atan(w/h) * 180.0 / Math.PI;
doc.selection.deselect();
if (angle != undefined) rotate(angle, x0, y0);
}
}
catch (e) { alert(e) }
}
function select_single_line(x, h, mode)
{
try
{
if (mode == undefined) mode = stringIDToTypeID("set");
var r = new ActionReference();
r.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection"));
var d = new ActionDescriptor();
d.putReference(stringIDToTypeID("target"), r);
var d1 = new ActionDescriptor();
d1.putUnitDouble(stringIDToTypeID(h?"top":"left"), stringIDToTypeID("pixelsUnit"), x);
d.putObject(stringIDToTypeID("to"), stringIDToTypeID(h?"singleRow":"singleColumn"), d1);
executeAction(mode, d, DialogModes.NO);
}
catch (e) { throw(e); }
}
function rotate(angle, x, y)
{
try
{
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID("layer"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var d = new ActionDescriptor();
d.putReference(stringIDToTypeID("target"), r);
d.putEnumerated(stringIDToTypeID("freeTransformCenterState"), stringIDToTypeID("quadCenterState"), stringIDToTypeID("QCSIndependent"));
var d2 = new ActionDescriptor();
d2.putUnitDouble(stringIDToTypeID("horizontal"), stringIDToTypeID("pixelsUnit"), x);
d2.putUnitDouble(stringIDToTypeID("vertical"), stringIDToTypeID("pixelsUnit"), y);
d.putObject(stringIDToTypeID("position"), stringIDToTypeID("point"), d2);
d.putUnitDouble(stringIDToTypeID("angle"), stringIDToTypeID("angleUnit"), angle);
d.putEnumerated(stringIDToTypeID("interpolation"), stringIDToTypeID("interpolationType"), stringIDToTypeID("bicubic"));
executeAction(stringIDToTypeID("transform"), d, DialogModes.NO);
}
catch (e) { throw(e); }
}
function selection_from_layer()
{
try
{
var r = new ActionReference();
r.putProperty(stringIDToTypeID("channel"), stringIDToTypeID("selection"));
var d = new ActionDescriptor();
d.putReference(stringIDToTypeID("target"), r);
var r = new ActionReference();
r.putEnumerated(stringIDToTypeID("channel"), stringIDToTypeID("channel"), stringIDToTypeID("transparencyEnum"));
d.putReference(stringIDToTypeID("to"), r);
executeAction(stringIDToTypeID("set"), d, DialogModes.NO);
}
catch (e) { throw(e); }
}