Sorry, it was a debugging script.
Here's the release version
/*
Sort Artboards
for Adobe Illustrator
by m1b
here: https://community.adobe.com/t5/illustrator-discussions/randomly-order-artboards/m-p/12692397
and: https://community.adobe.com/t5/illustrator-discussions/illustrator-script-to-renumber-reorder-the-artboards-with-there-position/m-p/12752568
Sorting by position compares top or left or right values to n decimal places, 3 by default
(without this limit, tiny discrepancies come in to play and cause weird results)
2023.11.15 Andrew_BJ:
- Fixed limit of 500 artboards to sort. Now limit is 999 (Limit 1000 by AI - 1)
- Fixed setup UI by selected sorting method in settings object
*/
var SortMethod = {
NAME: { sorter: sortByName, displayName: 'By Name' },
POSITION_TOP_LEFT: { sorter: sortByPositionTopLeft, displayName: 'By Position Top Left' },
POSITION_TOP_RIGHT: { sorter: sortByPositionTopRight, displayName: 'By Position Top Right' },
POSITION_LEFT_TOP: { sorter: sortByPositionLeftTop, displayName: 'By Position Left Top' },
POSITION_RIGHT_TOP: { sorter: sortByPositionRightTop, displayName: 'By Position Right Top' },
REVERSE: { sorter: reverse, displayName: 'Reverse' },
SHUFFLE: { sorter: shuffle, displayName: 'Shuffle' },
};
var settings = {
action: sortArtboards,
sortMethod: SortMethod.POSITION_TOP_LEFT,
decimalPlaces: 3,
showUI: true
};
main();
function main() {
if (settings.showUI) {
invokeUI();
} else {
settings.action.apply(null, [app.activeDocument, settings.sortMethod.sorter]);
}
}
function sortArtboards(doc, sortFunction) {
var decimalPlaces = Math.pow(10, settings.decimalPlaces);
// make an array of existing artboards
var _artboards = [];
for (var i = 0; i < doc.artboards.length; i++)
{
var a = doc.artboards[i];
var s = {name: a.name, artboardRect: a.artboardRect, srcIndex: i};
_artboards.push(s);
}
// sort
sortFunction(_artboards, decimalPlaces);
// add the artboards and match to existing
for (var i = 0; i < _artboards.length; i++)
{
var s = _artboards[i];
var a = doc.artboards[s.srcIndex];
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;
a.remove();
for (var j = i + 1; j < _artboards.length; j++)
if (_artboards[j].srcIndex > s.srcIndex)
_artboards[j].srcIndex--;
}
}
// Sorting Functions
function reverse(_artboards) {
_artboards.reverse();
}
function sortByName(_artboards) {
_artboards.sort(function (a, b) {
if (a.name < b.name) return -1;
if (a.name > b.name) return 1;
return 0;
});
}
function sortByPositionTopLeft(_artboards, decimalPlaces) {
// sorts T < B L < R
decimalPlaces = decimalPlaces || 1000;
_artboards.sort(function (a, b) {
return (
Math.round((b.artboardRect[1] - a.artboardRect[1]) * decimalPlaces) / decimalPlaces
|| Math.round((a.artboardRect[0] - b.artboardRect[0]) * decimalPlaces) / decimalPlaces
)
});
}
function sortByPositionTopRight(_artboards, decimalPlaces) {
// sorts T < B, R < L
_artboards.sort(function (a, b) {
return (
Math.round((b.artboardRect[1] - a.artboardRect[1]) * decimalPlaces) / decimalPlaces
|| Math.round((b.artboardRect[0] - a.artboardRect[0]) * decimalPlaces) / decimalPlaces
)
});
}
function sortByPositionLeftTop(_artboards, decimalPlaces) {
// sorts L < R, T < B
_artboards.sort(function (a, b) {
return (
Math.round((a.artboardRect[0] - b.artboardRect[0]) * decimalPlaces) / decimalPlaces
|| Math.round((b.artboardRect[1] - a.artboardRect[1]) * decimalPlaces) / decimalPlaces
)
});
}
function sortByPositionRightTop(_artboards, decimalPlaces) {
// sorts R < L, T < B
_artboards.sort(function (a, b) {
return (
Math.round((b.artboardRect[0] - a.artboardRect[0]) * decimalPlaces) / decimalPlaces
|| Math.round((b.artboardRect[1] - a.artboardRect[1]) * decimalPlaces) / decimalPlaces
)
});
}
function shuffle(_artboards) {
var i = _artboards.length,
j,
temp;
while (i--) {
j = Math.floor(Math.random() * (i + 1));
// swap randomly chosen element with current element
temp = _artboards[i];
_artboards[i] = _artboards[j];
_artboards[j] = temp;
}
}
function invokeUI(p) {
// add the sort methods to array
var sortMethods = [];
for (var i = 0; i < SortMethod.reflect.properties.length; i++)
if (SortMethod.hasOwnProperty(SortMethod.reflect.properties[i]))
sortMethods.push(SortMethod[SortMethod.reflect.properties[i]]);
// show UI
var dialog = makeUI(p)
dialog.center();
var result = dialog.show();
dialog = null;
// handle result
if (result === -1) {
// cancelled
return;
} else if (result === 1) {
// do action
settings.action.apply(null, [app.activeDocument, settings.sortMethod.sorter]);
}
function makeUI(p) {
var w = new Window("dialog", 'Sort Artboards'),
menuRow = w.add("Group {orientation:'row', alignment:['fill','top']}"),
// stack = w.add("Group {orientation:'stack', alignment:['fill','fill']}"),
buttonRow = w.add("Group {orientation:'row', alignment:['right','top']}");
menuRow.add("statictext", undefined, "Sort Method");
var mainMenu = menuRow.add("Dropdownlist {alignment:['left','center']}"),
cancelButton = buttonRow.add('button', undefined, 'Cancel', { name: 'cancel' }),
okButton = buttonRow.add('button', undefined, 'Sort', { name: 'ok' });
// add the sort methods to menu
var select = 0;
for (var i = 0; i < sortMethods.length; i++)
{
mainMenu.add('item', sortMethods[i].displayName);
if (settings.sortMethod.displayName == sortMethods[i].displayName)
select = i;
}
mainMenu.preferredSize.width = 180;
mainMenu.selection = mainMenu.items[select];
updateUI();
okButton.onClick = okButtonClicked;
cancelButton.onClick = cancelButtonClicked;
// mainMenu.onChange = updateUI;
return w;
function okButtonClicked() {
settings.sortMethod = sortMethods[mainMenu.selection.index];
w.close(1);
};
function cancelButtonClicked() {
w.close(-1);
}
function updateUI() {
// var s = mainMenu.selection ? mainMenu.selection.index : 0;
}
}
}