• Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
    Dedicated community for Japanese speakers
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
    Dedicated community for Korean speakers
Exit
0

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

Enthusiast ,
Oct 15, 2019 Oct 15, 2019

Copy link to clipboard

Copied

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;
}

 

 

 

 

TOPICS
Scripting

Views

2.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 24, 2024 May 24, 2024

Copy link to clipboard

Copied

quote

The script is getting the app and not the document swatches. 

 

By @rob day

 

And the solution is so simple... 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 24, 2024 May 24, 2024

Copy link to clipboard

Copied

Great! 

 

You are right, my script was just interacting with the default swatches in my installation. I never even checked what swatches were in Jeremy@m's document, and jumped to the wrong conclusion when testing.

 

My version of the script works even without any open documents. I don't recall whether it was intentionally, but it ought to be much more useful in this updated version.  Thanks Rob Day!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 24, 2024 May 24, 2024

Copy link to clipboard

Copied

LATEST

This works great!
Thanks, @rob day for your help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 07, 2023 Feb 07, 2023

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied


@Roger Breton wrote:

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.


 

activeDocument - doesn't have swatches collecton - only document have - in Illustrator.

 

I'm not JS guy, but this should work:

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

for (var i = 0; i <= Roger.swatches.length-1; i++){	
	var currentSwatch = Roger.swatches[i];
...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied

But the rest won't...

 

In Illustrator, swatch have those properties - screenshot is from VB:

RobertTkaczyk_0-1710441350043.png

 

then Color can be LabColor:

RobertTkaczyk_1-1710441378657.png

 

So you first need to check if currentSwatch is LabColor type - then refer to it's 3x values:

 

result += i.toString() + ';' + currentSwatch.name + ';' + currentSwatch.color.l + ';' + currentSwatch.color.a + ';' + currentSwatch.color.b + '\r\n';

 

Not sure if in JS it will be l, a, b or L, A, B.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2024 Mar 15, 2024

Copy link to clipboard

Copied

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.

 

Screen Shot 14.png

 

Screen Shot 16.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 15, 2024 Mar 15, 2024

Copy link to clipboard

Copied

Rob, can you share your "Import CSV to InDesign swatches" script?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2024 Mar 15, 2024

Copy link to clipboard

Copied

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;
app.scriptPreferences.measurementUnit = MeasurementUnits.INCHES
var sArray = []
var c, ns, sr;

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

    sr = doc.pages[0].rectangles.add({geometricBounds:[0, 0, 2, 2], fillColor:ns.name});
    sArray.push(sr)
};   

//makes a group of rectangles with the new swatch fills
var sGroup = doc.pages[0].groups.add(sArray);
sGroup.name = "LabSwatches"


/**
* Read a text file 
*  the fileā€™s path 
*  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 
*  the document to add the swatch to 
*  swatch name 
*  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);
}

 

The example .csv is attached

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 15, 2024 Mar 15, 2024

Copy link to clipboard

Copied

Works perfect šŸ™‚ 
THANK YOU!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied

Overall , very frustrating beccause there is no documentation around.

 

Hi @Roger Breton , Illustrator and InDesign do not share the same scripting API, so the script above would not work with Illustrator.

 

When running from VS Code you have to set a launch.json file for the scriptā€™s parent folderto specify which application you are targeting when you run the script from VSCode. The documentation for setting up a launch.json file for a folder of scripts can be found her:

 

https://blog.developer.adobe.com/extendscript-debugger-for-visual-studio-code-public-release-a2ff616...

 

The complete object API documentation for InDesign can be found here:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#about.html

 

Illustratorā€˜s is here:

https://www.indesignjs.de/extendscriptAPI/illustrator-latest/#Application.html

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied

Thanks Rob for your relevant suggestion. I don't have enough script developement, these days, to set the launch.json file. When I hit the "Run & Debug" icon, VScode prompts me accordingly everytime. It's annoying but it works šŸ™‚
Any reason, in your opinion, why the online document should reside on a German page?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied

All 3 links are loading in english for me.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied

Yes, in "English" but the friendly ".de" extension tells you this is not in the US. Weird.
It does not matter. I'm just saying...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 14, 2024 Mar 14, 2024

Copy link to clipboard

Copied

quote

Yes, in "English" but the friendly ".de" extension tells you this is not in the US. Weird.
It does not matter. I'm just saying...


By @Roger Breton

 

This should explain a lot:

https://www.indesignjs.de/extendscriptAPI/illustrator-latest/#about.html

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 15, 2024 Mar 15, 2024

Copy link to clipboard

Copied

Hallo Robert,

 

Vielen danke fƻr der dokumentation!!! Es ist wunderbar!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 15, 2024 Mar 15, 2024

Copy link to clipboard

Copied

quote

Hallo Robert,

 

Vielen danke fƻr der dokumentation!!! Es ist wunderbar!!!


By @Roger Breton

 

It's not mine. 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines