Skip to content

Instantly share code, notes, and snippets.

@bryanbuchanan
Last active October 28, 2024 16:16
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');
}
@schroef
Copy link

schroef commented Feb 8, 2024

@whitehorn799

thanks for highlighting this. It seems there is indeed and issue somewhere. I check the raw output of the area. Thats the negative number in the screengrab. Right is the number from that big white square. Which does have its awkward not rounded number 717 x x716.9996 px
convert issue points

I need to go over the code and see whats cause this.
Whats weird is that i checked it with lower values and it always seemed to return proper area values. like says 10x10 or 100x100
I think its an issue with using pixels and the conversion. If i use mm as ruler input and use those values of the square. I get 252.942 mm x 252.942 mm = 63979.655364 from a calculator and 63979.57105 from the script. Still not correct, but its closer

I think its due to my hot mess of using different decimals. i should look into this and make this neater code.
conversion-code-hot-mess

@whitehorn799
Copy link

Yeah, there seems to be a narrow margin of error that gets exponentially more noticeable. Glad to help where I can!

@schroef
Copy link

schroef commented Feb 15, 2024

@whitehorn799
Im still working on some other issues inbumpedninto. I noticed when a selection consists of multiple compoundpathitems, it would return the shape count as 1. Currently working on that. I believe the calculations are now fixed. As far as I can do that. I've noticed that still sometimes it returns a none round number even when the length and with are round numbers.. the pixel area is now as close as I can get it

@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 ¯_(ツ)_/¯

@floodwayprintco
Copy link

Love this! I think it would fit into a project I'm working on.

Does anyone think it would be possible to give the total area of every spot colour in a document? I am limited by having to select the shapes.

@schroef
Copy link

schroef commented Aug 22, 2024

@draugmot

now compound shape

Yeah, this original version is great but is minimal. You can also have a look at my altered version. There is still a minor issue now, it has some issues sometimes with a combination of pathItems and compoundPathItems mixed together.
https://github.com/schroef/Illustrator-Scripts/blob/master/GetShapeArea-dialog.jsx

getShapreArea-v015

@schroef
Copy link

schroef commented Aug 22, 2024

@floodwayprintco

Im not sure adding this to the script will help. Its kinda of a specific workflow

Have a look at my version. It has been updated with more functionality
https://github.com/schroef/Illustrator-Scripts/blob/master/GetShapeArea-dialog.jsx

You can easily use magic wand to iterate over colors and than run the script each time

getShapreArea-v015

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