Skip to content

Instantly share code, notes, and snippets.

@alijaya
Forked from bryanbuchanan/GetShapeArea.jsx
Last active November 9, 2021 19:10
Show Gist options
  • Save alijaya/cc693da563affad034b1241f33c915cc to your computer and use it in GitHub Desktop.
Save alijaya/cc693da563affad034b1241f33c915cc 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;
function calculateArea (obj) {
if (obj.typename == "PathItem") {
return obj.area; // could be negative
} else if (obj.typename == "CompoundPathItem") {
var totalArea = 0;
for (var i=0; i<obj.pathItems.length; i++) {
totalArea += calculateArea(obj.pathItems[i]); // could be negative
}
return Math.abs(totalArea); // make sure positive
} else if (obj.typename == "GroupItem") {
var totalArea = 0;
for (var i=0; i<obj.pathItems.length; i++) {
totalArea += Math.abs(calculateArea(obj.pathItems[i])); // make sure positive
}
for (var i=0; i<obj.compoundPathItems.length; i++) {
totalArea += calculateArea(obj.compoundPathItems[i]); // already positive
}
for (var i=0; i<obj.groupItems.length; i++) {
totalArea += calculateArea(obj.groupItems[i]); // already positive
}
return totalArea; // already positive
} else { // not path, compound path or group
return 0;
}
}
function convertArea (area) {
var scaleFactor = 1;
if (app.documents.length > 0 && app.activeDocument.scaleFactor != null) {
scaleFactor = app.activeDocument.scaleFactor;
}
var ppi = 72;
var result = {};
area *= scaleFactor * scaleFactor;
result.inch = area/ppi/ppi;
result.cm = result.inch * 2.54 * 2.54;
result.m = result.cm / 10000;
return result;
}
if (app.documents.length > 0) {
var objects = app.activeDocument.selection;
var display = ["Shape Area"];
// Collect info
var totalArea = 0;
for (var i=0; i<objects.length; i++) {
var area = Math.abs(calculateArea(objects[i])); // need absolute in case of PathItems
totalArea += area;
var conv = convertArea(area);
display.push(conv.inch.toFixed(decimalPlaces) + " in² / " + conv.cm.toFixed(decimalPlaces) + "cm² / " + conv.m.toFixed(decimalPlaces) + "m²");
}
var conv = convertArea(totalArea);
display.push("Total Area: " + conv.inch.toFixed(decimalPlaces) + " in² / " + conv.cm.toFixed(decimalPlaces) + "cm² / " + conv.m.toFixed(decimalPlaces) + "m²");
// Display
alert(display.join("\n"));
}
@alijaya
Copy link
Author

alijaya commented Nov 9, 2021

Glad it's finally working :D

@schroef
Copy link

schroef commented Nov 9, 2021

Haha that is funny, i was actually looking at that feature after reading this very long thread on adobe community. What a coincidence!!!

Never thought it would be linked to that setting!

I do wonder now,why was this document read a being large scale? Was this document first setup like that and then later scaled down?

I think this could be added in the code. Apperently what adobe does is scale it in the background. That is there trick to get these huge documents working

@schroef
Copy link

schroef commented Nov 9, 2021

You could try this code and see if semething related to large scale is in the attributes. I use these simple loops to find all items available through code.

items = []
for (attr in app.activeDocument){
    items.push(attr+", ")
}
alert(items)

Then you can alter the for loop if you a attribute you think is connected, then run it again pointing to that attribute

@schroef
Copy link

schroef commented Nov 9, 2021

After checking, my earlier script did not get very far. Ive found this great read on StackExhange, it references the scripting reference PDF and the Illustrator settings file, probably it show the correct name for the scaling to get the value for scripting.

I think im gonna check tonight at home and dig through that preferences file see if i can find anything related. Though i would guess such a setting would be saved as a setting in the document. Ill chech that as well

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