Skip to main content
Participant
January 19, 2022
Answered

Randomly order artboards.

  • January 19, 2022
  • 1 reply
  • 1858 views

Hello,
Is there a way to randomly sort artboards? The "Rearrange All Artboards" button provides a limited and orderly solution to this. But I need a solution that will sort randomly and differently every time it runs. Is this possible with a script?

This topic has been closed for replies.
Correct answer m1b

Okay I'm still curious why you need to shuffle the artboard order, so feel free to give me a hint if you can. 🙂

 

Here's a script that will do what you want I think.

- Mark

 

UPDATE: you can find an improved version of my script here.

 

 

/*

    Shuffle artboards
    for Adobe Illustrator

    by m1b
    here: https://community.adobe.com/t5/illustrator-discussions/randomly-order-artboards/m-p/12692397
    
    (Fisher Yates algorithm: sorry I can't find a source for this but not my code)

*/

if (!Array.prototype.shuffle) Array.prototype.shuffle = (function (Object, max, min) {
    "use strict";
    // fisher-yates shuffle method
    return function shuffle(picks) {
        // randomises order of an array
        if (this === null || this === undefined) throw TypeError("Array.prototype.shuffle called on null or undefined");
        var p = picks === null || picks === undefined || picks == 0 ? this.length : picks;
        var i = this.length, j = 0, temp;
        while (i--) {
            j = Math.floor(Math.random() * (i + 1));
            // swap randomly chosen element with current element
            temp = this[i];
            this[i] = this[j];
            this[j] = temp;
        }
        return this.slice(0, p);
    };
})(Object, Math.max, Math.min);


var doc = app.activeDocument,
    _artboards = [],
    indices = [];

// make an array of existing artboards
for (var i = 0; i < doc.artboards.length; i++)
    _artboards.push(doc.artboards[i]);

// copy the array for later
var deleteMeLater = _artboards.slice();

// shuffle using fisher yates method
_artboards.shuffle();

// add the artboards and match to existing
var a;
while (a = _artboards.pop()) {
    var b = doc.artboards.add(a.artboardRect);
    b.name = a.name;
    b.rulerOrigin = a.rulerOrigin;
    b.rulerPAR = a.rulerPAR;
    b.showCenter = a.showCenter;
    b.showCrossHairs = a.showCrossHairs;
    b.showSafeAreas = a.showSafeAreas;
}

// delete the original artboards
while (a = deleteMeLater.pop())
    a.remove();

 

1 reply

m1b
Community Expert
Community Expert
January 19, 2022

I'm intrigued by this question. It is easy to shuffle artboards in a script (and then do something in that shuffled order), or do you need the artboards shuffled in the document? Can you explain more? I'm quite curious.

- Mark

Participant
January 19, 2022

Yes, I need the artboards to be shuffled in the document. I have dozens of documents containing at least 100 artboards. And each of them must be sorted randomly and irrelevantly in itself.

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
January 19, 2022

Okay I'm still curious why you need to shuffle the artboard order, so feel free to give me a hint if you can. 🙂

 

Here's a script that will do what you want I think.

- Mark

 

UPDATE: you can find an improved version of my script here.

 

 

/*

    Shuffle artboards
    for Adobe Illustrator

    by m1b
    here: https://community.adobe.com/t5/illustrator-discussions/randomly-order-artboards/m-p/12692397
    
    (Fisher Yates algorithm: sorry I can't find a source for this but not my code)

*/

if (!Array.prototype.shuffle) Array.prototype.shuffle = (function (Object, max, min) {
    "use strict";
    // fisher-yates shuffle method
    return function shuffle(picks) {
        // randomises order of an array
        if (this === null || this === undefined) throw TypeError("Array.prototype.shuffle called on null or undefined");
        var p = picks === null || picks === undefined || picks == 0 ? this.length : picks;
        var i = this.length, j = 0, temp;
        while (i--) {
            j = Math.floor(Math.random() * (i + 1));
            // swap randomly chosen element with current element
            temp = this[i];
            this[i] = this[j];
            this[j] = temp;
        }
        return this.slice(0, p);
    };
})(Object, Math.max, Math.min);


var doc = app.activeDocument,
    _artboards = [],
    indices = [];

// make an array of existing artboards
for (var i = 0; i < doc.artboards.length; i++)
    _artboards.push(doc.artboards[i]);

// copy the array for later
var deleteMeLater = _artboards.slice();

// shuffle using fisher yates method
_artboards.shuffle();

// add the artboards and match to existing
var a;
while (a = _artboards.pop()) {
    var b = doc.artboards.add(a.artboardRect);
    b.name = a.name;
    b.rulerOrigin = a.rulerOrigin;
    b.rulerPAR = a.rulerPAR;
    b.showCenter = a.showCenter;
    b.showCrossHairs = a.showCrossHairs;
    b.showSafeAreas = a.showSafeAreas;
}

// delete the original artboards
while (a = deleteMeLater.pop())
    a.remove();