Skip to main content
Known Participant
January 31, 2023
Answered

Script to group artboard contents, centre and resize proportionality

  • January 31, 2023
  • 2 replies
  • 925 views

Hi,

 

I've seen a script to help with portions of this, here. However, I was wondering if there's a script to work through all artboards and:
1. group artboard contents

2. centre

3. size proportionality (from the middle) to the artboard size (whichever vertically or horizontally) meet the size first

 

Possible?

 

Thanks!

Correct answer femkeblanco

This should do what you want (iterate artboards, group, centre and resize):

var doc = app.activeDocument;
var ABs = doc.artboards;
for (var i = 0; i < ABs.length; i++) {
    app.selection = null;
    ABs.setActiveArtboardIndex(i);
    doc.selectObjectsOnActiveArtboard();
    // group
    var o = doc.groupItems.add();
    for (var j = 0; j < app.selection.length; j++) {
        app.selection[j].moveToEnd(o);
    }
    var b1 = o.visibleBounds;
    var w1 = b1[2] - b1[0];
    var h1 = b1[1] - b1[3];
    var b2 = o.geometricBounds;
    var w2 = b2[2] - b2[0];
    var h2 = b2[1] - b2[3];
    // centre
    var ABR = ABs[i].artboardRect;
    o.position = [(ABR[0] + ((ABR[2] - ABR[0]) / 2)) - (w2 / 2), 
                  (ABR[1] + ((ABR[3] - ABR[1]) / 2)) + (h2 / 2)];
    // resize
    w = doc.width - (w1 - w2);
    h = doc.height - (h1 - h2);
    var f = (w1 > h1) ? w / w2 * 100 : h / h2 * 100;
    o.resize(f, f);
}

 

2 replies

femkeblanco
femkeblancoCorrect answer
Legend
March 2, 2023

This should do what you want (iterate artboards, group, centre and resize):

var doc = app.activeDocument;
var ABs = doc.artboards;
for (var i = 0; i < ABs.length; i++) {
    app.selection = null;
    ABs.setActiveArtboardIndex(i);
    doc.selectObjectsOnActiveArtboard();
    // group
    var o = doc.groupItems.add();
    for (var j = 0; j < app.selection.length; j++) {
        app.selection[j].moveToEnd(o);
    }
    var b1 = o.visibleBounds;
    var w1 = b1[2] - b1[0];
    var h1 = b1[1] - b1[3];
    var b2 = o.geometricBounds;
    var w2 = b2[2] - b2[0];
    var h2 = b2[1] - b2[3];
    // centre
    var ABR = ABs[i].artboardRect;
    o.position = [(ABR[0] + ((ABR[2] - ABR[0]) / 2)) - (w2 / 2), 
                  (ABR[1] + ((ABR[3] - ABR[1]) / 2)) + (h2 / 2)];
    // resize
    w = doc.width - (w1 - w2);
    h = doc.height - (h1 - h2);
    var f = (w1 > h1) ? w / w2 * 100 : h / h2 * 100;
    o.resize(f, f);
}

 

Phil5C41Author
Known Participant
March 6, 2023

Amazing, thank you @femkeblanco 🙌

Phil5C41Author
Known Participant
March 2, 2023

I've kind of managed to put this together using some smaller scripts and actions within a single action. The only thing I'm unable to figure out is how to repeat the action across the number of artboards. Is there a couter scrtipt out there, I've looked but can't find one?