Copy link to clipboard
Copied
I come across indds that predominantly contain swatches that are named differently but are the same exact color, value-wise, i.e. their C=n, M=n, Y=n, K=n, are the exact same.
I’m looking for a script that will recognize swatches with identical values, i.e. identical CMYK values, and do away with duplicate swatches that share the same values and leave only one swatch, with the name as “Name with Color Value (screenshot below).”
So in the end, there is only one instance of a specific color, and it’s name as CMYK, etc.
Any help, as always, is very much appreciated.
Thanks.
Name with Color Value.
This may work:
...function normalizeCMYK(/*Document*/doc, swa,a,r,o,t,k,i)
// -------------------------------------
// Remove CMYK swatch duplicates and set every name in `C= M= Y= K=` form.
{
if( !doc ) return;
const __ = $.global.localize;
const CM_PROCESS = +ColorModel.PROCESS;
const CS_CMYK = +ColorSpace.CMYK;
swa = doc.swatches;
a = doc.colors.everyItem().properties;
r = {};
// Gather CMYK swatches => { CMYK_Key => {id, name}[] }
// ---
while( o=a.shift() )
{
Copy link to clipboard
Copied
This may work:
function normalizeCMYK(/*Document*/doc, swa,a,r,o,t,k,i)
// -------------------------------------
// Remove CMYK swatch duplicates and set every name in `C= M= Y= K=` form.
{
if( !doc ) return;
const __ = $.global.localize;
const CM_PROCESS = +ColorModel.PROCESS;
const CS_CMYK = +ColorSpace.CMYK;
swa = doc.swatches;
a = doc.colors.everyItem().properties;
r = {};
// Gather CMYK swatches => { CMYK_Key => {id, name}[] }
// ---
while( o=a.shift() )
{
if( o.model != CM_PROCESS ) continue;
if( o.space != CS_CMYK ) continue;
t = swa.itemByName(o.name);
if( !t.isValid ) continue;
for( i=(k=o.colorValue).length ; i-- ; k=Math.round(k) );
k = __("C=%1 M=%2 Y=%3 K=%4",k[0],k[1],k[2],k[3]);
(r
||(r =[])).push({ id:t.id, name:t.name }); }
// Remove dups and normalize names.
// ---
for( k in r )
{
if( !r.hasOwnProperty(k) ) continue;
t = swa.itemByID((o=(a=r
)[0]).id); for( i=a.length ; --i ; swa.itemByID(a.id).remove(t) );
if( k == o.name ) continue; // No need to rename.
try{ t.name=k }catch(_){} // Prevent read-only errors.
}
};
normalizeCMYK(app.properties.activeDocument);
@+
Marc
Copy link to clipboard
Copied
Hi Marc,
just tested your code.
Two remarks:
1. Swatches that defined as
Cyan, Magenta and Yellow = 0 and Black = 100
are converted to [Black].
That can be ok. Or not. Perhaps one wanted to make a distinction between a [Black] that is always overprinting—depending on set preferences—and a Black that is not overprinting automatically.
2. Swatches defined as
Cyan, Magenta, Yellow and Black = 0
will throw an error in line 36 of your posted code:
"Diese Farbe kann nicht gelöscht werden."
$ID/CantDeleteColorError
Error number 90898
Regards,
Uwe
Copy link to clipboard
Copied
Should this still work in the latest version of InDesign? I've tried saving it as a .js script and running it in ID, but I receive this error:
I previously used this script (or a similar one I can no longer find...) on another computer and unfortunately forgot to back it up. Missing it dearly!
Copy link to clipboard
Copied
Hi ohbfcu,
could be that this code was damaged by moving the thread from the old InDesign Scripting forum to this one a couple of years ago.
Look for the original code at Marc Autret's website:
3/ Graphics and Geometry
How to Normalize CMYK Swatches
Marc Autret, June 11, 2018
https://www.indiscripts.com/post/2018/06/indesign-scripting-forum-roundup-12#hd3sb1
Regards,
Uwe Laubender
( ACP )