Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

extendscript - Script measurement tool in Photoshop to measure distance between two points

Can the measurement tool be scripted to measure a custom distance? Not an existing measurement, that is.

I would have thought it possible, but Abobe's documentation are sketchy and there's not much info when it comes down to scripting the measurement tool. Or ruler, as it use to be known. More specifically a lack of examples using the ruler in a script.

Ideally I'd like to:

  • Script a measurement from x1,y1 to x2,y2
  • Determine its length, (preferably from a variable, not from a text file - see below)
  • Clear the ruler tool (so no ruler line appears)
  • Delete any measurements (so there are no recordings in the ruler window)

That's basic trig, right? But if you can't set the initial ruler then this is all for nothing.

The only thing on the list I've done is the last. :(

I stumble at the first hurdle not being able to set the ruler, which I thought would be app.activeDocument.recordMeasurements(); Only it gets returned as undefined or Unable to record measurements for selection.

As minimal working, this is pretty minimal:

 // Let's stick to pixels as the units to make things easier.

// Switch off any dialog boxes
displayDialogs = DialogModes.ERROR; // OFF 

// Get and set the units to pixels
var currentUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

// delete existing measurements
app.measurementLog.deleteMeasurements(); // THIS WORKS!!

// call the source document
var srcDoc = app.activeDocument;

// Set array to record measurements
var rulerArr = [];

// Set ruler measurement
measure_it(x1,y1,x2,y2); // this a fictional function
// not even the ScriptListener records the ruler tool in a useful form

// var d = app.activeDocument.recordMeasurements(MeasurementSource.MEASURESELECTION, rulerArr); // Unable to record measurements for selection
// var d = app.activeDocument.recordMeasurements(MeasurementSource.MEASURESELECTION); // Unable to record measurements for selection
var d = app.activeDocument.recordMeasurements(); // undefined

var ruler = app.measurementLog; // Is an object!
alert(ruler.length); // undefined

// There's an option to export to a text file
// Is that the only way to get the measurements?
// Can't this be don't internally?
var f = "D:\temp\ruler.txt";
ruler.exportMeasurements(f, MeasurementRange.ACTIVEMEASUREMENTS)
alert(app.measurementLog);


// put the units back to how it was
app.preferences.rulerUnits = currentUnits;

// Set Display Dialogs back to normal
displayDialogs = DialogModes.ALL; // NORMAL
question from:https://stackoverflow.com/questions/65857856/script-measurement-tool-in-photoshop-to-measure-distance-between-two-points

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...