Skip to content

Instantly share code, notes, and snippets.

@creold
Last active November 8, 2024 14:12
Show Gist options
  • Save creold/50165f03645376365df782b60269c326 to your computer and use it in GitHub Desktop.
Save creold/50165f03645376365df782b60269c326 to your computer and use it in GitHub Desktop.
Switches the spot colors in the specified swatch group to the spot color values of the selected group. Adobe Illustrator script
/*
Switches the spot colors in the specified swatch group to the spot color values of the selected group or by index matching
Discussion: https://community.adobe.com/t5/illustrator-discussions/a-script-to-look-at-one-group-og-swatches-and-replace-with-another-group-of-swatches/td-p/14070309
Author: Sergey Osokin, email: [email protected]
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
// Main function
function main() {
var destName = 'Working Pallete'; // Edit destination group name
if (!documents.length) return;
var doc = app.activeDocument;
try {
var destGroup = doc.swatchGroups.getByName(destName);
} catch (e) {
alert('Group ' + destName + ' not found');
return;
}
var swGroups = [];
for (var i = 0; i < doc.swatchGroups.length; i++) {
if (doc.swatchGroups[i].name !== '') {
swGroups.push(doc.swatchGroups[i].name);
}
}
if (swGroups.length == 0) {
alert('No swatch groups found');
return;
}
var win = new Window('dialog', 'Swatches Replacement');
win.alignChildren = 'fill';
win.add('statictext', undefined, 'Destination: ' + destName);
var swList = win.add('dropdownlist', undefined, swGroups);
swList.selection = 0;
var isByOrder = win.add('checkbox', undefined, 'Replace by swatches order');
var btns = win.add('group');
var cancelBtn = btns.add('button', undefined, 'Cancel', {name: 'cancel'});
var rplcBtn = btns.add('button', undefined, 'Replace Colors', {name: 'ok'});
cancelBtn.onClick = win.close;
rplcBtn.onClick = function() {
var selGroup = swList.selection.text;
if (selGroup === destName) {
alert('Select a group other than destination');
return;
}
replaceSwatches(destGroup, selGroup, isByOrder.value);
win.close();
};
win.show();
}
function replaceSwatches(dest, src, isByOrder) {
var doc = app.activeDocument;
var isRgb = /rgb/i.test(doc.documentColorSpace);
try {
var selGroup = doc.swatchGroups.getByName(src);
} catch (e) {
alert('Group ' + src + ' not found');
return;
}
var destColors = dest.getAllSwatches();
var selColors = selGroup.getAllSwatches();
for (var j = 0; j < destColors.length; j++) {
var sw = destColors[j];
var name = sw.name.toLowerCase();
if (isNotSpot(sw)) continue;
if (isByOrder) {
if (selColors[j] && !isNotSpot(selColors[j])) replaceColor(sw, selColors[j], isRgb);
} else {
for (var k = 0; k < selColors.length; k++) {
var selSw = selColors[k];
if (isNotSpot(selSw)) continue;
if (selSw.name.toLowerCase().indexOf(name) != -1) {
replaceColor(sw, selSw, isRgb);
}
}
}
}
}
function isNotSpot(sw) {
return sw.color.typename !== 'SpotColor';
}
function replaceColor(sw1, sw2, isRgb) {
var c1 = sw1.color.spot.color;
var c2 = sw2.color.spot.color;
if (isRgb) {
c1.red = c2.red;
c1.green = c2.green;
c1.blue = c2.blue;
} else {
c1.cyan = c2.cyan;
c1.magenta = c2.magenta;
c1.yellow = c2.yellow;
c1.black = c2.black;
}
}
// Run script
try {
main();
} catch (e) {}
@creold
Copy link
Author

creold commented Sep 10, 2023

New version — ColorGroupReplacer.
More about script in Telegram channel.

replaceSwatchGroup

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment