Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to select ALL objects and get bounding box of objects???

Community Beginner ,
Jun 22, 2009 Jun 22, 2009

I am trying to find the bounding box on a document.  In Illustrator you can select ALL objects and see the bounding box (width & height).  But I need to do this in a script.  How do you select all the objects on the document?  and How do you get the bounding box of those objects?

TOPICS
Scripting
4.2K
Translate
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
New Here ,
Jun 30, 2009 Jun 30, 2009

What you need? If you need the document width and height just you open the illustrator file in textpad you can find out the full document width and height(BoundingBox). If you need every object widh and height just you select the every object using PageItems and then you get out the bounding box values.

Translate
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
Advocate ,
Jun 30, 2009 Jun 30, 2009

Not sure right now if there is a direct way, but you could loop through all of the items and get the lowest .left and .top and the highest (.left + .width) and (.top + .height) then from there you could get the width and height of the group of items.

Translate
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
New Here ,
Nov 13, 2011 Nov 13, 2011

Just ran into the same issue. Isn't there a shorter way to get the geometricBounds value from an array of selected pathItems (assuming you have multiple pathItems selected)? The overall height and width is already there in the toolbar (X Value, Y Value, W Value, H Value). How do we get to them from scripting?

var mydoc = app.activeDocument,

myselection = mydoc.selection[0]; // this gets the value for the topmost element only

alert(myselection.geometricBounds);

Translate
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 Expert ,
Nov 13, 2011 Nov 13, 2011

Isn't there a shorter way to get the geometricBounds value from an array of selected pathItems

are you kidding? you're lucky if you can accomplish something "the long way" scripting illustrator...

what's wrong with doing it the long way, as Mark sugested? how did you make your selection? in the GUI?

anyway, an alternative to the long way could be

- copy you selection

- create a new document

- paste there

- use visibleBounds property

Translate
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
Guru ,
Nov 14, 2011 Nov 14, 2011

Why select anything at all? Script is NOT like the GUI you don't need to select any array of objects in order to get their properties… Just target the ones you want… A document has both 'height' and 'width' properties calculated off the geometric bounds… all in points thou. Oh on second look Im commenting on the wrong post… sorry about that…

Translate
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 ,
Sep 12, 2017 Sep 12, 2017

You can duplicate all your selected objects and make the group with those objects.

For the group you can get bounding box etc.

You can delete this group afterward.

For selection all objects I use this method:

app.activeDocument.selectObjectsOnActiveArtboard();

Translate
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
New Here ,
Jun 20, 2025 Jun 20, 2025

Earlier I was wondering how I could get the bounding box of all selected paths, when I don't know if the .typename is going to be a PathItem, PlacedItem, CompoundPathItem, etc. Your message gave me the confidence to try it out:

// Get the active document
var doc = app.activeDocument;

//Get active selections
var selectedItems = doc.selection;

//Only if something is selected
if (selectedItems.length > 0) {

    // Create a NEW GROUP item (this is only to get all items group bounding box for artboard fit)
    var ghostGroup = doc.groupItems.add();


    // Iterate through the selected items and duplicate each one, to add them to a group
    for (var i = 0; i < selectedItems.length; i++) {

        // Duplicate the selected item in its current parent
        // ElementPlacement.PLACEATEND places the duplicate at the end of the parent's children
        var duplicatedItem = selectedItems[i].duplicate(selectedItems[i].parent, ElementPlacement.PLACEATEND);

        //Add duplicate selection to the NEW GROUP
        duplicatedItem.moveToEnd(ghostGroup);
    }

    //Get GROUP PATH bounds
    var geometricBounds = ghostGroup.geometricBounds;

    // geometricBounds is an array: [left, top, right, bottom]
    var left = geometricBounds[0];
    var top = geometricBounds[1];
    var right = geometricBounds[2];
    var bottom = geometricBounds[3];

    //Offset
    var offset = 36; //1/2"

    //Inset
    //var inset = 36; //1/2"

    //Get active artboard
    var activeartboard = doc.artboards.getActiveArtboardIndex();

    // Calculate new bounds based on center and new dimensions
    var newLeft = left - offset;
    var newTop = top + offset; // Illustrator's Y-axis is inverted for top/bottom
    var newRight = right + offset;
    var newBottom = bottom - offset;

    // Apply the new artboard rectangle
    doc.artboards[activeartboard].artboardRect = [newLeft, newTop, newRight, newBottom];
    
    ghostGroup.remove();

    //alert("Artboard resized successfully!");

}

 

