Skip to main content
j.khakase
Inspiring
June 28, 2023
Answered

How to adjust vertical spacing between Selected artboards by script?

  • June 28, 2023
  • 1 reply
  • 746 views

Is there a way to adjust vertical spacing between Selected artboards by script?

I know 'Rearrange all' can do this, but it affects to all artboards

 

This topic has been closed for replies.
Correct answer femkeblanco

For a column of artboards, targeted by selecting the items within the artboards:

var spacing = Number(prompt("", "100"));
var doc = app.activeDocument;

// find relevant artboards and items
var artboards = [];
var collection = [];
for (var i = 0; i < doc.artboards.length; i++) {
    var bounds1 = doc.artboards[i].artboardRect;
    var subcollection = [];
    for (var j = 0; j < doc.selection.length; j++) {
        var bounds2 = doc.selection[j].geometricBounds;
        if ((bounds2[2] > bounds1[0] && bounds2[0] < bounds1[2]) &&
            (bounds2[3] < bounds1[1] && bounds2[1] > bounds1[3])) {
            if (!elementIsInArray(doc.artboards[i], artboards)) {
                artboards.push(doc.artboards[i]);
            }
            subcollection.push(doc.selection[j]);
        }
    }
    if (subcollection.length > 0) {
        collection.push(subcollection);
    }
}

// sort relevant artboards from top to bottom
artboards.sort(function(a, b) {
    return b.artboardRect[1] - a.artboardRect[1];
});

// move relevant artboards and items
for (var i = 1; i < artboards.length; i++) {
    var ABR1 = artboards[i - 1].artboardRect;
    var ABR2 = artboards[i].artboardRect;
    var y1 = ABR2[1];
    artboards[i].artboardRect = [
        ABR2[0], ABR1[3] - spacing,
        ABR2[2], ABR1[3] - spacing + (ABR2[3] - ABR2[1])
    ];
    var y2 = artboards[i].artboardRect[1];
    for (var j = 0; j < collection[i].length; j++) {
        collection[i][j].translate(0, y2 - y1);
    }
}
doc.selection = null;

function elementIsInArray(e, a) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] == e) {
            return true;
        }
    }
    return false;
}

NB. Items spanning two artboards will cause unexpected results.

1 reply

femkeblanco
femkeblancoCorrect answer
Legend
June 28, 2023

For a column of artboards, targeted by selecting the items within the artboards:

var spacing = Number(prompt("", "100"));
var doc = app.activeDocument;

// find relevant artboards and items
var artboards = [];
var collection = [];
for (var i = 0; i < doc.artboards.length; i++) {
    var bounds1 = doc.artboards[i].artboardRect;
    var subcollection = [];
    for (var j = 0; j < doc.selection.length; j++) {
        var bounds2 = doc.selection[j].geometricBounds;
        if ((bounds2[2] > bounds1[0] && bounds2[0] < bounds1[2]) &&
            (bounds2[3] < bounds1[1] && bounds2[1] > bounds1[3])) {
            if (!elementIsInArray(doc.artboards[i], artboards)) {
                artboards.push(doc.artboards[i]);
            }
            subcollection.push(doc.selection[j]);
        }
    }
    if (subcollection.length > 0) {
        collection.push(subcollection);
    }
}

// sort relevant artboards from top to bottom
artboards.sort(function(a, b) {
    return b.artboardRect[1] - a.artboardRect[1];
});

// move relevant artboards and items
for (var i = 1; i < artboards.length; i++) {
    var ABR1 = artboards[i - 1].artboardRect;
    var ABR2 = artboards[i].artboardRect;
    var y1 = ABR2[1];
    artboards[i].artboardRect = [
        ABR2[0], ABR1[3] - spacing,
        ABR2[2], ABR1[3] - spacing + (ABR2[3] - ABR2[1])
    ];
    var y2 = artboards[i].artboardRect[1];
    for (var j = 0; j < collection[i].length; j++) {
        collection[i][j].translate(0, y2 - y1);
    }
}
doc.selection = null;

function elementIsInArray(e, a) {
    for (var i = 0; i < a.length; i++) {
        if (a[i] == e) {
            return true;
        }
    }
    return false;
}

NB. Items spanning two artboards will cause unexpected results.

j.khakase
j.khakaseAuthor
Inspiring
June 29, 2023

It doesn't move artboards with artwork

femkeblanco
Legend
June 29, 2023

A bug was fixed. Try again.