Copy link to clipboard
Copied
Hi there.
I've got the page width of a document:
app.activeDocument.documentPreferences.pageWidth
I would like to subtract 5mm from it.
How do I do that?
I can't set the ruler units, because this script is running in a ScripUI dialog box that's running while the user is able to work in InDesign (using #targetengine)
Now, app.activeDocument.documentPreferences.pageWidth - "5mm" doesn't work.
So it looks like I have to use a UnitValue object.
But
x = UnitValue (5, "mm")
app.activeDocument.documentPreferences.pageWidth - x
doesn't work either.
So it looks like I need to convert the pageWidth into a UnitValue also.
But how do I know what units are being used?
The only option I can see is to go through all the possible MeasureUnits enumerations and compare.
But is there a simpler way of doing all this?
Many thanks,
Ariel
Copy link to clipboard
Copied
app.scriptPreferences.MeasurementUnits = MeasurementUnits.millimeters
app.activeDocument.documentPreferences.pageWidth -= 5;
Peter
Copy link to clipboard
Copied
That .MeasurementUnits should have been .measurementUnits...
Be very careful not to mess up existing scripts with this!!!
I'd always wrap these changes in a try/catch/finally:
try{
var scriptUnits = app.scriptPreferences.measurementUnits;
app.scriptPreferences.measurementUnits = MeasurementUnits.millimeters
app.activeDocument.documentPreferences.pageWidth -= 5;
}catch(err){}
finally{
app.scriptPreferences.measurementUnits = scriptUnits;
}
Copy link to clipboard
Copied
> That .MeasurementUnits should have been .measurementUnits...
And so it should have been indeed.
As to your other comment, I don't see the harm to other scripts in setting the measurement units: the units are set just for the script you're running. But, bother, does it maybe set the units for the session? Anyway, every script should set its own measurements.
Peter
Copy link to clipboard
Copied
Thanks very much guys. I didn't know about scriptPreferences. Looks like
that should do the trick!
Copy link to clipboard
Copied
pkahrel wrote:
Anyway, every script should set its own measurements.
scriptPreferences.measurementUnits didn't exist prior to version 7 (I think). Legacy scripts all use viewPreferences.verticalMeasurementUnits and viewPreferences.horizontalMeasurementUnits. With scriptPreferences set to a different measurement unit, things will start to break in weird and unexpected ways.
I don't know about you, but I have not updated all my scripts to take this into account...
I'd always make sure that the value is returned to AutoEnum.AUTO_VALUE at all costs...
Harbs