Skip to main content
Known Participant
Jan 31, 2023 4:25 PM UTC
Answered

Script to group artboard contents, centre and resize proportionality

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!

femkeblanco
femkeblancoCorrect answer
Legend
Mar 2, 2023 8:28 PM UTC

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);
}

 

4 replies

femkeblanco
femkeblancoCorrect answer
Legend
Mar 2, 2023 8:28 PM UTC

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);
}

 

Translate
Phil5C41Author
Known Participant
Mar 6, 2023 9:09 PM UTC

Amazing, thank you @femkeblanco 🙌

Translate
Phil5C41Author
Known Participant
Mar 2, 2023 9:25 AM UTC

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?

Translate