First of all, credits where credits are due, this script is based on Randomize Colors, by John Wundes .

This script works on solid fills and strokes, on gradients and on vectorized text (could not succeed to make it work on regular text, any hint is welcome). It changes predefined CMYK values to other ones. The match on the predefined value has a certain tolerance (default 1%). A color which is not matched is turned to a highly visible red, letting you know where something went wrong. This complete javascript documentation for illustrator unveils of the secrets of the internal form of illustrator, and in many ways it's cleaner than the clumsy Illustrator GUI.

There are 3 parts in this script:

  • 2 arrays containning the source palette and the destination palette. Colors with identical indexes will be swapped.
  • An internal form walker, working on the current selection, and calling itself recursively while walking the internal form. When we reach the final colors, the swap is realized.
  • 2 Functions to find the destination color from object color and the correspondance tables.

And now, here is the script:

//Stick to CMYK Palette
//JS code modifications (c) Aubanel MONNIER
//http://un.regne.net
//Original was:
//Randomize Colors
//JS code (c) by John Wundes 
// johnwun@aol.com
// www.wundes.com
//////////////////////////////////////////////////////////////////

len = app.activeDocument.selection.length;
gradAlert = 0;
strokes = false;
epsilon = 1; //1% of error margin
palette=[
        0, 0, 0, 0,  //blanc
        87.06, 19.61, 0, 0, //Bleu electrique
        23.92, 3.53, 1.18, 0, //Bleu Ciel
        94.90,60.78,34.90,29.019,//Pétrole
        1.96, 59.60, 89.01, 0, //Orange tricycle
        1.96, 29.8, 41.57, 0, //Peche
        28.62, 67.84, 94.11, 16.07,//Fruit Pourri
        58.03, 3.13, 100, 0.39, //Vert Tendre        
        20.39, 0, 52.54, 0, //Vert Tilleul
        72.54, 29.01, 99.21, 13.72, //Vert Sapin
        37.64,25.09,0,0, //Mauve
        10.59,5.49,1.57,0, //Lavande
        83.92,66.27,0,0, //Nuit
        5.88, 17.64, 27.45, 0.78, //Chene
        23.52, 43.92, 78.82, 9.80, //Beige
        43.92, 65.49, 94.90, 57.25, //Chocolat
        2.35,13.72,92.54,0.39, //Jaune d'or
        3.52,3.92,41.96,0, //Jaune pale
        39.99,39.99,97.64,26.66, //Caca d'oie
        1.17, 96.07, 84.70, 0,  //Rouge Camion
        2.35, 14.50, 1.96, 0, //Rose pale
        37.25, 83.92, 92.54, 36.07,//Bordeaux
        9.41, 6.27, 4.71, 0.39, //Gris10  
        18.82,12.94,10.2,1.18, //Gris20
        27.06, 19.22, 16.08, 3.14, //Gris30
        35.29,25.49,22.35,6.67, //Gris40
        42.75,31.37, 28.63,12.94, //Gris50
        49.02,36.47,34.51,21.96, //Gris60
        56.08,42.35,41.57,32.94, //Gris70
        62.35,48.63,48.63,47.84, //Gris80
        68.63,54.9,56.08, 69.41, //Gris90
        0, 0, 0, 100, //Noir
        ];
swapTo=[
        0, 0, 0, 0,  //blanc
        91, 43, 0, 0,//Bleu electrique
        25, 10, 0, 0, //Bleu Ciel
        91,43,0,70,//Pétrole
        0, 60, 94, 0, //Orange tricycle
        0, 30, 50, 0, //Peche
        0, 60, 100, 40,//Fruit Pourri
        40,0,100,30, //Vert Tendre        
        20,0,50,0, //Vert Tilleul  
        40, 0, 100, 60, //Vert Sapin  
        34,30.5,0,6, //Mauve
        11.5,8.5,0,0, //Lavande
        69,65,0,30.5, //Nuit        
        0, 15, 30.5, 6, //Chene
        0, 24, 76, 30.5, //Beige
        0, 51, 100, 76, //Chocolat
        0,15,94,0,//Jaune d'or
        0,5,43,0, //Jaune pale
        0,18.5,100,56, //Caca d'oie
        0, 100, 91, 0,  //Rouge Camion
        0, 15, 6, 0, //Rose pale
        0, 76, 76, 60,//Bordeaux
        0, 0, 0, 10, //Gris10
        0, 0, 0, 20, //Gris20
        0, 0, 0, 30, //Gris30
        0, 0, 0, 40, //Gris40
        0, 0, 0, 50, //Gris50
        0, 0, 0, 60, //Gris60
        0, 0, 0, 70, //Gris70
        0, 0, 0, 80, //Gris80
        0, 0, 0, 90, //Gris90
        0, 0, 0, 100, //Noir
        ];
        
