Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
0

as("px") - Usage Clarification - Unit conversion

Enthusiast ,
Oct 10, 2025 Oct 10, 2025

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.

TOPICS
Actions and scripting , macOS
153
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Oct 14, 2025 Oct 14, 2025

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

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 14, 2025 Oct 14, 2025

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Oct 14, 2025 Oct 14, 2025
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines