Copy link to clipboard
Copied
It has a problem converting Grayscale color to CMYK.
There is no problem in the codes I wrote until
I select the objects. But I couldn't convert the
selected color to CMYK color.
Where am I wrong?
var refDoc = app.activeDocument;
function myCmykColor(c, m, y, k) {
var ink = new CMYKColor();
ink.cyan = c;
ink.magenta = m;
ink.yellow = y;
ink.black = k;
return ink;
}
var textColor = new GrayColor();
textColor.gray = 3;
refDoc.defaultFillColor = textColor;
/// So far there are no problems. He makes the choice.
app.executeMenuCommand("Find Fill Color menu item");
/// But here the Grayscale can not convert to CMYK ??
refDoc.selection.fillColor = myCmykColor(0, 0, 0, 3);
Copy link to clipboard
Copied
Hi @Goldberg_1453, the problem may be that in the last line "refDoc.selection" resolves to an Array, which doesn't have a fillColor property. You can loop over the elements of the array and try setting the fillColor of each:
(function () {
var refDoc = app.activeDocument;
var textColor = new GrayColor();
textColor.gray = 3;
refDoc.defaultFillColor = textColor;
/// So far there are no problems. He makes the choice.
app.executeMenuCommand("Find Fill Color menu item");
// loop over each element of selection:
var items = refDoc.selection;
for (var i = 0; i < items.length; i++) {
var item = items[i];
item.fillColor = myCmykColor(0, 0, 0, 3);
}
})();
function myCmykColor(c, m, y, k) {
var ink = new CMYKColor();
ink.cyan = c;
ink.magenta = m;
ink.yellow = y;
ink.black = k;
return ink;
};
I put the code inside an IIFE to keep the global scope clean and moved the function to the end (just for neatness). You don't have to do those, but look at the loop part.
- Mark
Copy link to clipboard
Copied
what m1b said,
another way, after making your selection, you can change the default document color again
/// But here the Grayscale can not convert to CMYK ??//refDoc.selection.fillColor = myCmykColor(0, 0, 0, 3);
refDoc.defaultFillColor = myCmykColor(0, 0, 0, 3);
Copy link to clipboard
Copied
thanks CarlosCanto
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more