Skip to main content
M.Hasanin
Inspiring
December 27, 2022
Answered

How to get the color value during character color fill test

  • December 27, 2022
  • 2 replies
  • 1247 views

Hi Experts, 

im trying to add a new color if the color is not black after looping the other characters color fill, it suppose document only have black! and another color (only two colors in every doc) , so here is my try :

 

GetCharacterColorValue();

function GetCharacterColorValue(){
    
     var myDoc = app.activeDocument;
     
     //fills for characters
     fills = app.documents[0].stories.everyItem().words.everyItem().characters.everyItem().fillColor;
     
     //loop for the charcters fills
           for (var i = 0; i < fills.length; i++) {
    //Check First Character Fill Color Match
    if (fills[i].name !== 'Black'){
        alert("color: "+"["+fills[i].colorValue+"]");
        var myColor = [fills[i].colorValue.join(',')] //How to get the Color Value!
        
            //Add Color if not exist
            var mycolorvalue = myDoc.swatches.itemByName("C=75 M=5 Y=100 K=0");
            if (!mycolorvalue.isValid) {
            mycolorvalue = myDoc.colors.add({
            //colorValue: myColor,
            colorValue: [75,5,100,0],
            space: ColorSpace.CMYK,
            name: "C=75 M=5 Y=100 K=0",
                });
            }
        break; //stop the loop after first one found
        }
    };
}

 

im stuck here, acutally i dont know if this is possible! :

 

var myColor = [fills[i].colorValue.join(',')] //How to get the Color Value!

 

so i imagined that after getting this value i can use it in the variable :

 

colorValue: myColor,

 

but im stuck there, actually i dont know how to extract the first value found, in the alert is showing :

its one of green colors, so i need to add this color value automatically beacause i have many files like this with different color values (black and anohter color ) and thanks for advance

 

This topic has been closed for replies.
Correct answer rob day

Hi @M.Hasanin , Do you mean you want to add unnamed colors to the Swatches panel (I can see in your .idml the green characters are unnamed colors).

 

I don’t think there is a document method for that, but you can invoke the Add Unnamed Colors menu item:

 

var menuItem =  app.menuActions.itemByID(16403)
if(menuItem.enabled){  
    menuItem.invoke();  
}; 

 

Run on a doc with 3 unnamed colors:

 

2 replies

rob day
Community Expert
Community Expert
December 27, 2022

Hi @M.Hasanin , are you trying to change the color of all the document characters that don’t have a Black fill? if that’s the case try this:

 

SetCharacterColorValue();

function SetCharacterColorValue(){
    
    var myDoc = app.activeDocument;
    var c = app.documents[0].stories.everyItem().characters.everyItem().getElements();
    
    var myColor = makeSwatch(myDoc, "C=75 M=5 Y=100 K=0");
    myColor.properties = {space:ColorSpace.CMYK, colorValue: [75,5,100,0]};
   
    for (var i = 0; i < c.length; i++) {
        if (c[i].fillColor.name !== 'Black'){
            c[i].fillColor = myColor;
        }
    }
}



/**
* Makes a new named Swatch 
* @ param the document to add the color to 
* @ param color name 
* @ return the new swatch 
*/

function makeSwatch(d, n){
    if (d.colors.itemByName(n).isValid) {
        return d.colors.itemByName(n);
    } else {
        return d.colors.add({name:n});
    }
}
M.Hasanin
M.HasaninAuthor
Inspiring
December 27, 2022

Thanks alot @rob day for your script, im trying to add any second color not is black in the swatch panel, thank you very much for your suggestion and scripts

Mohammad Hasanin
rob day
Community Expert
rob dayCommunity ExpertCorrect answer
Community Expert
December 27, 2022

Hi @M.Hasanin , Do you mean you want to add unnamed colors to the Swatches panel (I can see in your .idml the green characters are unnamed colors).

 

I don’t think there is a document method for that, but you can invoke the Add Unnamed Colors menu item:

 

var menuItem =  app.menuActions.itemByID(16403)
if(menuItem.enabled){  
    menuItem.invoke();  
}; 

 

Run on a doc with 3 unnamed colors:

 

Community Expert
December 27, 2022

Hi @M.Hasanin,

I did not understand you issue here. For what I know colorValue is an array and the array has 4 elements for CMYK color and 3 elements for RGB and LAB. So to get the first value doesn't [0] work for you?

-Manan

-Manan
M.Hasanin
M.HasaninAuthor
Inspiring
December 27, 2022

Hi @Manan Joshi ,Thanks for replying,  what i mean that i need to make the script identify the Color Value automaticley and add it to swatch panel, in my first file the second color beside black  have the  value [75,5,100,0] as alert box said, i need to add this value in the swatch panel  but i cant assign this value as color value, also i tried to use the [0] as you said,  but didnt work, here is the modified code and the error message (invalid parameter) :

GetCharacterColorValue();

function GetCharacterColorValue(){
    
     var myDoc = app.activeDocument;
     
     //fills for characters
     fills = app.documents[0].stories.everyItem().words.everyItem().characters.everyItem().fillColor;
     
     //loop for the charcters fills
           for (var i = 0; i < fills.length; i++) {
    //Check First Character Fill Color Match
    if (fills[i].name !== 'Black'){
        var myColor = [fills[0].colorValue.join(',')] //How to get the Color Value!
        
            //Add Color if not exist
            var mycolorvalue = myDoc.swatches.itemByName("C=75 M=5 Y=100 K=0");
            if (!mycolorvalue.isValid) {
            mycolorvalue = myDoc.colors.add({
            colorValue: myColor,
            space: ColorSpace.CMYK,
            name: "C=75 M=5 Y=100 K=0",
                });
            }
        break; //stop the loop after first one found
        }
    };
}

the error appeared :

Mohammad Hasanin
Community Expert
December 27, 2022

What happens if you use 

var myColor = fills[0].colorValue

-Manan

-Manan