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.

Show comment
Hide comment
@yeamal

yeamal Jul 29, 2015

Worked great! thanks

yeamal commented Jul 29, 2015

Worked great! thanks

@y1ann15

This comment has been minimized.

Show comment
Hide comment
@y1ann15

y1ann15 Oct 9, 2015

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

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.

Show comment
Hide comment
@ErmiasTessema

ErmiasTessema 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));

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.

Show comment
Hide comment
@nanoSpawn

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

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.

Show comment
Hide comment
@Yona-Appletree

Yona-Appletree Jun 19, 2016

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

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.

Show comment
Hide comment
@Kylwell

Kylwell Mar 15, 2017

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

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.

Show comment
Hide comment
@fairytaller

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

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.

Show comment
Hide comment
@GoyWebdesign

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

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.

Show comment
Hide comment
@mattmurphy23

mattmurphy23 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!

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.

Show comment
Hide comment
@brazaguy

brazaguy 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');

}

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.

Show comment
Hide comment
@bryanbuchanan

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

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.

Show comment
Hide comment
@mammuthus

mammuthus Aug 11, 2018

@bryanbuchanan Thank for your work, Bryan, awesome script

mammuthus commented Aug 11, 2018

@bryanbuchanan Thank for your work, Bryan, awesome script

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