Skip to main content
Participating Frequently
August 4, 2012
Question

Convert CSV to Swatch Library?

  • August 4, 2012
  • 4 replies
  • 15979 views

Hi everyone

I have a list of 300+ colors that I need to make into a swatch library for Illustrator. The data looks like this:

GREEN GRASS,127,187,0

PALE YELLOW,241,235,135

LIGHT YELLOW,238,231,93

DAFFODIL,249,231,21

MOONBEAM,249,223,22

etc.

It's RGB I think. In any case, I am just starting with Illustrator and I know NOTHING about scripting. Can anyone help me get aaaaalllll these colors into a swatch library, please? I am getting a migraine just thinking about putting them in one by one. LOL

I found something here, but that didn't work for me. I get an error on processing on line 75. http://forums.adobe.com/message/2877951

Error 24: app.doScript is not a function, Line 75 _> app.dpScript(speakThis, 1095978087); //AppleScript.

I get as far as choosing the csv file, and then I get the error. I think this outputs as CMYK, but not sure. Does anyone know of another script, or can anyone help me out?

Thanks,

Gina

This topic has been closed for replies.

4 replies

Tom Winkelmann
Inspiring
December 3, 2020

Here are both scripts...(Adobe has destroyed the original ones)

 

#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);
}
#target illustrator

function test(){
  var doc = app.activeDocument;
  var arr = [["name", "cyan", "magenta", "yellow", "black"]], thisSwatch, thisSwatchColorSpotColor;
  for(var i = 0;  i < doc.swatches.length;  i++){
    thisSwatch = doc.swatches[i];
    if(thisSwatch.color.typename != "SpotColor"){
        continue;
    }
    thisSwatchColorSpotColor = thisSwatch.color.spot.color;
    if(thisSwatchColorSpotColor.typename != "CMYKColor"){
        continue;
    }
    arr.push([
      thisSwatch.name,
      Math.floor(thisSwatchColorSpotColor.cyan),
      Math.floor(thisSwatchColorSpotColor.magenta),
      Math.floor(thisSwatchColorSpotColor.yellow),
      Math.floor(thisSwatchColorSpotColor.black)
    ]);
  };
  var newFile = File("~/Desktop/SwatchList.csv");
  newFile.open("w");
  newFile.write(arr.join("\n"));
  newFile.close();
};
test();

 

 

kings_freak
New Participant
December 8, 2020

Thank you so much for this! I don't know if it is a PC or Mac thing but I had to take the s off of documents in lines 31 and 32 to make it work on my Mac.  After I did that, it worked beautifully in Illustrator 2021.

Inspiring
April 4, 2021

Hi kings_freak! Could you send to me your adjusted script? I have the same problem...

Thank you!!

 

CarlosCanto
Brainiac
September 23, 2020

please post your csv file to analyze

New Participant
September 24, 2020

Thanks for the quick reply. 

the content of the csv file is:

name,RED,GREEN,BLUE
one,1,1,1
two,2,2,2
three,3,3,3

https://www.dropbox.com/sh/9dvyz1vb7j3aqnx/AAC_WGTDBhVI8_3fgo0J9dkBa?dl=0

kings_freak
New Participant
December 3, 2020

Hello,

I am getting the same error.  Did you figure out what the issue was?

Thanks!

Jmsa15369610
New Participant
July 21, 2016

Hello Everyone,

I have used the script for the export of the .csv file to create the color swatches using RGB values and this works OK.

But there is a problem that it adds QUOTES to the beginning of the name after exported, is there any way these QUOTES can be removed or not be added wile exporting?

these swatches can be renamed one by one but I would like to rename them all at once, I have used a renaming script but it does not recognize these quotes at the beginning of each swatch name. it changes the name bu the quotes.

Thank you all for any help you can provide and it will be well appreciated it.

New Participant
September 23, 2020

I couldn't make this work under CC.
It keeps giving me back this message:
Color values are out of range?

Can anyone help what to change to be able to use it in CC? Thank you

Inspiring
August 5, 2012

Funny, someone has just asked a similar question only this last couple of days… My memory is shot I don't recall that script at all… It will error at that line because Illustrator doesn't have a method to do AppleScript… ( it only makes the mac speak the errors anyhow )

Ginakra67Author
Participating Frequently
August 5, 2012

Hi

I went looking for the other thread you said was started in the last couple of days and found it. My search terms didn't pick it up for some reason..Oh, I found the thread I referenced by google so it must not have parsed the newer thread yet. I was desperate so I tried the script in the older thread, lol. I didn't know if it would work or not. Um..not.

I have a windows pc, btw.

I was hoping there would be something out there I could use without bothering anyone, but I haven't seen anything. I'd appreciate the help, but I can understand that someone creating the script for me is a big favor. The thread is two years old, so I think it's understandable that you don't remember it, lol.

Thanks,

Gina

Inspiring
February 4, 2014

Dave, I agree with Larry -- there seems nothing wrong with my script:

I get the error message you mentioned if I save a file as a Word document and then manually rename it to CSV. Of course that will not work -- CSV files are supposed to be plain ASCII text. Could this be a PICNIC problem?


I created the CSV using Excel (also attempted it in TextEdit) and edited the script using ExtendedScript Toolkit