Calculate area and path-length in AI using Scripts

December 1, 2013

KateBaldwin

'}}

I have needed to calculate the area of a shape in order to determine the acreage of a perspective property plot, and to measure the length of a pen-tool-traced transgenic seedling root. Measuring area, perimeter, and length is not built into Adobe Illustrator, but there is an easy work around.

This technique should work for all recent (perhaps all) versions of Adobe Illustrator. [2020 update, still works!]

  • Step 1: Either download this file, or make one yourself.
    • To make it yourself, paste this text into a simple text file (like notepad) and save it with the file extension .jsx:

alert(
"Area (millimeters): " + (Math.abs(app.activeDocument.selection[0].area/8.03521617).toFixed(4)) + " mm" +
"\nLength (millimeters): " + (app.activeDocument.selection[0].length/2.8346567).toFixed(4) + " mm" +
"\n\nArea (inches): " + (Math.abs(app.activeDocument.selection[0].area/5184).toFixed(4)) + " in" +
"\nLength (inches): " + (app.activeDocument.selection[0].length/72).toFixed(4) + " in"
);

  • Step 2: Open your file in Illustrator and select the path or object that you want to measure.
  • Step 3: Click on “FILE” and scroll down to ‘scripts.’ Select “Other Script.” A file dialog window should open up. Find and select AreaLength.JS. A small window will pop up that states the area and length/perimeter in inches.

Some details & options

Do you plan to use it often?
Then you might want this script to appear on the quick menu within illustrator so that you don’t have to navigate through your files every time you use it.
Here is where I put it on my PC to successfully make it show up in the menu:
C:\Program Files\Adobe\Adobe Illustrator CC (64 Bit)\Presets\en_US\Scripts

Rounding

This script rounds the decimal points to 4. If you want more or less, you can open AreaLength.JS in notepad/text editor and change “toFixed(4)” to a different number.

Units

Would you like to measure your paths in other units instead?
Change the text in the file.

Centimeters

alert(
"Area (centimeters): " + (Math.abs(app.activeDocument.selection[0].area/803.521617).toFixed(4)) + " cm" +
"\nLength (centimeters): " + (app.activeDocument.selection[0].length/28.346567).toFixed(4) + " cm" +
);

You can change it other units too. The default would give you the units in px, and there are 72 px per inch. Thus, in the ‘inch’ unit script we take the length is divided by 72, and the area is divided by 5184, which is 72 squared. If you make a new script, I recommend testing it on a shape of known size. You can do this by selecting the rectangle tool, single clicking once on the canvas. A dialog will pop up. Type in “1 cm” for width and “1 cm” for height (for example).

10 Replies to “Calculate area and path-length in AI using Scripts”

Caje Kindred

Hi Dr. Baldwin,

I’m a graduate student studying geology at The Ohio State University. A portion of my current research involves calculating the sum of the area of individual sand grains that I’ve manually mapped (I have a “base-map” image that I’m mapping over). Also, I map each fracture within those sand grains and need to calculate the sum of the length of those fractures (I currently use the “Document Info” window for the path length in pixels). I’ve run into some issues using a combination of various programs to accomplish this goal, so now I’m interested in writing my own Illustrator script to perform all of my desired calculations in one place. Would you mind helping me understand the few lines of code that you have written here? I am also curious if you know of a way to input a known scale for these calculations – I am not interested in the area that these grains would be if I printed them, but I am interested in the true area of the grains based on a known scale of ~50 pixels/mm. Any help would be greatly appreciated!

Best,
Caje

KateBaldwin

Hello Caje!
Yes, these units can totally be converted!
The default normal in AI is that there are 72 pixels per inch, but if your image has dimensions embedded in it, it will open at that size (even if they are not accurate).
[Please check the “document info panel” for embedded images to confirm what dimensions and resolution your photomicrographs are displaying at.]
The scripting feature that this script uses: app.activeDocument.selection[0].area, gives the area in pixels. You’ll see in the script above that I divide that by 5184 in order to get inches. That’s because 72 px per in * 72 px per in = 5184 px per square inch.
So, if your images are always going to come in at 50 px per mm but display at 72 px per inch, you could divide the area by 2,500 in order to get mm.
Be careful though, because if you get a new batch of images with a different resolution or different embedded dimensions (such that it’s displaying differently in AI), your numbers would wrong. (It makes me nervous to have a script that gives lengths different from the dimensions built into the AI document.) I would add in a warning text at the top of the script in case another lab member tries to use it years from now and doesn’t understand that, like this:

alert(
“Warning! This scrip assumes 50 px per mm. All numbers will be wrong if that is not true. Test a known length or area first” +
“\nArea (millimeters): ” + (Math.abs(app.activeDocument.selection[0].area/2500).toFixed(4)) + ” mm” +
“\nLength (millimeters): ” + (app.activeDocument.selection[0].length/50).toFixed(4) + ” mm”
);

*****But, I think you should consider a totally different option: just resize your image in AI so that 1 mm = 1 mm. Then you can use the original script included in this blog post. That seems a lot more straight forward and less prone to error. AI has a built in dimensions for your entire document that is quite reliable, so we might as well take advantage of it. If you know the width of your photomicrograph view field in real mm, you could use that info to resize the whole image. Or, if there is a scale bar in the image of known size, you could use that. Resizing the image in AI will NOT actually effect the pixels or image at all, so that’s good. We are just changing the size at which the pixel-based image is displaying in the AI document. If this doesn’t make sense, email me an example photomicrograph with an object or mark of known size and I’ll show you. 🙂

Hello Kate,
it seems that geologists have great need for your Adobe Illustrator (AI) scripts 😉

I’m a structural geologist and I was looking for a replacement of an old, venerable AI plugin provided by telegraphics* that allowed measuring lengths and areas of lines and polygons. I use this for line-length- and area-balanced geological cross sections, in which both a deformed and a retrodeformed” (=restored) cross section must be equal to satisfy material balance constraints.
The telegraphics plugins worked nicely up to AI CS6, but didn’t work anymore under the newer AI versions.
I’ve just adopted your script following your “recipe” and got it to run under AI 2021.

Thank you very much for providing your script to the community!

Very best wishes from Jena, Germany
Kamil Ustaszewski

*) http://telegraphics.com.au/sw/product/patharea

KateBaldwin

That’s awesome! I’m so glad it worked for you!
I also loved those telegraphics plugins, and it was only when they stopped working that I had to figure out a new route!

Makai

Thank you for the very, very, easy solution and script.

Saved my OCD brain from a lot of stress! : p

Rombout Versluijs

We made a better version of this using the base of Bryan Buchanan and added an interface and more options. You can find it here.
Im also going to upload a new version which can show m2 as well

https://github.com/schroef/Illustrator-Scripts/

KateBaldwin

This is great Rombout! I tried out “GetShapeArea-dialog-v005.jsx” in the lastest version of Illustrator (2023). It worked very well! In its current form, this script only gives area, not length/perimeter. But users could edit the script to include length too if they want. I also get a spurious alert box that says TRUE or FALSE when I check the box for “show total shapes,” but it still works. Thank you for making your script freely available on Git Hub!

Annaliese

Another geologist here… I tried this in Illustrator 10 and it did not work, probably because the program only seems capable of using normal Javascript. I didn’t really expect it to work, but I guess you now know the limit of which versions it will work in. 🙂 I did use the Telegraphics extension someone listed above though and that worked, just have to use a different link because they took the page down:
https://web.archive.org/web/20170710045444/http://telegraphics.com.au/sw/product/patharea
Thanks for the help!

Leave a Reply

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

' skin='skin1'}}

Kate Baldwin