Copy link to clipboard
Copied
An image is open and a fragment is being measured using the ruler tool:
I want to resize the image to real scale through a simple javascript using the value of the measurement that appears in the information tab. My problem is that I don't know how to get the value of the rule tool in javascript.
The script is the following:
// =======================================================
var startRulerUnits = app.preferences.rulerUnits
var docRef = app.documents[0]
var w; var h; var r; var measureRulerTool
app.preferences.rulerUnits = Units.CM
measureRulerTool = <<MEASUREMENT IN CENTIMETERS TAKEN BY THE RULER TOOL>>
w= docRef.width / measureRulerTool
h = docRef.height / measureRulerTool
r = 1000
docRef.resizeImage (w, h, r)
app.preferences.rulerUnits = startRulerUnits
// =======================================================
Can any expert help me?
Many thanks to the community
1 Correct answer
Unfortunately it won't work in CS3.
The original (corrected code) is shown below.
function getRulerEndpoints()
{
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("rulerPoints"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var result = executeActionGet(r);
if (result.hasKey(stringIDToTypeID("points")))
{
var
...
Explore related tutorials & articles
Copy link to clipboard
Copied
I haven't used this but Photoshop Scripting Reference has info about using measurements, so the methods do exist.
Copy link to clipboard
Copied
I have already consulted this guide and others. I have not found examples or clear help on how to get the value I need.
It is possible that it is explained in the guides and that I do not know how to find it because of my little experience. For that reason I need the help of some expert.
Thank you very much for your interest
Copy link to clipboard
Copied
Welcome to Adobe scripting. A lot of it is like this, little clear documentation or examples. Best bet is to learn how to write test code, use reflect, and be patient. Every one of use has had to figure a lot of things out. Have you tried a test script to see if you can get it working?
Copy link to clipboard
Copied
I have tried many tests but got nothing. There are some functions, methods and constants on measures but I don't know if they are useful for that.
Copy link to clipboard
Copied
Script "C:\Program Files\Adobe\Adobe Photoshop ...\Required\Straighten.jsx"
Look at getRulerEndpoints function.
Copy link to clipboard
Copied
I can't find this script in this folder. I guess it's because it's an old version of photoshop (CS3)
Can you paste the code here?
Copy link to clipboard
Copied
Unfortunately it won't work in CS3.
The original (corrected code) is shown below.
function getRulerEndpoints()
{
try {
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("rulerPoints"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var result = executeActionGet(r);
if (result.hasKey(stringIDToTypeID("points")))
{
var list = result.getList(stringIDToTypeID("points"));
// The middle item in the list is an unused "midpoint" (currently == p0)
return [
[list.getObjectValue(0).getUnitDoubleValue(stringIDToTypeID("x")),list.getObjectValue(0).getUnitDoubleValue(stringIDToTypeID("y"))],
[list.getObjectValue(2).getUnitDoubleValue(stringIDToTypeID("x")),list.getObjectValue(2).getUnitDoubleValue(stringIDToTypeID("y"))],
];
}
else
return [];
}
catch (e) { alert("line "+e.line+"\n\n"+e); return []; }
}
alert(getRulerEndpoints().toSource());
Copy link to clipboard
Copied
In my workplace I have CS6 and in this version it works.
Your code has been very useful to me. With the initial and final position of the ruler tool and some calculations I have obtained the value that I needed.
Thank you very much for your help
This is the final result of my code
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var docRef = app.activeDocument;
var measureRulerTool = 0;
var r = new ActionReference();
r.putProperty(stringIDToTypeID("property"), stringIDToTypeID("rulerPoints"));
r.putEnumerated(stringIDToTypeID("document"), stringIDToTypeID("ordinal"), stringIDToTypeID("targetEnum"));
var result = executeActionGet(r);
if (result.hasKey(stringIDToTypeID("points")))
{
var list = result.getList(stringIDToTypeID("points"));
var posXinitial = list.getObjectValue(0).getUnitDoubleValue(stringIDToTypeID("x"));
var posYinitial = list.getObjectValue(0).getUnitDoubleValue(stringIDToTypeID("y"));
var posXfinal = list.getObjectValue(2).getUnitDoubleValue(stringIDToTypeID("x"));
var posYfinal = list.getObjectValue(2).getUnitDoubleValue(stringIDToTypeID("y"));
var legX = Math.abs(posXinitial-posXfinal);
var legY = Math.abs(posYinitial-posYfinal);
measureRulerTool = Math.round(Math.sqrt(legX*legX + legY*legY));
}
if (measureRulerTool > 0)
{
var w = docRef.width.value / measureRulerTool;
docRef.resizeImage(UnitValue(w,"cm"),null,1000,ResampleMethod.BICUBIC);
}
else { alert("sin regla"); }
app.preferences.rulerUnits = startRulerUnits;

