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

Grayscale Color conver to CMYK Color

Explorer ,
Sep 26, 2023 Sep 26, 2023

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

 

 

 

TOPICS
Scripting
435
Translate
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
Adobe
Community Expert ,
Sep 26, 2023 Sep 26, 2023

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

Translate
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 ,
Sep 26, 2023 Sep 26, 2023

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

Translate
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
Explorer ,
Sep 27, 2023 Sep 27, 2023
LATEST
thanks CarlosCanto
Translate
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