sum of total area of text boxes on page relative to doc size in percentage

Community Beginner ,
Nov 14, 2020 Nov 14, 2020

Copy link to clipboard

Copied

Hi,

How to i capture the area of all the text boxes on one page and alert me of the total percentage relative to the doc size? for example, alert: The total is 20.45% of doc size. 

It would loop through all the text boxes, multiply w *h, add them up, then divide by doc size to get the percentage. 

I need to store each box's area, add them up and then divide by the page's area. 
This are a partial codes i found in this forum, from 10 yrs ago. Mr Kahrel wrote this.

doc = app.documents[0];
view_prefs = doc.viewPreferences.properties;
p_area = doc.documentPreferences.pageWidth * doc.documentPreferences.pageHeight;
thatNewLayer = app.activeDocument.layers.item("Object Percent of Page");

all_objects = doc.pageItems.everyItem().getElements();

for (i = 0; i < all_objects.length; i++)
    {

    o_area = object_area (all_objects);
     createTextFrame(all_objects, o_area);
     
   //code here  
 //compute the area of the each text box, add them up an divide by the p_area

    
    };

function object_area (obj)
    {
    var gb = obj.geometricBounds;
    var width = gb[3]-gb[1];
    var height = gb[2]-gb[0];
    return width * height
    }

 The funtion at the bottom throws an error. 
underfine is not an object
var width = gb[3]-gb[1]; 
I don't quite understand the parameter of (obj). 

Any pointers is appreciated in writing the array, spent couple hours googling this and playing with different scripts I found. 

Cheers

 

 

 

TOPICS
Scripting

Views

153

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Nov 16, 2020 Nov 16, 2020
Peter's script will get the area of all objects across the document. Here's a tweaked version to just get text frames. Select one text frame on the page with your black arrow tool then run this script to get the total area of all text frames on that page. Note: 1) Some pages could have different dimensions than the document setup options. This will return a wrong number for that. 2) This only gets top level text frames; not text frames that may be anchored into other text frames. If you wanted t...

Likes

translate

Translate

Translate
Contributor ,
Nov 14, 2020 Nov 14, 2020

Copy link to clipboard

Copied

change this line

o_area = object_area (all_objects);

into

o_area = object_area(all_objects[i]);

 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Nov 16, 2020 Nov 16, 2020

Copy link to clipboard

Copied

Peter's script will get the area of all objects across the document. Here's a tweaked version to just get text frames. Select one text frame on the page with your black arrow tool then run this script to get the total area of all text frames on that page. Note: 1) Some pages could have different dimensions than the document setup options. This will return a wrong number for that. 2) This only gets top level text frames; not text frames that may be anchored into other text frames. If you wanted to count anchored frames, you would get allPageItems from the parent page, then loop through that, checking if it's a Text Frame before doing the calc. 

 

 

var doc = app.documents[0];
var sel = app.selection[0];
if (sel.constructor.name != "TextFrame") {
   alert("Select a Text Frame with your black arrow tool and try again.");
   exit();
}
var p_area = doc.documentPreferences.pageWidth * doc.documentPreferences.pageHeight;
var tf_area = 0;
var frames = sel.parentPage.textFrames.everyItem().getElements();
for (var i = 0; i < frames.length; i++) {
    if (frames[i].itemLayer.visible) { 
        tf_area += object_area(frames[i]);
    } 
}
alert( ((tf_area / p_area) * 100) + "% area on page " + sel.parentPage.name); 

function object_area (obj) {
    var gb = obj.geometricBounds;
    var width = gb[3]-gb[1];
    var height = gb[2]-gb[0];
    return width * height
}

 

 

 

 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 16, 2020 Nov 16, 2020

Copy link to clipboard

Copied

wow, thank you so much. 

I will dissect it and try to learn from it.
I couldn't figure out how to do the math to add the function to the array and spit out the result.
My hat is off to you and also to crazyPanda. 

Cheers

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 16, 2020 Nov 16, 2020

Copy link to clipboard

Copied

Hi, how do I restrict it to "if visible" or "selected" textFrames? because it's couting the frames in the layers that are turn off. I tried applying a if/then, but I am not sure of the syntax and its throwing errors.

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Nov 16, 2020 Nov 16, 2020

Copy link to clipboard

Copied

Edited my original post to check visibilty of the frame's layer. 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 16, 2020 Nov 16, 2020

Copy link to clipboard

Copied

thank you so much, once again! 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines