Skip to content

Instantly share code, notes, and snippets.

@creold
Last active November 8, 2024 14:09
Show Gist options
  • Save creold/5d0296a79485c33f56853db306d80c38 to your computer and use it in GitHub Desktop.
Save creold/5d0296a79485c33f56853db306d80c38 to your computer and use it in GitHub Desktop.
Output swatches as a group of text objects, with each name colored with the swatch. Adobe Illustrator script
/*
Output swatches as a group of text objects, with each name colored with the swatch
Discussion: https://community.adobe.com/t5/illustrator-discussions/simple-script-is-it-possible/td-p/13944314
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 isOnlySpot = false; // true - output spot colors, false - all swatches
var fontSize = 8; // Pt
var typeface = 'Arial-BoldMT';
var isUseUppercase = true; // Convert name to uppercase
var pads = 20; // Artboard paddings
var distX = 4; // Horizontal distance between text
var distY = 2; // Vertical distance between text
if (!documents.length) {
alert('No documents\nOpen a document and try again', 'Script error');
return;
}
var doc = app.activeDocument;
var swNames = getSwatchNames(doc, isOnlySpot);
if (!swNames.length) {
var msg = isOnlySpot? 'No spot colors found' : 'No process or spot colors found';
alert(msg, 'Script error');
return;
}
var allTfs = [];
var lay = getEditableLayer(doc);
var idx = doc.artboards.getActiveArtboardIndex();
var abRect = doc.artboards[idx].artboardRect;
var totalW = abRect[2] - abRect[0] - 2 * pads;
var x = abRect[0] + pads;
var y = abRect[1] - pads;
var curW = 0;
for (var i = 0, len = swNames.length; i < len; i++) {
try {
var tf = addText(doc, lay, swNames[i], isUseUppercase, fontSize, typeface);
// Start a new row if total width exceeds given value
if (curW + tf.width > totalW) {
x = abRect[0] + pads;
y -= tf.height + distY;
curW = 0;
}
tf.position = [x, y];
x += tf.width + distX;
curW += tf.width + distX;
allTfs.push(tf);
} catch (err) {}
}
if (allTfs.length) groupItems(allTfs, 'Swatches Legend');
}
// Get document swatch names
function getSwatchNames(doc, isOnlySpot) {
var arr = [];
var swatches = doc.swatches.getSelected();
if (!swatches.length) swatches = doc.swatches;
for (var i = 0; i < swatches.length; i++) {
var sw = swatches[i];
var swColor = sw.color;
if (/nocolor|gradient|pattern/i.test(swColor)) continue;
if (swColor == '[SpotColor]' && swColor.spot.colorType == ColorModel.REGISTRATION) continue;
if ((isOnlySpot && swColor == '[SpotColor]') || !isOnlySpot) arr.push(sw.name);
}
return arr;
}
// Get layer to create object
function getEditableLayer(doc) {
var aLay = doc.activeLayer;
if (aLay.visible && !aLay.locked) return aLay;
for (var i = 0, len = doc.layers.length; i < len; i++) {
var curLay = doc.layers[i];
if (curLay.visible && !curLay.locked) {
doc.activeLayer = curLay;
return curLay;
}
}
doc.layers[0].visible = true
doc.layers[0].locked = false;
doc.activeLayer = doc.layers[0];
return doc.layers[0];
}
// Add colored text object with swatch name
function addText(doc, lay, str, isUseUppercase, size, typeface) {
var tf = lay.textFrames.add();
tf.contents = isUseUppercase ? str.toUpperCase() : str;
tf.textRange.characterAttributes.size = size;
try {
tf.textRange.characterAttributes.textFont = app.textFonts.getByName(typeface);
} catch (err) {}
var sw = doc.swatches.getByName(str);
tf.textRange.characterAttributes.fillColor = sw.color;
return tf;
}
// Move items to single group
function groupItems(arr, str) {
var grp = arr[0].layer.groupItems.add();
grp.name = str;
for (var i = 0, len = arr.length; i < len; i++) {
arr[i].move(grp, ElementPlacement.INSIDE);
}
}
// Run script
try {
main();
} catch (err) {}
@creold
Copy link
Author

creold commented Jul 20, 2023

More about script in Telegram channel.

swatchLegendAsText

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