Skip to content

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 */
/*
Script grabbed from
https://gist.github.com/bryanbuchanan/11387501
fixes negative areas "bug" (some areas are shown negative)
and shows result in square milimeters, given that's
the default unit used in document.
*/
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
// TODO, if no selection returned, objects.length is undefined
// and shows an error, not important, and still works fine, though
if (objects.length > 0) {
var totalArea = 0;
for (var i = 0; i < objects.length; i++) {
if (objects[i].area) {
// If negative, make it positive
var totalArea = totalArea + Math.abs(objects[i].area);
}
}
// 12.4451 is a multiplier that converts area returned to square milimeters.
// I guess Illustrator returns inches or feet, no idea really.
// Anyway, this value has been cherry picked from my own calculations.
totalArea = 12.4451 * totalArea / 10000;
// this part helps presenting a prettier alert
var area = "area";
if (i != 1) {
area = "areas";
}
// Conversions
/*var ppi = 72;
var areaInInches = Math.round((totalArea / ppi / ppi) * 100) / 100;
if (areaInInches < 0) var areaInInches = -areaInInches;*/
// Display, I assume that 1 square milimeter = 1 square meter.
alert('Shape Area\n' + totalArea.toFixed(2) + ' square meters \n' + i + " " + area);
}
}
@nanoSpawn

This comment has been minimized.

Copy link
Owner Author

@nanoSpawn nanoSpawn commented Jan 28, 2016

Changed the way I make the area always positive, leaving it to be handled by the Math object.
Also, limiting the decimals shown in the resulting alert.

@Plomhub

This comment has been minimized.

Copy link

@Plomhub Plomhub commented Nov 28, 2017

Hi NanoSpawn. I really, really, appreciated your script. I'm going to need it and i wanted to say thank you. I also have to ask you a little help: doing some testing i noticed that if i divide an area A from an area B (with pathfinder -> divide) the script result area is A+B instead of B-A. Can you help me find a solution for that? Again, thank you very much. Have a nice day.

@Paputto0

This comment has been minimized.

Copy link

@Paputto0 Paputto0 commented Apr 13, 2020

The script is quite helpful. Thanks! I just got one tip for you:

` // 12.4451 is a multiplier that converts area returned to square milimeters.
// I guess Illustrator returns inches or feet, no idea really.
// Anyway, this value has been cherry picked from my own calculations.

    totalArea = 12.4451 * totalArea / 10000;`

In my opinion it would be wise to use those numbers as a constant for readability :)

@nanoSpawn

This comment has been minimized.

Copy link
Owner Author

@nanoSpawn nanoSpawn commented Apr 13, 2020

@Plomhub It's unforgivable I missed your comment back in the day. Since I never returned to this repo, and somehow the mail was skipped, didn't see it. I'll try to have a look into that and fix it.

@Paputto0 You've got a fair point there. Didn't do it back in the day because the number never appears anymore, could also even simplify the operation doing a simple multiplication to 0,00124451, reducing it at a single math operation. Will attempt to find time this weekend to fix this. Didn't expect a fork I quickly made to solve a problem of mine would get reviewed.

@schroef

This comment has been minimized.

Copy link

@schroef schroef commented Feb 13, 2021

You have some errors in this script, i think your method of multiplying is wrong. Also at the end you state "1mm = 1meter" that kinda weird right.
On a test square of 1cm it returns 1 m square.

PS illustrator works in points, the original script showed this i believe and also has a working method for this. Im busy making a dialog for this script and im trying to solve the issue with compound paths and compoundshapes. I thought it was working, someone posted a method to do recursive checks, but that cant do CompoundPathItems properly neither.

This is what i got so far, i added some options

getShapeArea-dialog-v002
getShapeArea-dialog-v002-alert

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