working with measurement units in adobe extendScript

measurement_units (img from internet)
measurement_units (img from internet)

While working with Adobe ExtendScript to extend Adobe CS features (through scripts and/or plugins), there is a good chance you need to get or set an elements dimensions (document dimensions for instance).
When you ask the Software about the dimensions, it will give you back a number, but this number could be in a lot of different units (Points, Picas, Inches, Inches Decimal, Millimeters, Centimeters, Ciceros, Agates, Pixels, etc.) and it’s gonna be the same when you set a number as well.
There is a lot of different ways to handle this issue, like getting the unit as well and trying to convert them etc. but it may add a huge overload on your code, your time and a lot of unnecessary complexity which can make your code harder to debug as-well.

Here I’ll put a simple solution I use in my own codes which may come helpful to you too (This code is written for In-design but should work less or more for the whole CS collection -maybe with a bit of tweaking- ):

I just get the current document units (for both Vertical and Horizontal measurement units in In-Design), keep them in a (pair of) variable(s) (I check to see if the variable is defined before and set it if it isn’t defined already, doing this we can be sure that even if we forgot to remove the vars later on, we don’t set them to our newly defined units again), change the units (for the whole document) and run my code and then when I was done with it change it back to original units and remove the created variables.
I found this technique to be the easiest, cleanest, fastest and the most performant way to handle the units issue in extend-script.

Here is the simple code I use; two functions, one to run before the main script and one when the script was done to set everything back to what they were before we run the script:

Note: here I use the very front document but you can easily pass desired document to the function through an argument as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>