Skip to content

Instantly share code, notes, and snippets.

@bryanbuchanan
Last active July 8, 2025 04:24
Show Gist options
  • 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');
}
@ericadyson
Copy link

ericadyson commented May 20, 2025 via email

@candiceda
Copy link

@schroef ericadyson is correct, you are a star! I have tested v0.1.7.1 using a simple 100x100cm square on a 600x600cm artboard and the old version gave an area of 100cm2 (100x too small) and v0.1.7.1 gave an area of 10,000cm2 which is correct! I need to do some further testing on more complicated designs, but for now it looks very promising. A HUGE THANK YOU :)

@schroef
Copy link

schroef commented May 20, 2025

@candiceda

Make sure not use difficult compounds shape with negative shapes. I'm trying to figure out what's going wrong in those cases. It has issues when a group consists of multiple compundpathitems with negative (cutout) shapes and return faulty values

@candiceda
Copy link

@schroef is it that it includes the negative/cutout area in the area calculation?

@candiceda
Copy link

image
Seems to work for something like this. But is this too simple?

@schroef
Copy link

schroef commented May 20, 2025

@candiceda

Those simple shapes work. I'll post a more complex shape when I'm at work. It when there is a group build of sub items which are compound shapes. Each of these have lots of cutouts. This is where it goes wrong. I must have made an error somehwere

@schroef
Copy link

schroef commented May 20, 2025

@candiceda

Look here is an example of a more complex shape. The item in the middle is a group build of 4 compoundPathItem, noticed how the end result is wrong. It should be 250.0952 if my math is still correct
Compound with negatives wrong total number

If i run on a single section, it does work
single compoundPathItem works correctly

When i run a multi selection of compoundPathItems, it also seems to work correct. I did some tricks applying some filters and expanding it, to make sub items work. But something is going wrong
multi compoundPathItem works correctly

Its really depends on how the shape is build. Because if i group the the above version and then run it, it also returns that 250....

@schroef
Copy link

schroef commented May 20, 2025

Notice this, look how they are looking to be the same in terms of structure. The outcome is different though, i dont get why that is?!
compoundPathItem structure issue

@schroef
Copy link

schroef commented May 20, 2025

Check the sub items, i noticed that a shape can be build in different manner and looking the same. The way i am calculating compounds with negative shapes is by checking the polarity. Normally a cutout is negative. But it seems the most outer shape can also be negative and still show the same. Look at the images below. They look the same, but the outer shape has different polarity. Its kinda weird that its actually showing the shape correctly

Example negative polarity on outer shape
compoundpathitem with negative polarity

Example positive polarity on outer shape
compoundpathitem with positive polarity

@candiceda
Copy link

@schroef are you only finding this with the design above or have you seen the same in other cases? I am not sure I can replicate the error.

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