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).

2 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. 🙂

Leave a Reply

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