Copy link to clipboard
Copied
Are there general guidelines on how to use .as("px") with a script? I noticed a bug when calling it through UnitValue vs directly. I had to include UnitValue for 0, presumably because it's a number?
My document is 5 inches x 5 inches @ 300 dpi.
alert("doc width: "+app.activeDocument.width) // returns "5 in"
alert("doc width as px: "+app.activeDocument.width.as("px")) // returns 1500
function asPX(measure) {
alert("func as px: "+measure.as("px"))
}
asPX(app.activeDocument.width) // returns 1500
function asPXUnit(measure) {
var measure = UnitValue(measure).as("px");
alert("func unitValue as px: "+measure)
}
asPXUnit(app.activeDocument.width) // returns 360
I guess best practice may be to test and convert any numbers to measurements.
Even nesting UnitValue(UnitValue().value,UnitValue().type).as("px"), returns 360.
Copy link to clipboard
Copied
Hi, try this function;
function asPXUnit(measure) {
var a = new UnitValue(measure);
a.baseUnit = measure.baseUnit; // 0.0033333333333 in
var measure = a.as("px");
alert("func unitValue as px: "+measure);
}
asPXUnit(app.activeDocument.width); // returns 1500
Copy link to clipboard
Copied
In order to convert the width of the document to pixels, it must always be 72ppi to be obtained successfully.
I see, the problem is that the baseUnit is affected by the resolution when you put the UnitValue of the document width as an argument, right?
The immediate way to avoid it is to watch the baseUnit.
Copy link to clipboard
Copied
I'm not sure what you are asking. You should ALWAYS explicitely set the ruler units. Measurements are expressed as UnitValue which is whatever you specify. And no, conversions don't require the document to be at 72ppi. document.resolution is in pixels per inch, you can use that for conversions.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now