Skip to content

Instantly share code, notes, and snippets.

@bryanbuchanan
Last active April 1, 2024 09:32
Show Gist options
  • Star 62 You must be signed in to star a gist
  • Fork 17 You must be signed in to fork a gist
  • Save bryanbuchanan/11387501 to your computer and use it in GitHub Desktop.
Save bryanbuchanan/11387501 to your computer and use it in GitHub Desktop.
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');
}
@whitehorn799
Copy link

Cool, thanks for updating this and helping out! I appreciate all the work you've done. Will you update the code when you iron out those bumps you were talking about?

@schroef
Copy link

schroef commented Feb 15, 2024

@whitehorn799

Of course I will

@draugmot
Copy link

draugmot commented Mar 23, 2024

For some reasons, it got negative area for some paths, so for multiple paths it substracted them. not added. Fixed with adding math.abs:
var totalArea = totalArea + Math.abs(objects[i].area);

But unfortunately, now compound shape calculation doesn't work correctly ¯_(ツ)_/¯

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