RESULT: You can fit the artboard to your selection, no matter the .typename. It makes a duplicate of the selection, groups it (doesn't matter if is a single item), gets group bounds, fits the artboard to the groups geometricBounds (so that it fits to the path, not the stroke width), then adds an offset to the artboard.

Translate
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
New Here ,
Jun 20, 2025 Jun 20, 2025

Try this:

 

// Get the active document
var doc = app.activeDocument;

//Get active selections
var selectedItems = doc.selection;

//Only if something is selected
if (selectedItems.length > 0) {

    // Create a NEW GROUP item (this is only to get all items group bounding box for artboard fit)
    var ghostGroup = doc.groupItems.add();


    // Iterate through the selected items and duplicate each one, to add them to a group
    for (var i = 0; i < selectedItems.length; i++) {

        // Duplicate the selected item in its current parent
        // ElementPlacement.PLACEATEND places the duplicate at the end of the parent's children
        var duplicatedItem = selectedItems[i].duplicate(selectedItems[i].parent, ElementPlacement.PLACEATEND);

        //Add duplicate selection to the NEW GROUP
        duplicatedItem.moveToEnd(ghostGroup);
    }

    //Get GROUP PATH bounds
    var geometricBounds = ghostGroup.geometricBounds;

    // geometricBounds is an array: [left, top, right, bottom]
    var left = geometricBounds[0];
    var top = geometricBounds[1];
    var right = geometricBounds[2];
    var bottom = geometricBounds[3];

    //Get active artboard
    var activeartboard = doc.artboards.getActiveArtboardIndex();

    // Calculate new bounds based on center and new dimensions
    var newLeft = left;
    var newTop = top; // Illustrator's Y-axis is inverted for top/bottom
    var newRight = right;
    var newBottom = bottom;

    // Apply the new artboard rectangle
    doc.artboards[activeartboard].artboardRect = [newLeft, newTop, newRight, newBottom];
    
    ghostGroup.remove();

    alert("Artboard resized to the bounding box of all objects selected.");

}

 

Translate
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
New Here ,
Jun 21, 2025 Jun 21, 2025
LATEST

Apparently you can't modify a group item if there are hidden layers.
As discussed here: https://www.techyv.com/questions/error-8705-target-layer-cannot-be-modified/
So to work around this, I did some fixes:

// Get the active document
var doc = app.activeDocument;

//Get active selections
var selectedItems = doc.selection;

//Only if something is selected
if (selectedItems.length > 0) {

    //Create a new RGBColor object for the desired color
    //This will be used to toggle layers visible/hidden button by colors

    //VISIBLE
    var lightBlue = new RGBColor();
    lightBlue.red = 79;
    lightBlue.green = 128;
    lightBlue.blue = 255;

    //HIDDEN
    var cyan = new RGBColor();
    cyan.red = 79;
    cyan.green = 255;
    cyan.blue = 255;


    //TURN ON ALL LAYERS (if a layer is hidden, the code wonk work. So you have to turn them on. At the end you turn them back off.)
    // Iterate through all layers in the document and check their visibility
    for (var i = 0; i < doc.layers.length; i++) {

        var currentLayer = doc.layers[i];

        if (currentLayer.visible) {

            //IF LAYER IS VISIBLE

            //Assign the new color to the active layer's color property
            currentLayer.color = lightBlue;

        } else {

            //IF LAYER IS HIDDEN

            //Assign the new color to the active layer's color property
            currentLayer.color = cyan;

            //Change layer visibility to VISIBLE
            currentLayer.visible = true;
        }
    }


    // Create a NEW GROUP item (this is only to get all items group bounding box for artboard fit)
    var ghostGroup = doc.groupItems.add();


    // Iterate through the selected items and duplicate each one, to add them to a group
    for (var i = 0; i < selectedItems.length; i++) {

        // Duplicate the selected item in its current parent
        // ElementPlacement.PLACEATEND places the duplicate at the end of the parent's children
        var duplicatedItem = selectedItems[i].duplicate(selectedItems[i].parent, ElementPlacement.PLACEATEND);

        //Add duplicate selection to the NEW GROUP
        duplicatedItem.moveToEnd(ghostGroup);
    }

    //Get GROUP PATH bounds
    var geometricBounds = ghostGroup.geometricBounds;

    // geometricBounds is an array: [left, top, right, bottom]
    var left = geometricBounds[0];
    var top = geometricBounds[1];
    var right = geometricBounds[2];
    var bottom = geometricBounds[3];

    //Offset
    var offset = 36; //1/2"

    //Inset
    //var inset = 36; //1/2"

    //Get active artboard
    var activeartboard = doc.artboards.getActiveArtboardIndex();

    // Calculate new bounds based on center and new dimensions
    var newLeft = left - offset;
    var newTop = top + offset; // Illustrator's Y-axis is inverted for top/bottom
    var newRight = right + offset;
    var newBottom = bottom - offset;

    // Apply the new artboard rectangle
    doc.artboards[activeartboard].artboardRect = [newLeft, newTop, newRight, newBottom];
    
    alert("Width: " + ghostGroup.width);
    alert("Height" + ghostGroup.height);

    ghostGroup.remove();


    //TURN OFF LAYERS THAT WERE PREVIOUSLY OFF
    // Iterate through all layers in the document and check their visibility
    for (var i = 0; i < doc.layers.length; i++) {

        var currentLayer = doc.layers[i];

        //Get colors of current layer
        if (currentLayer.color instanceof RGBColor) {
            var r = Math.floor(currentLayer.color.red);
            var g = Math.floor(currentLayer.color.green);
            var b = Math.floor(currentLayer.color.blue);
            
            //alert("Active Layer Color (RGB): R=" + r + ", G=" + g + ", B=" + b);

            //Create color with current layer colors
            //VISIBLE
            var currentLayerColor = new RGBColor();
            currentLayerColor.red = r;
            currentLayerColor.green = g;
            currentLayerColor.blue = b;

        }

        //LIGHT BLUE
        //currentLayerColor.green = 128;

        //CYAN
        //currentLayerColor.green = 255;

        if (currentLayerColor.green == "255") {
            
            //LAYER COLOR CYAN
            //IF LAYER WAS PREVIOUSLY HIDDEN

            //Change layer visibility to HIDDEN
            currentLayer.visible = false;
        }

        if (currentLayerColor.green == "128") {
            
            //LAYER COLOR LIGHT BLUE
            //IF LAYER WAS PREVIOUSLY VISIBLE
        }
        
    }


}
Translate
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