I understand what you are saying and I agree that to match the measure tool the function would need to be changed. What I was trying to say was that it was originally written to work with left to right paths. The angle of a path made from left to right( that is pathPoint[0].anchor[0] is less than of pathPoint[3].anchor[0] when using a line tool path ) can only have an angle of +/- 90.
A path made from right to left needs to use different pathpoints and requries different math. Another factor for the mismatch is the measure tool rounds to the nearest whole pixel. Paths can use fractions of a pixel. The angle may not match exactly because of that rounding.
It's true all of those issues could be fixed and you are free to make any modifications you like.
This only works with CS5 as it now uses the ruler tool instead of a path. But it fixes all the issues talked about in this thread and well as some other differences between the original funciton and the data in the info panel.
It returns a custom object with angle and length properties if there is a ruler tool line in the active document and undefined it not. I was unsure if the length should be a unitValue or a number in the ruler units settings. I went with number because the info panel has different number of decimal places for the different units. And like the info panel if the ruler setting are set to percent the length value is blank
function getRulerToolLengthAndAngle(){
var points = [];
points.start = [];
points.end = [];
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putProperty( charIDToTypeID('Prpr'), charIDToTypeID('RrPt') );
ref.putEnumerated( charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt') );
desc.putReference( charIDToTypeID('null'), ref );
var desc = executeAction( charIDToTypeID('getd'), desc, DialogModes.NO );
if( desc.hasKey( charIDToTypeID('Pts ') ) ){
var pointList = desc.getList( charIDToTypeID('Pts ') );
var startPointDesc = pointList.getObjectValue(0);
points.start.push( startPointDesc.getUnitDoubleValue( charIDToTypeID( 'X ' ) ) );
points.start.push( startPointDesc.getUnitDoubleValue( charIDToTypeID( 'Y ' ) ) );
var endPointDesc = pointList.getObjectValue(2);
points.end.push( endPointDesc.getUnitDoubleValue( charIDToTypeID( 'X ' ) ) );
points.end.push( endPointDesc.getUnitDoubleValue( charIDToTypeID( 'Y ' ) ) );
var res = {};
res.toString = function(){ return 'RulerTool Info';};
var pointA = points.start;
var pointB = points.end;
if( pointA[0]<pointB[0] ){
var a = Math.max(pointA[0],pointB[0])-Math.min(pointA[0],pointB[0]);
var o = Math.max(pointA[1],pointB[1])-Math.min(pointA[1],pointB[1]);
var ang = (180/Math.PI) * Math.atan2(o,a);
if(pointA[1] < pointB[1]){//negative angle
ang = -ang;
};
res.angle = ang.toFixed(1);
}else{
var a = Math.max(pointA[1],pointB[1])-Math.min(pointA[1],pointB[1]);
var o = Math.max(pointA[0],pointB[0])-Math.min(pointA[0],pointB[0]);
var ang = 180-((180/Math.PI) * Math.atan2(a,o));
if(pointA[1] < pointB[1]){//negative angle
ang = -ang;
};
res.angle = ang.toFixed(1);
}
if( app.preferences.rulerUnits == Units.PERCENT ){
res.length = "";
}else{
var c = Math.sqrt((a*a)+(o*o));
var length = new UnitValue( c, 'px');
length.baseUnit = new UnitValue((1/app.activeDocument.resolution),'in');
switch( app.preferences.rulerUnits ){
case Units.PIXELS: res.length = length.as('px').toFixed(2); break;
case Units.INCHES : res.length = length.as('in').toFixed(3); break;
case Units.CM: res.length = length.as('cm').toFixed(2); break;
case Units.MM: res.length = length.as('mm').toFixed(1); break;
case Units.POINTS: res.length = length.as('pt').toFixed(1); break;
case Units.PICAS: res.length = length.as('pc').toFixed(2); break;
}
}
return res;
}
}
var info = getRulerToolLengthAndAngle();
if(info != undefined ) alert('The ruler tool angle is: '+info.angle+'\nThe length is: '+info.length);