Skip to content

Instantly share code, notes, and snippets.

@creold
Created June 6, 2024 05:20
Show Gist options
  • Save creold/366084d8e2becbae5074263c68bc8883 to your computer and use it in GitHub Desktop.
Save creold/366084d8e2becbae5074263c68bc8883 to your computer and use it in GitHub Desktop.
Group objects on document artboards. Adobe Illustrator script
/*
Group objects on document artboards. Skips locked or hidden objects
Author: Sergey Osokin, email: hi@sergosokin.ru
Check my other scripts: https://github.com/creold
Donate (optional):
If you find this script helpful, you can buy me a coffee
- via Buymeacoffee: https://www.buymeacoffee.com/aiscripts
- via Donatty https://donatty.com/sergosokin
- via DonatePay https://new.donatepay.ru/en/@osokin
- via YooMoney https://yoomoney.ru/to/410011149615582
*/
//@target illustrator
app.preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
function main() {
var CFG = {
aiVers: parseFloat(app.version),
isMac: /mac/i.test($.os)
};
if (!documents.length) {
alert('Error\nOpen a document and try again', 'Script error');
return;
}
if (CFG.aiVers < 16) {
alert('Wrong app version\nSorry, script only works in Illustrator CS6 and later', 'Script error');
return;
}
var doc = app.activeDocument;
// DIALOG
var win = new Window('dialog', 'Group Artboard Contents');
win.orientation = 'column';
win.alignChildren = ['fill', 'fill'];
// RANGE
var rangePnl = win.add('panel', undefined, 'Artboards Range');
rangePnl.orientation = 'row';
rangePnl.alignChildren = ['fill', 'fill'];
rangePnl.margins = [10, 15, 10, 7];
var isAll = rangePnl.add('radiobutton', undefined, 'All');
var isCurr = rangePnl.add('radiobutton', undefined, 'Current');
var isCstm = rangePnl.add('radiobutton', undefined, 'Custom');
isCstm.value = true;
var idxPnl = win.add('panel', undefined, 'Indexes as in Panel');
idxPnl.orientation = 'row';
idxPnl.alignChildren = ['fill', 'fill'];
idxPnl.margins = [10, 15, 10, 7];
var startGrp = idxPnl.add('group');
startGrp.alignChildren = ['left', 'center'];
var startLbl = startGrp.add('statictext', undefined, 'Start:');
startLbl.justify = 'left';
var startInp = startGrp.add('edittext', undefined, '1');
startInp.characters = 4;
startInp.enabled = isCstm.value;
var endGrp = idxPnl.add('group');
endGrp.alignChildren = ['left', 'center'];
var endLbl = endGrp.add('statictext', undefined, 'End:');
endLbl.justify = 'left';
var endInp = endGrp.add('edittext', undefined, doc.artboards.length);
endInp.characters = 4;
endInp.enabled = isCstm.value;
var isAddName = win.add('checkbox', undefined, 'Rename Group as Artboard');
isAddName.value = true;
// Buttons
var btns = win.add('group');
btns.alignChildren = ['fill', 'fill'];
var cancel, ok;
if (CFG.isMac) {
cancel = btns.add('button', undefined, 'Cancel', { name: 'cancel' });
ok = btns.add('button', undefined, 'Ok', { name: 'ok' });
} else {
ok = btns.add('button', undefined, 'Ok', { name: 'ok' });
cancel = btns.add('button', undefined, 'Cancel', { name: 'cancel' });
}
var prgGroup = win.add('group');
var progBar = prgGroup.add('progressbar', [20, 5, 230, 10], 0, 100);
isAll.onClick = isCurr.onClick = function () {
startInp.enabled = endInp.enabled = false;
}
isCstm.onClick = function () {
startInp.enabled = endInp.enabled = true;
}
cancel.onClick = win.close;
ok.onClick = function () {
var length = doc.artboards.length;
if (isAll.value) {
for (var i = 0; i < length; i++) {
groupArtboard(doc, i, isAddName.value);
progBar.value = parseInt(100 * (i + 1) / length);
win.update();
}
} else if (isCurr.value) {
groupArtboard(doc, doc.artboards.getActiveArtboardIndex(), isAddName.value);
progBar.value = 100;
} else {
var startIdx = parseInt(startInp.text) - 1 || 0;
var endIdx = parseInt(endInp.text) || length;
if (isNaN(startIdx) || startIdx < 0 || startIdx >= length) {
alert('Start index is invalid', 'Input error');
return;
}
if (isNaN(endIdx) || endIdx < startIdx || endIdx > length) {
alert('End index is invalid', 'Input error');
return;
}
var rangeLength = endIdx - startIdx;
for (var i = startIdx; i < endIdx; i++) {
groupArtboard(doc, i, isAddName.value);
progBar.value = parseInt(100 * (i - startIdx + 1) / rangeLength);
win.update();
}
}
app.executeMenuCommand('deselectall');
win.close();
}
win.center();
win.show();
}
// Group all items on artboard
function groupArtboard(doc, i, isAddName) {
app.executeMenuCommand('deselectall');
doc.artboards.setActiveArtboardIndex(i);
doc.selectObjectsOnActiveArtboard();
if (app.selection.length >= 2) {
app.executeMenuCommand('group');
} else if (app.selection.length === 0 || app.selection[0].typename !== 'GroupItem') {
return;
}
// Rename new group or exists
if (isAddName) app.selection[0].name = doc.artboards[i].name;
}
// Run script
try {
main();
} catch (e) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment