Skip to content

Instantly share code, notes, and snippets.

@creold
Last active July 9, 2024 13:50
Show Gist options
  • Save creold/1e0fc33ca6b9748b4b689e44d07779b4 to your computer and use it in GitHub Desktop.
Save creold/1e0fc33ca6b9748b4b689e44d07779b4 to your computer and use it in GitHub Desktop.
Adds colored text labels for cyan, magenta, yellow and black inks, and any additional spot color. Adobe Illustrator script
/*
Adds colored text labels for cyan, magenta, yellow and black inks, and any additional spot color used in each artboard
Discussion: https://community.adobe.com/t5/illustrator-discussions/adding-color-names-instead-of-color-bars/td-p/14718668
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
preferences.setBooleanPreference('ShowExternalJSXWarning', false); // Fix drag and drop a .jsx file
function main () {
if (!/illustrator/i.test(app.name)) {
alert('Wrong application\nRun script from Adobe Illustrator', 'Script error');
return false;
}
if (parseFloat(app.version) < 16) {
alert('Wrong app version\nSorry, script only works in Illustrator CS6 and later', 'Script error');
return;
}
if (!documents.length) {
alert('No documents\nOpen a document and try again', 'Script error');
return;
}
var win = new Window('dialog', 'Show Ink List v0.2.1');
win.alignChildren = ['fill', 'top'];
win.spacing = 10;
win.margins = 16;
var fontGrp = win.add('group');
fontGrp.alignChildren = ['fill', 'center'];
var fontLbl = fontGrp.add('statictext', undefined, 'Font Size, pt:');
fontLbl.preferredSize.width = 75;
var fontInp = fontGrp.add('edittext', undefined, 12);
fontInp.characters = 8;
var spGrp = win.add('group');
spGrp.alignChildren = ['fill', 'center'];
var spLbl = spGrp.add('statictext', undefined, 'Spacing, pt:');
spLbl.preferredSize.width = 75;
var spInp = spGrp.add('edittext', undefined, 15);
spInp.characters = 8;
var isUsed = win.add('checkbox', undefined, 'Show Only Used Inks');
isUsed.value = true;
isUsed.helpTip = 'Collects a ink list\nfor each artboard';
var btns = win.add('group');
var cancel = btns.add('button', undefined, 'Cancel', {name: 'cancel'});
var ok = btns.add('button', undefined, 'OK', {name: 'ok'});
var copyright = win.add('statictext', undefined, '\u00A9 Sergey Osokin. Visit Github');
copyright.justify = 'center';
cancel.onClick = win.close;
ok.onClick = function () {
var doc = app.activeDocument;
app.executeMenuCommand('deselectall');
try {
var newLayer = doc.layers.getByName('InkNames');
newLayer.locked = false;
newLayer.visible = true;
newLayer.hasSelectedArtwork = true;
// Remove previous text labels
for (var s = app.selection.length - 1;s >= 0;s--) {
app.selection[s].remove();
}
} catch (err) {
var newLayer = doc.layers.add();
newLayer.name = 'InkNames';
}
var font = parseFloat(fontInp.text);
if (isNaN(font)) font = 12;
var spacing = parseFloat(spInp.text);
if (isNaN(spacing)) spacing = 15;
var artboards = doc.artboards;
var length = artboards.length;
for (var i = 0; i < length; i++) {
app.executeMenuCommand('deselectall');
doc.artboards.setActiveArtboardIndex(i);
doc.selectObjectsOnActiveArtboard();
if (!app.selection.length) continue;
var inks = length === 1 ? getUsedInks(doc.inkList, isUsed.value)
: getArtboardInks(app.selection, doc.documentColorSpace, isUsed.value);
var ab = artboards[i];
var abRect = ab.artboardRect;
addInkNames(doc, inks, abRect[0], abRect[3], spacing, font, newLayer);
}
if (!newLayer.pageItems.length) newLayer.remove();
app.executeMenuCommand('deselectall');
win.close();
}
copyright.addEventListener('mousedown', function () {
openURL('https://github.com/creold');
});
win.show();
}
// Add a list of current ink names to a document at specified positions
function addInkNames(doc, inks, left, bottom, spacing, font, target) {
var x = left - spacing;
var y = bottom + spacing;
for (var i = 0; i < inks.length; i++) {
var inkName = inks[i].name;
var inkNameText = target.textFrames.add();
inkNameText.contents = inkName;
inkNameText.textRange.characterAttributes.size = font;
var inkNameTextChar = inkNameText.textRange.characterAttributes;
switch (inks[i].inkInfo.kind) {
case InkType.CYANINK:
inkNameText.contents = 'Process Cyan';
inkNameTextChar.fillColor = setCMYKColor([100, 0, 0, 0]);
break;
case InkType.MAGENTAINK:
inkNameText.contents = 'Process Magenta';
inkNameTextChar.fillColor = setCMYKColor([0, 100, 0, 0]);
break;
case InkType.YELLOWINK:
inkNameText.contents = 'Process Yellow';
inkNameTextChar.fillColor = setCMYKColor([0, 0, 100, 0]);
break;
case InkType.BLACKINK:
inkNameText.contents = 'Process Black';
inkNameTextChar.fillColor = setCMYKColor([0, 0, 0, 100]);
break;
case InkType.CUSTOMINK:
try {
inkNameTextChar.fillColor = doc.swatches.getByName(inkName).color;
} catch (err) {}
break;
}
inkNameText.rotate(90, true, true, true, true, Transformation.CENTER);
inkNameText.position = [x - inkNameText.width, y + inkNameText.height];
y += inkNameText.height + spacing;
}
}
// Retrieve a list of inks used in the specified items on an artboard
function getArtboardInks(items, colorMode, isUsed) {
var newDoc = app.documents.add(colorMode);
copyObjectsTo(items, newDoc);
app.executeMenuCommand('Fit Artboard to artwork bounds');
var inks = newDoc.inkList;
var result = getUsedInks(inks, isUsed);
newDoc.close(SaveOptions.DONOTSAVECHANGES);
return result;
}
function getUsedInks(inks, isUsed) {
var result = [];
for (var i = 0; i < inks.length; i++) {
if (!isUsed || inks[i].inkInfo.printingStatus === InkPrintStatus.ENABLEINK) {
result.push(inks[i]);
}
}
return result;
}
// Duplicate objects and add them to a document
function copyObjectsTo(items, doc) {
if (Object.prototype.toString.call(items) === '[object Array]') {
for (var i = 0; i < items.length; i++) {
items[i].duplicate(doc.activeLayer, ElementPlacement.PLACEATEND);
}
} else {
items.duplicate(doc.activeLayer, ElementPlacement.PLACEATBEGINNING);
}
}
// Generate solid CMYK color
function setCMYKColor(cmyk) {
var c = new CMYKColor();
c.cyan = cmyk[0];
c.magenta = cmyk[1];
c.yellow = cmyk[2];
c.black = cmyk[3];
return c;
}
// Open a URL in the default web browser
function openURL(url) {
var html = new File(Folder.temp.absoluteURI + '/aisLink.html');
html.open('w');
var htmlBody = '<html><head><META HTTP-EQUIV=Refresh CONTENT="0; URL=' + url + '"></head><body> <p></body></html>';
html.write(htmlBody);
html.close();
html.execute();
}
try {
main();
} catch (err) {}
@creold
Copy link
Author

creold commented Jul 9, 2024

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