Skip to main content
Andreas Jansson
Inspiring
October 15, 2019
Question

Script for exporting LAB Color swatch values to Excel (CSV)

  • October 15, 2019
  • 3 replies
  • 9399 views

Not sure this landed on the right place. Where is the scripting part of the forum? Was it deleted? All that work...

 

(This post is not a question.)

Below you will find a sample script for exporting LAB color swatches from InDesign. InDesign seem to have difficulties keeping thousands of swatches in its palette, so take care. A variant of this script made for Illustrator or Photoshop would probably have been a better idea.

 

 

 

/*
	Script for exporting LAB colours from InDesign Swatches. 
	Number, Name, L, A, and B columns semi colon separated.
	By Andreas Jansson, Code Combinations AB, Sweden, 2019.
	
	Description:
	First import a library of Pantone Colours so that they are present in the swatches palette of InDesign (you can export swatches from Photoshop, to an ASE file that is importable in InDesign).
	Then alter the path below to a folder of your choice and run this script with InDesign.
	The resulting file, I pasted into Excel.
*/

var result = '';
for (var i = 0; i <= app.swatches.length-1; i++){	
	var currentSwatch = app.swatches[i];
	if (currentSwatch.hasOwnProperty('space') && currentSwatch.space == ColorSpace.LAB){
		var val = currentSwatch.colorValue;
		result += i.toString() + ';' + currentSwatch.name + ';' + val[0] + ';' + val[1] + ';' + val[2] + '\r\n';
	}
}
result = 'N:o;Name;Lightness;A;B' + '\r\n' + result;
var outPath = 'C:/temp/test/out.txt';  // Put an existing path here
alert(writeToFile (result, outPath));

function writeToFile(fileContents, destinationFilePath){
	var myFileOut = new File(destinationFilePath);
	myFileOut.open('w');
	// myFileOut.encoding = 'UTF-8';
	myFileOut.write(fileContents);
	myFileOut.close();
	return new File(destinationFilePath).fsName;
}

 

 

 

 

3 replies

Roger Breton
Legend
February 7, 2023

Hi Andreas,

 

Thanks for the script. I need to tell you that, in v18.1 x64 on Windows, I had to modify your script by adding the "activeDocument" everywhere otherwise it would result "nothing". Here is the modified script that is working for me :

var result = '';
var Roger = app.activeDocument.swatches.length;

for (var i = 0; i <= app.activeDocument.swatches.length-1; i++){	
	var currentSwatch = app.activeDocument.swatches[i];
	if (currentSwatch.hasOwnProperty('space') && currentSwatch.space == ColorSpace.LAB){
		var val = currentSwatch.colorValue;
		result += i.toString() + ';' + currentSwatch.name + ';' + val[0] + ';' + val[1] + ';' + val[2] + '\r\n';
	}
}
result = 'N:o;Name;Lightness;A;B' + '\r\n' + result;
var outPath = 'C:/temp/test/out.txt';  // Put an existing path here
alert(writeToFile (result, outPath));

function writeToFile(fileContents, destinationFilePath){
	var myFileOut = new File(destinationFilePath);
	myFileOut.open('w');
	// myFileOut.encoding = 'UTF-8';
	myFileOut.write(fileContents);
	myFileOut.close();
	return new File(destinationFilePath).fsName;
}

It took me a while to figure the problem. Had to download and install VScode and Adobe's ExtendScript Plug-in (v2.0.3). Overall , very frustrating beccause there is no documentation around.

Roger Breton
Legend
March 14, 2024

Update: March 14 2024

I tried running the script from VScode, using Adobe ExtendScrip Plug-in installed.

The script returned nothing when ran against the current Adobe Illustrator document? Seems the the 

currentSwatch.space

property does not exist in Illustrator?

But the scrupt will run fine against an Adobe InDesign open document since InDesign natively "honors" CIE Lab color model as far as swatch names are concerned.

rob day
Community Expert
Community Expert
March 15, 2024

Thank you very much, Robert.

In case you're interested, I painted a bunch of Liquitex Acrylic paint onto some canvas and proceeded to measure each one using an X-Rite SP64 sphere spectrophotometer (I could have used an i1pro or some other but I happen to also have an SP64 in good order) into CIE Lab D50/2. You see, once I have all the measurements, my question becomes "Where do I enter these values in my computer?". Ultimately, I want the freedom of using those Lab values anywhere, such as in Excel or, ideally, Matlab, so that I can plot them in 3D for my students to see and compare against the sRGB gamot (or some other), and find the closest PANTONE Coated V4 swatch and Munsell notations. But, as you can see, I am trying to avoid having to retype these 42 triplets: only want to enter them once and use "many". InDesign or Illustrator are good places since then I can give the ASE to my students for them to use in their asignments.


But, as you can see, I am trying to avoid having to retype these 42 triplets: only want to enter them once and use "many"

 

