Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to find the area of shapes in Adobe Illustrator
/* Save this file with a jsx extension and place in your
Illustrator/Presets/en_US/Scripts folder. You can then
access it from the File > Scripts menu */
var decimalPlaces = 3;
if (app.documents.length > 0) {
if (app.activeDocument.selection.length < 1) {
alert('Select a path');
} else if (app.activeDocument.selection[0].area) {
// Individual Items
var objects = app.activeDocument.selection;
} else if (app.activeDocument.selection[0].pathItems) {
// Group/Compound Shape
var objects = app.activeDocument.selection[0].pathItems;
} else {
alert('Please select a path or group.');
}
// Collect info
var totalArea = 0;
for (var i=0; i<objects.length; i++) {
if (objects[i].area) {
var totalArea = totalArea + objects[i].area;
}
}
// Conversions
var ppi = 72;
var areaIn = totalArea / ppi / ppi;
if (areaIn < 0) var areaIn = -areaIn;
var areaCm = areaIn * 6.4516;
// Display
alert('Shape Area\
' + areaIn.toFixed(decimalPlaces) + ' in² \
' + areaCm.toFixed(decimalPlaces) + ' cm² \n\
' + i + ' shapes');
}
@yeamal

This comment has been minimized.

yeamal commented Jul 29, 2015

Worked great! thanks

@y1ann15

This comment has been minimized.

y1ann15 commented Oct 9, 2015

That's awesome. Thanks!
Can also this script return millimetres or centimetres instead of inches?

@ErmiasTessema

This comment has been minimized.

ErmiasTessema commented Jan 7, 2016

This one works both area and perimeter length in 'mm'

alert("Area & Length\nArea: " + ((app.activeDocument.selection[0].area/5184)*645.16).toFixed(3) + "\nLength: " + (app.activeDocument.selection[0].length/2.834645669291).toFixed(3));

@nanoSpawn

This comment has been minimized.

nanoSpawn commented Jan 28, 2016

Works absolutely great, thanks for this script.

I forked this one in https://gist.github.com/nanoSpawn/8e8fca48d0fb85dd961e/

Basically, sometimes Illustrator returns negative areas for some shapes, and thus the sum is wrongly returned, fixed this, also I show it in square milimeters instead of inches (i'm from continental Europe). But otherwise, the original script works great.

I also automated the process using Apple Automator and assigning a shortcut (ctrl+shift+option+a), to make it even easier to use.

Hope you don't mind me forking and modifying it! I credit you as the author.

@Yona-Appletree

This comment has been minimized.

Yona-Appletree commented Jun 19, 2016

@nanoSpawn The negative areas are really useful if you want to determine the area of a compound path with "cutouts".

@Kylwell

This comment has been minimized.

Kylwell commented Mar 15, 2017

Is there a way to get this to return more than 2 decimal points?

@fairytaller

This comment has been minimized.

fairytaller commented Mar 17, 2017

Thank you so much!

Maybe I'm doing something wrong.
But

alert("Area & Length\nArea: " + ((app.activeDocument.selection[0].area/5184)*645.16).toFixed(3) + "\nLength: " + (app.activeDocument.selection[0].length/2.834645669291).toFixed(3));

does not work.
Displays: NaN

@ErmiasTessema help me please.

@GoyWebdesign

This comment has been minimized.

GoyWebdesign commented May 19, 2017

Thank you a lot for the original script, Bryan. Thank you, Ermias, for you conversion.
Works like a charm on AI CC2017.

@mattmurphy23

This comment has been minimized.

mattmurphy23 commented Jul 21, 2017

Any way to have the results go out three decimals? PDFs I create for legal review require that all math is taken out three places.
Thanks for a great script!

@brazaguy

This comment has been minimized.

brazaguy commented Dec 17, 2017

For area in CM² and for more decimal points:


/* Save this file with a jsx extension and place in your
Illustrator/Presets/en_US/Scripts folder. You can then 
access it from the File > Scripts menu */

/* Done by bryanbuchanan and reviewed by Bruno Lizidatti */

if (app.documents.length > 0) {

	if (app.activeDocument.selection.length < 1) {
		alert('Select a path first');
	} else if (app.activeDocument.selection[0].area) {
		// Individual Items
		var objects =  app.activeDocument.selection;
	} else if (app.activeDocument.selection[0].pathItems) {
		// Group/Compound Shape
		var objects = app.activeDocument.selection[0].pathItems;
	} else { 
		alert('Please select a path or group.');
	}
	
	// Collect info
	var totalArea = 0;
	for (var i=0; i<objects.length; i++) {
		if (objects[i].area) {
			var totalArea = totalArea + objects[i].area;
		}
	}
    
	// Conversions    
	var ppi = 72;
	var areaInInches = ((totalArea / ppi / ppi) * 100) / 100;
	if (areaInInches < 0) var areaInInches = -areaInInches;
	
	// Display
	alert('Shape Area\n' + areaInInches + ' square inches \n' + i + ' shapes \n' + areaInInches*6.4516 + ' cm²  \n');

}
@bryanbuchanan

This comment has been minimized.

Owner

bryanbuchanan commented Jul 28, 2018

Wow, I totally forgot about this gist. It's awesome to hear you guys are finding it useful :)

@y1ann15, I just added cm output.

@Kylwell and @mattmurphy23, I changed it to default to 3 decimal places, but also added a little variable to the very top to let you change that relatively easily.

@mammuthus

This comment has been minimized.

mammuthus commented Aug 11, 2018

@bryanbuchanan Thank for your work, Bryan, awesome script

@cmichielsen

This comment has been minimized.

cmichielsen commented Oct 31, 2018

@bryanbuchanan I love the script, it saves me a ton of time in my work. However I am having an issue when selecting multiple objects. If I select objects individually, run the scripts, and then add the totals together, I get a drastically different number than if I select multiple objects and run the script.

image

You can see here that the area of one object is around 56 square inches.

image

However, you'll notice that it is not physically possible that the total area of these 7 shapes could be just over 76 square inches. Any ideas on what is going on here? I am running the newest version of Adobe Illustrator CC 2019.

Any help would be greatly appreciated.

@MattttE

This comment has been minimized.

MattttE commented Nov 14, 2018

Getting an error after trying the script:

Error on line 3: ReferenceError: Can't find variable: app

Wondering if anyone might know what the problem is? I'm using AI CC 2015 on Mac.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment