Copy link to clipboard
Copied
Hi,
I made 2 scripts that used to work great, but no longer are. Any advice?
This is what I've tried thus far to no avail:
Script 1 takes a csv file with RGB info and loads the colors as swatches. When it works, it tells me to select a CSV file before creating the swatches.
Script 2 takes swatches and make them into little rectangle with the color name on it and the RGB values.
Copy link to clipboard
Copied
Hi,
Can you send me your scripts and CSV files?
you can attach here or mail me at (personal info removed by moderator)
Copy link to clipboard
Copied
Here are the scripts... and I've attached a representative csv file.
Script 1 (pasted from jsx file):
#target illustrator
function main() {
if (isOSX()) {
var csvFile = File.openDialog('Select a CSV File', function(f) {
return (f instanceof Folder) || f.name.match(/\.csv$/i);
});
} else {
var csvFile = File.openDialog('Select a CSV File', 'comma-separated-values(*.csv):*.csv;');
}
if (csvFile != null) {
fileArray = readInCSV(csvFile);
var columns = fileArray[0].length;
//alert('CSV file has ' + columns + ' columns…');
var rows = fileArray.length;
//alert('CSV file has ' + rows + ' rows…');
if (columns == 4 && rows > 0) {
exchangeSwatches(csvFile);
} else {
var mess = 'Incorrect CSV File?';
isOSX ? saySomething(mess) : alert(mess);
}
} else {
var mess = 'Ooops!!!';
isOSX ? saySomething(mess) : alert(mess);
}
}
main();
function exchangeSwatches(csvFile) {
// var docRef = app.documents.add();
var docRef = app.activeDocument;
var swatchgroup = docRef.swatchGroups.add();
swatchgroup.name = csvFile.name;
with(docRef) {
/* for (var i = swatches.length-1; i >= 0; i--) {
swatches[i].remove();
} */
for (var a = 0; a < fileArray.length; a++) {
var n = fileArray[a][0]; // First Column is name
if (n == 'Cyan' || n == 'Magenta' || n == 'Yellow' || n == 'Black') {
n = n + '-???'; // Reserved swatch name;
}
r = parseFloat(fileArray[a][1]); // Second Column is Red
g = parseFloat(fileArray[a][2]); // Third Column is Green
b = parseFloat(fileArray[a][3]); // Forth Column is Bloo
if (r >= 0 && r <= 255 && g >= 0 && g <= 255 && b >= 0 && b <= 255) {
var color = new RGBColor;
color.red = r;
color.green = g;
color.blue = b;
var swatch = swatches.add();
swatch.name = n;
swatch.color = color;
swatchgroup.addSwatch(swatch);
} else {
var mess = 'Color values are out of range?';
isOSX ? saySomething(mess) : alert(mess);
}
}
}
}
function readInCSV(fileObj) {
var fileArray = new Array();
fileObj.open('r');
fileObj.seek(0, 0);
while (!fileObj.eof) {
var thisLine = fileObj.readln();
var csvArray = thisLine.split(',');
fileArray.push(csvArray);
}
fileObj.close();
return fileArray;
}
function saySomething(stringObj) {
var speakThis = 'I say, "' + stringObj + '"';
alert(speakThis);
}
function isOSX() {
return $.os.match(/Macintosh/i);
}
And script 2 (also pasted from JSX file):
/////////////////////////////////////////////////////////////////
// Render Swatch Legend v1.4.4 -- CC
//>=--------------------------------------
//
// This script will generate a legend of rectangles for every swatch in the main swatches palette.
// You can configure spacing and value display by configuring the variables at the top
// of the script.
// update: v1.1 now tests color brightness and renders a white label if the color is dark.
// update: v1.2 uses adobe colour converter, rather than rgb colour conversion for a closer match.
// update: v1.3 adds multiple colour space values based on array printColors.
// update: v1.4.1 Updated by CarlCanto > https://community.adobe.com/t5/illustrator-discussions/illustrator-javascript-render-swatch-legend-l...
// update: v1.4.2 Only on selected Rombout Versluijs
// update: v1.4.3 Added HEX colors Rombout Versluijs
// update: v1.4.4 Added Split by Color Component Rombout Versluijs
// LAB values by Carlos Canto - 09/16/2020
// reference: https://community.adobe.com/t5/illustrator/illustrator-javascript-render-swatch-legend-lab-colour-va...
//>=--------------------------------------
// JS code (c) copyright: John Wundes ( john@wundes.com ) www.wundes.com
// copyright full text here: http://www.wundes.com/js4ai/copyright.txt
//
// Edits by Adam Green (@wrokred) www.wrokred.com
//////////////////////////////////////////////////////////////////
doc = activeDocument,
swatches = doc.swatches.getSelected(),
cols = 50, // number of columns in group
displayAs = "RGBColor"
printColors = ["RGB"], // RGB, CMYK, LAB and/or GrayScale
colorSeparator = " ", // Character used to separate the colours eg "|" output = R: XXX|G: XXX|B: XXX
splitColorComponents = false;
textSize = 10, // output text size value in points
rectRef = null,
textRectRef = null,
textRef = null,
swatchColor = null,
w = 125;
h = 192,
h_pad = 10,
v_pad = 10,
t_h_pad = 10,
t_v_pad = 10,
x = null,
y = null,
black = new GrayColor(),
white = new GrayColor();
black.gray = 100;
white.gray = 0;
activeDocument.layers[0].locked = false;
var newGroup = doc.groupItems.add();
newGroup.name = "NewGroup";
newGroup.move(doc, ElementPlacement.PLACEATBEGINNING);
for (var c = 0, len = swatches.length; c < len; c++) {
var swatchGroup = doc.groupItems.add();
swatchGroup.name = swatches[c].name;
x = (w + h_pad) * ((c) % cols);
y = (h + v_pad) * (Math.round(((c+2) + .03) / cols)) * -1;
rectRef = doc.pathItems.rectangle(y, x, w, h);
swatchColor = swatches[c].color;
rectRef.fillColor = swatchColor;
textRectRef = doc.pathItems.rectangle(y - t_v_pad, x + t_h_pad, w - (2 * t_h_pad), h - (2 * t_v_pad));
textRef = doc.textFrames.areaText(textRectRef);
textRef.contents = swatches[c].name + "\r" + getColorValues(swatchColor);
textRef.textRange.fillColor = is_dark(swatchColor) ? white : black;
textRef.textRange.size = textSize;
rectRef.move(swatchGroup, ElementPlacement.PLACEATBEGINNING);
textRef.move(swatchGroup, ElementPlacement.PLACEATBEGINNING);
swatchGroup.move(newGroup, ElementPlacement.PLACEATEND);
}
function getColorValues(c, spot) {
if (c.typename) {
if (c.typename == "SpotColor") {
return getColorValues(c.spot.color, c.spot);
};
switch (c.typename) {
case "RGBColor":
sourceSpace = ImageColorSpace.RGB;
colorComponents = [c.red, c.green, c.blue];
break;
}
var outputColors = new Array();
for (var i = printColors.length - 1; i >= 0; i--) {
colorType = printColors[i] == "HEX" ? "Indexed": printColors[i];
targetSpace = ImageColorSpace[colorType] ;
if (printColors[i] == 'LAB' && spot && spot.spotKind == 'SpotColorKind.SPOTLAB') {
outputColors[i] = spot.getInternalColor();
} else if(printColors[i] == 'HEX') {
if (app.activeDocument.documentColorSpace == DocumentColorSpace.CMYK) {
colorArray = [c.cyan, c.magenta, c.yellow, c.black];
// [Math.round(c), Math.round(m), Math.round(y), Math.round(k)]
rgbConv = app.convertSampleColor(ImageColorSpace["CMYK"], colorArray, ImageColorSpace["RGB"], ColorConvertPurpose.defaultpurpose);
outputColors[i] = [rgbConv[0].toString(16), rgbConv[1].toString(16), rgbConv[2].toString(16)];
} else{
outputColors[i] = [c.red.toString(16), c.green.toString(16), c.blue.toString(16)];
}
}
else {
outputColors[i] = app.convertSampleColor(sourceSpace, colorComponents, targetSpace, ColorConvertPurpose.previewpurpose);
}
for (var j = outputColors[i].length - 1; j >= 0; j--) {
colorComp = splitColorComponents == true ? printColors[i].charAt(j) + ": " : "";
if(isNaN(outputColors[i][j])){
outputColors[i][j] = colorComp + outputColors[i][j];
} else {
outputColors[i][j] = colorComp + Math.round(outputColors[i][j]);
}
if (j == outputColors[i].length - 1) {
outputColors[i][j] += "\r";
};
};
outputColors[i] = outputColors[i].join(colorSeparator);
if(!splitColorComponents) outputColors[i] = printColors[i]+" "+outputColors[i]
};
return outputColors.join("");
}
return "Non Standard Color Type";
}
function is_dark(c) {
if (c.typename) {
switch (c.typename) {
}
}
}
Copy link to clipboard
Copied
Hi @Elizabeth27148842zcxt
the os check does not return true or false for me. I replaced this line
return $.os.match(/Macintosh/i); in function isOSX
with
Copy link to clipboard
Copied
Hello @Elizabeth27148842zcxt,
We hope Stefan's suggestions have been helpful in resolving the issue. If you still need assistance, please feel free to reply to this thread. We're here to support you.
Thanks,
Anubhav
Copy link to clipboard
Copied
I don't see any problem running a CSV script. Mac OS + Illustrator CC 2020. Try swatch_group_from_csv.jsx, only the first CSV line should be the table header.