-
-
Save bryanbuchanan/11387501 to your computer and use it in GitHub Desktop.
/* 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'); | |
} |
@SFR75
Why is this wrong?
100pt is 3.528cm > 3.528 x 3.528 = 12.445cm2
Which version of the script are you using
@SFR75 Why is this wrong? 100pt is 3.528cm > 3.528 x 3.528 = 12.445cm2 Which version of the script are you using
I was expecting to see 10.000 or something divisible by 10. Actually I thought it might be related to DPI, so I changed it to 300, but result was the same. Latest version.
@SFR75
The output is cm and inch not point, you need to select points if you want that. My guess your using the original script. Have a look at my version of this script i've added.
If you use points, you'll get that 10000pt you was expecting
@SFR75
You will see 10.000 if you use points as output, but you used inch and centimeter. Using a different metric will show a different value, thats how it works
Hi, thanks for this script. I really find it useful when trying to calculate the area of my fantasy world's tectonic plates and landmasses. I want to ask if it is just me or are the areas about 10.2% off? I used a rectangle3600x1800 pixels. I divided it up into various irregular polygons within the rectangle. After I went through and measured each "plate", I found that the total areas of the plates equaled 5,880,449.21 pixels squared, but it the area of the rectangle should properly be 6,480,000 pixels squared. Subtracting the sum from the script from the actual total gave me a difference of 599,550.79 pixels squared. Comparing the percentage difference between this and what it should be, I got a percentage difference of exactly 10.2%. I'm not sure if I'm right. I'm just a graphic designer, but I can show you what I did using the image below. What do you think?
@whitehorn799
Well doing a percentage difference I get about 9.7 percent. Perhaps this is caused by the rounding of the decimals.
Did you manual add all those number or is this done in excel with a function?
I used a calculator on this webpage
https://www.calculator.net/percent-calculator.html?c3par1=6480000&c3par2=5880449.21&ctype=3&x=Calculate#pctdifference
I got discrepancies even when the polygon was a regular shape with a whole number as an area, like a square or rectangle. Here's a funny thing I just noticed: if the polygon (regular or irregular) is not snapped to the pixel grid, there's a chance that the script's pixel result will be slightly off because Illustrator gives a fractional pixel value when it isn't aligned to the grid. I would suggest going to Edit>Preferences>General and change Keyboard Increment to 1. Then go to ...>Units and change General and Stroke to Pixels (I think the script doesn't mind whether a polygon has a stroke or not). Then, go to ...>Guides & Grid and change "Gridline every" to 1 px. Go to View>Snap to Grid, and make sure it is checked. Then, in the document, make sure your polygons do not have pixel fractions by looking at the X,Y coordinates and the Width, Height dimensions in the Transform panel.
This fixes regular polygon area calculations, but if your polygon is crazy, like some of mine, I think the script may still throw fractions of pixels, and I think that's understandable.
Another suggestion: there's a chance that totalArea will return negative, so you may want to include a line to fix that up. Again, please let me know if I am way off base, just a graphic designer here!
I still get a discrepancy after re-calculating all the plates with this new method, only this time the results are less consistent, with a 12.17% difference for pixels, 14.25% difference in miles, and a 10.17% difference in kilometers. I think it's just the nature of the beast of rounding numbers, eh?
I'm working with numbers in the millions, so would there be a way to convert the results into numbers with proper commas?
@whitehorn799
Well I'm not sure how the area is calculated. It's an internal function, so perhaps something weird is going on there. Though I've never,not to my knowledge seen faulty outcome.
If I would place a 4x3 cm or pixel format randomly on the canvas, the outcome is the same
Are you willing to share your document?
https://www.dropbox.com/scl/fi/satcipmjk8u7vcenqfbc3/Tectonics.eps?rlkey=gu2t4arwv664vk78v0ev8i48c&dl=0 You can take a look if you like. I put two white squares on there and turned off the snap to pixel grid. The area of the smaller square is about 2 hundredths off, and the square has fractional pixel dimensions. If you turn on the grid, and you give the square whole numbers as WxH dimensions, the result is correct. Like I said, though, it's quite easy and close enough, I think!
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
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.
Yeah, there seems to be a narrow margin of error that gets exponentially more noticeable. Glad to help where I can!
@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
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?
Of course I will
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 ¯_(ツ)_/¯
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.
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
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
Thank you bro! Btw, can this one work on Adobe Photoshop 2024 too?
谢谢你,兄弟!顺便说一句,这个也可以在 Adob e Photoshop 2024 上运行吗?
Ah i tried and it failed...
@V-E-R-I-T-A-S
No its an Illustrator script. Photoshop doesnt have a prop like area. But they have the measurement tool there
You'd need to convert the image to a selection, convert the selection to a path, export the path to Illustrator, then run the script in Illustrator.
@V-E-R-I-T-A-S No its an Illustrator script. Photoshop doesnt have a prop like area. But they have the measurement tool there
It seems like PS cannot measure an irregular area...
You'd need to convert the image to a selection, convert the selection to a path, export the path to Illustrator, then run the script in Illustrator.
Thx! I am turning my work into illustrator
Hi @schroef, firstly I just want to say thank you for keeping the AI Area script up-to-date for the past few years, and thank you to @bryanbuchanan for creating the script in the first place.
I have been using it for a couple of years now and for the most part, it works flawlessly. I just have one issue, when using the script in files with a large-sized canvas, the areas provided always seem to be 100x too small. I think this is because the script is reading the scale as 1:10 instead of 1:1 which seems to be an issue in older versions of AI and PDFs (https://helpx.adobe.com/uk/illustrator/kb/large-sized-canvas-troubleshooting.html)
For now I just use a work-around of manually multiplying the area provided by 100, but it would be great if you could update the script at some point to work correctly with large-sized canvases.
Thank you!
Yeah, that's is probably true. I think illustrator uses an internal scaling factor for this "new" large scale document. This script is not build for that.
Perhaps I can do some research, see if this properties are exposed for scripting. If so, than that would be an easy fix
Okay got my answer within 1 minute. Its exposed, I'll check if I can update it.
I had been working also on parts where the script still fails. That's internal compound path items. Still haven't found a method to properly do those. Tried many variations doing all kinds of automated path operators, but it keeps failing.
I'll do some work and see if I can implement the scaleFactor
@schroef thank you so much for looking into this so quickly.
What is the issue with compound paths? I thought it was providing the area accurately and I have therefore been using it to do so for a couple of years?
Well it does, but there are so manu different ways art can be created using a mix of shapes and compound paths. The issue arise when such such are combined into one, but are build of many separate sub items. There are certain cases where it's not working properly
I need to check again which cases. I made some test for this months ago. But then laid aside as I couldn't figure it out and worked on other scripts.
Ps I was just thinking. If you run alert(app.activeDocument.scaleFactor) from a simple jsx file. It will return the scale factor. If you use this in the latest getShapeArez you could apply a scale. I have not tested it yet. But I think it should work
It used to work on my PC. Now I tried your script on OSX + AI 2021. The script produces wrong result. For square with 100pt x 100pt it gives 12.603 cm2. This is quite wrong...