• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to get the color value during character color fill test

Enthusiast ,
Dec 26, 2022 Dec 26, 2022

Copy link to clipboard

Copied

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 :

green color.jpg

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

 

Best
Mohammad Hasanin
TOPICS
Scripting

Views

480

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

correct answers 2 Correct answers

Community Expert , Dec 27, 2022 Dec 27, 2022

Ahh sorry you have used [0] as the subscript. Try the following

var myColor = fills[i].colorValue

That is working for me. The rest of my observtion does hold true though

-Manan

Votes

Translate

Translate
Community Expert , Dec 27, 2022 Dec 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:

 

Screen Shot 16.png

Votes

Translate

Translate
Community Expert ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

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

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 ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

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 :
Invalid parameterInvalid parameter

Best
Mohammad Hasanin

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 ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

What happens if you use 

var myColor = fills[0].colorValue

-Manan

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 ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

it works with no error but wrong color added!, script add black color instead of green color , here is swatch color screenshot :
Black Color Added.png

Best
Mohammad Hasanin

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 ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

Send me a sample document to test this with.

-Manan

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 ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

Thanks you @Manan Joshi , here is the project folder of black and green words :
https://drive.google.com/drive/folders/1uMcuWjs9hJAScJTZAhfk3iOadLOiRrcq?usp=share_link 

Best
Mohammad Hasanin

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 ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

Also, another approach would be to make your green swatch, then loop through the user defined document colors and remove them replacing with the new swatch—should be faster than checking the text:

 

 

 

 

SetCharacterColorValue();
function SetCharacterColorValue(){
    //all of the document’s colors and swatches
    var c = app.activeDocument.colors.everyItem().getElements();
    
    var myColor = makeSwatch(app.activeDocument, "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++) {
        //[Black] gets skipped because it cannot be removed
        try {
            c[i].remove(myColor)
        }catch(e) {}  
    } 
}

/**
* 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});
    }
}

 

 

 

Screen Shot 11.pngScreen Shot 12.png

 

Screen Shot 13.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
Community Expert ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

I checked you document and it goes into the if block for i == 3, the name value is blank(probably an unnamed swatch is applied) and the color value is 0,0,0,100 hence the black color is added. So what this means is the code is working as per the instructions given, we would need to work on the problem and solutioning a bit more. Did you try what @rob day gave,

-Manan

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 ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

Ahh sorry you have used [0] as the subscript. Try the following

var myColor = fills[i].colorValue

That is working for me. The rest of my observtion does hold true though

-Manan

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 ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

Thanks a lot @Manan Joshi , now its works

Best
Mohammad Hasanin

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 ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

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

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 ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

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

Best
Mohammad Hasanin

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 ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

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:

 

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 ,
Dec 27, 2022 Dec 27, 2022

Copy link to clipboard

Copied

LATEST

yes, that is my target , thank you alot, thanks alot for your suggestion, very helpful

 

Best
Mohammad Hasanin

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