Sounds like you want to create swatches from a list or spreadsheet of Lab values?

 

@Andreas Jansson ’s script is exporting InDesign Swatch values to a text file. My InDesign example script above from Nov 2021 does the opposite—reads a CSV file and creates swatches from the provided values. The CSV named LabColors.csv is on the desktop, and has 4 columns for the color name and its Lab values.

 

 

Participant
November 8, 2022

I need to get CMYK Swatch values into an Excel Spreadsheet. 5 Columns, Swatch Name, C, M, Y, K.
Does anybody have an Apple Script to do this? Or any other recommendations?

Andreas Jansson
Inspiring
November 8, 2022

My LAB version above script can easily be changed into any color space. Here is one for CMYK. Just change the local result path, and import the resulting csv file into Excel, after running the script.

You can use Javascript which is used for platform independent scripting in InDesign.

 

// My LAB version script can easily be changed into any color space. CMYK.
var result = '';
for (var i = 0; i <= app.swatches.length-1; i++){	
	var currentSwatch = app.swatches[i];
	if (currentSwatch.hasOwnProperty('space') && currentSwatch.space == ColorSpace.CMYK){
		var val = currentSwatch.colorValue;
		result += i.toString() + ';' + currentSwatch.name + ';' + val[0] + ';' + val[1] + ';' + val[2] + ';' + val[3] +  '\r\n';
	}
}
result = 'N:o;Name;C;M;Y;K' + '\r\n' + result;
// var outPath = 'C:/temp/test/out.txt';  // Put an existing path here
var outPath = app.activeDocument.filePath.parent + '/' + 'out.txt';
var resultFilePath = writeToFile (result, outPath);
alert('File path: ' + resultFilePath);

function writeToFile(fileContents, destinationFilePath){
	var myFileOut = new File(destinationFilePath);
	myFileOut.open('w');
	// myFileOut.encoding = 'UTF-8';
	myFileOut.write(fileContents);
	myFileOut.close();
	return new File(destinationFilePath).fsName;
}
Participant
November 8, 2022

I should have mentioned that I'm on a Mac. Looks like this might be geared towards Windows, based on the C:/temp path. Is that correct? I appreciate the help!

Participant
November 10, 2021

Good Day Everyone, 

 

I am a new Adobe Photoshop and InDesign user and am looking for some help or direction. I need to import a color library using the color name and L a b values for a set of colors into my  inDesign Swatch Library. Does anyone know of an available script to do this? I saw a post from Mark that had the formatted spreadsheet to do this and it had the MacOS script but not the PC javascript. The requestor was using a Mac. If anyone could point me in the right direction or suggest a script to do this on my PC it would be greatly appreciated. My data is in the following format in excel.  

 

Thank you! 

 

NAMEL*a*b*
SWATCH 150.00-30.0020.00
SWATCH 253.00-32.0021.00
SWATCH 356.00-34.0022.00
SWATCH 459.00-36.0023.00
rob day
Community Expert
Community Expert
November 10, 2021

Hi @ColorDyeingMan2021 Save your csv named LabColors.csv to the desktop, and try this:

 

var path = File(Folder.desktop + "/LabColors.csv");
var cTable = readFile(path);
//split the table into an array of rows
var r = cTable.split("\n");

var doc = app.activeDocument;
var c, ns;

for (var i = 1; i < r.length-1; i++){
    //split the row into an array of cells
    c = r[i].split(",");
    ns = makeSwatch(doc, c[0]); 
    ns.model = ColorModel.PROCESS;
    ns.space = ColorSpace.LAB;
    ns.colorValue = [Number(c[1]),Number(c[2]),Number(c[3])]
};   


/**
* Read a text file 
* @param the file’s path 
* @return the file’s contents 
* 
*/

function readFile(p) {
	var f = new File(p);
	f.open("r");  
	var x = f.read();  
	f.close();
	return x; 
}


/**
* Makes a new swatch 
* @param the document to add the swatch to 
* @param swatch name 
* @return the new or existingswatch 
* 
*/
function makeSwatch(d, n){
    var s;
    try {
        d.colors.add({name:n});
    }catch(e) {
        s = d.colors.itemByName(n);
    } 
    return d.colors.itemByName(n);
}

 

Also, if the color channels are evenly incremented you wouldn’t need to load a spreadsheet—you should be able to increment the values in a loop

Participant
November 10, 2021

Hi rob day, 

 

Thank you so much. I am not a programmer but was able to get the jsx file created (after a couple of tries and an error) and added as a script in InDesign and to try on the stored csv. Success!!! The colors and data values look great. I really appreciate this. One more question. If I wanted to store the swatches to a specific swatch library can /could I do that using this script? Or is this something I would do post importing? I want to be able to save a color library and share as an ASE file eventually.  I will look for more details on managing libraries and see if I can figure that out.

 

Again, thank you.This is gonna save me hours of time.