defaultColor=0,100,100,0;
log='';        
if (len == 0) {
	alert("Please select one or more objects. Grouped objects will not be affected.");
} else if(app.activeDocument.documentColorSpace == "DocumentColorSpace.RGB") {
  alert("Document is in RGB Mode ! Switch it to CMYK.");
} else {
	ob = app.activeDocument.selection;
	
       
	for (j=0; j<len; j++) { 

                changeObjectPalette(obj);

        
        
	} 
if (log =='') log='Done !'
alert (log);
}
function changeObjectPalette(obj){
//uncomment to debug
                //alert(obj.typename);
                
//only owrks on vectorized text                
                
if (obj.typename=='CMYKColor'){

//a cmyk color, find the best replacement
  swapColorToNearest(obj);
}

if (obj.typename=='GroupItem'){
//a group, recursive call on all page items
for (l=0; l <obj.pageItems.length; l++ ) {
  changeObjectPalette(obj.pageItemsl);
}
}
if (obj.typename=='TextFrame') {
//Mmmh ,some text
//recurse on the text Range
  //changeObjectPalette(obj.textRange);
changeObjectPalette(obj.textPath);
  //alert(introspect(obj));
}
if (obj.typename=='TextPath') {
  changeObjectPalette(obj.fillColor);
}
if (obj.typename=='TextRange') {
//A text range. Get all the inner character style (style pere char)and the  paragraph styles
for (m=0; m <obj.characterStyles.length; m++ ) {
  changeObjectPalette(obj.characterStylesm);
}
for (m=0; m <obj.paragraphStyles.length; m++ ) {
  changeObjectPalette(obj.paragraphStylesm);
}

}
if (obj.typename=='CharacterStyle') {
//recurse on character attributes
  changeObjectPalette(obj.characterAttributes);
}
if (obj.typename=='ParagraphStyle') {
//recurse on character attributes 
  changeObjectPalette(obj.characterAttributes);
}
if (obj.typename=='CharacterAttributes') {
//get colors

  changeObjectPalette(obj.fillColor);
  changeObjectPalette(obj.strokeColor);
}


if (obj.typename=='GradientColor') {
changeObjectPalette(obj.gradient);       
          }
if (obj.typename=='Gradient') {   
nbStops= obj.gradientStops.length;
            for (k=0; k<nbStops; k++) {
          changeObjectPalette(obj.gradientStopsk);
        } 
}
if (obj.typename=='GradientStop') {
changeObjectPalette(obj.color);       
}
          
if (obj.filled == true) {
changeObjectPalette(obj.fillColor);
}
if (obj.stroked == true) {
        changeObjectPalette(obj.strokeColor);
}

if (obj.typename=='CompoundPathItem') {
for (m=0; m <obj.pathItems.length; m++ ) {
  changeObjectPalette(obj.pathItemsm);
}
}

}

function introspect(obj){
property_list='';
for (prop in obj) 
  property_list += prop +"\n";
return property_list;
}

function swapColorToNearest(c){
  swap = findNearColor(c);
  c.cyan = swap0;
  c.magenta = swap1;
  c.yellow = swap2;
  c.black = swap3;

}

function findNearColor(c){

for (i=0; i < palette.length ; i++) {
 if 
   (nearColor(c.cyan,c.magenta,c.yellow,c.black,palettei0, palettei1, palettei2, palettei3)
   ||
   nearColor(c.cyan,c.magenta,c.yellow,c.black,swapToi0, swapToi1, swapToi2, swapToi3)
 )
 {
 return swapToi;
 }    
}
log+='No match for C '+c.cyan+' M '+c.magenta+' Y '+c.yellow+' K '+c.black+"\n";
   
     return defaultColor;
}

function e(s,r){
return (s >= (r-epsilon)) && (s <= (r+epsilon));
}

function nearColor(cs, ms, ys, ks, cr, mr, yr, kr){
  return e(cs, cr) && e(ms, mr) && e(ys, yr) && e (ks, kr);

}

To install it, drop it in the C:\Program Files\Adobe\Illustrator CS\Paramètres prédéfinis\Scripts directory, it is available from the File->Scripts menu.

UPDATE: modified the funtion findNearColor to make the colors already in the destination palette not beeing swapped.