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 :
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
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
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:
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
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 parameter
Copy link to clipboard
Copied
What happens if you use
var myColor = fills[0].colorValue
-Manan
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 :
Copy link to clipboard
Copied
Send me a sample document to test this with.
-Manan
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
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});
}
}
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
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
Copy link to clipboard
Copied
Thanks a lot @Manan Joshi , now its works
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});
}
}
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
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:
Copy link to clipboard
Copied
yes, that is my target , thank you alot, thanks alot for your suggestion, very helpful