Script for exporting LAB Color swatch values to Excel (CSV)
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;
}
Copy link to clipboard
Copied
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!
NAME | L* | a* | b* |
SWATCH 1 | 50.00 | -30.00 | 20.00 |
SWATCH 2 | 53.00 | -32.00 | 21.00 |
SWATCH 3 | 56.00 | -34.00 | 22.00 |
SWATCH 4 | 59.00 | -36.00 | 23.00 |
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Unless something has changed with the new ID version, Libraries are not scriptable, and I don’t think there is a way to add an .ase file to a Library, but it’s easy to select the swaches and save an .ASE after you run the script.
You could make a matrix of rectangles filled with the Lab colors, and add that to the Library as a Graphic. If the user dragged the matrix graphic on the a document, the fill colors would be added to the Swatches panel, but an .ase seems simpler.
Copy link to clipboard
Copied
Thank you for the advice. The graphic is an interesting idea.
Copy link to clipboard
Copied
This version creates a group of rectangles in the upper left corner of page 1, with the new Lab swatches as fills. If you drag the group into the CC Library, make a new document, and drag the library group on to the new doc the swatches should get added:
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);
}
Copy link to clipboard
Copied
Hi,
creating ase files via script is possible
app.saveSwatches()
https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Application.html#d1e42253__d1e48249
But CC libraries not, true. The old-style library files on the other hand would be...
Copy link to clipboard
Copied
Thanks Jens, I was assuming @ColorDyeingMan2021 was looking for a way to distribute .ase files via a CCLibraries invite, but I don’t see a way to add .ase files to a CC or regular Library via scripting or the UI.
Another option might be to save the .ase files to a folder inside of the local Creative Cloud Files folder and share the ase folder with a group of users. In that case the .ase files would sync to the group’s computers as they are created.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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;
}
Copy link to clipboard
Copied
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!
Copy link to clipboard
Copied
I guess you have never used a script in InDesign, right?
Open the scrits panel (Windows / Utilities / Scripts). In the foldout menu of that panel, select to locate the scripts panel path in Finder. Create a text file, paste my code into it, save the files with the ending .jsx and, in that folder that opened up in Finder. Now the scripts panel will show the name of the script in Indesign.
Open a document with CMYK swatches in it, and double click the script in the Scripts panel (in InDesign).
As I wrote before, you would have had to change my path to one that was valid on your computer.
But now I made an update to the script above, so that the script saves the file to the folder of the current InDesign document instead. Just make sure that the document is saved, so that it has a path.
app.activeDocument.filePath.parent + '/' + 'out.txt'
I repeat: Javascript is platform independent, meaning that you can use javascript as well as applescript for Indesign scripting on Mac.
Copy link to clipboard
Copied
I just tried using this script in InDesign 19.3 and only see the following swatches in the out.txt file:
- Registration
- Paper
- Black
Is anyone able to get this to have their swatch list included in the out.txt file?
I'm using an INDD file that has a couple of thousand CMYK swatches. I also tried it using a document that only had a couple of CMYK swatches.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Yes
Copy link to clipboard
Copied
Hi Jeremy@M , Which version of @Andreas Jansson ’s script are you running—there‘s the one from the original post and there’s a different one from Nov 2022. The Nov 2022 version works for me in CC2024
Copy link to clipboard
Copied
I've been attempting to work with the version from Nov 08, 2022 because I'm hoping to use the CMYK version, which I believe is the one I replied to. I'm also using CC2014 but just noticed there's an update available for 19.4 that I'll give a try.
Copy link to clipboard
Copied
No dice after updating InDesign to 19.4 (I even copied the script and pasted into a new jsx).
I'll give it a try on one of my Intel macs when I get a chance.
@rob day thanks for your responses.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
This is the script I was using. I didn't change the file path and had the out.txt go to where the file is located.
Copy link to clipboard
Copied
Hi Jeremy!
I downloaded your two files (the document and the script) and ran them. The 'Untitled.indd' file was in my Downloads folder, so the out.txt file ended up in the root directory of my user folder. Please ensure you are looking at the correct 'out.txt' file. To be certain, delete the existing 'out.txt' file so you can confirm that it is recreated by your script and not corrupted or locked in some strange way.
I am in a Windows environment (currently using InDesign 19.3 x64). I get the 4-9 rows as well, with CMYK numbers:
N:o;Name;C;M;Y;K
1;Registration;100;100;100;100
2;Paper;0;0;0;0
3;Black;0;0;0;100
4;C=100 M=0 Y=0 K=0;100;0;0;0
5;C=0 M=100 Y=0 K=0;0;100;0;0
6;C=0 M=0 Y=100 K=0;0;0;100;0
7;C=15 M=100 Y=100 K=0;15;100;100;0
8;C=75 M=5 Y=100 K=0;75;5;100;0
9;C=100 M=90 Y=10 K=0;100;90;10;0
To continue troubleshooting the error, could you open the script in Adobe ExtendScript Toolkit CC and step through it? When you reach line 4 in the script, paste app.swatches.length into the console and press Enter to see the result. It should be 10.
Copy link to clipboard
Copied
Thanks for taking a look. I was reviewing the correct out.txt. @rob day's update is working for me now!
Copy link to clipboard
Copied
The script is getting the app and not the document swatches. Try this:
var result = '';
var sw = app.activeDocument.swatches.everyItem().getElements()
for (var i = 0; i < sw.length; i++){
var currentSwatch = sw[i];
if (currentSwatch.hasOwnProperty('space') && currentSwatch.space == ColorSpace.CMYK){
var val = currentSwatch.colorValue;
result += i.toString() + ';' + currentSwatch.name + ';' + val[0].toFixed(1) + ';' + val[1].toFixed(1) + ';' + val[2].toFixed(1) + ';' + val[3].toFixed(1) + '\r\n';
}
}
alert(result)
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 + '/' + '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;
}


-
- 1
- 2