0

I searched through stackoverflow and the adobe community to find an answer for this. I can't figure out, how to get the color of the selected Object in Indesign.

I know now that I need to check for it to be a swatch or a normal color added. But I just don't know how to then get the color seperated in CMYK out of it for each case.

Any help is greatly appreciated!

Frederik Witte
  • 1,167
  • 2
  • 11
  • 35
  • Do you need 'the' CMYK value of a selected object? Not all swatches are CMYK or can be converted to this (mixed inks, spot colors, gradients). – Jongware Nov 28 '14 at 10:56
  • The Colors we are using are always CMYK. It's for an intern project and we only use CMYK :) One question though, can you get the start and stop color of a gradient in CMYK? – Frederik Witte Nov 28 '14 at 13:51

1 Answers1

3

The fillColor of an object is always reported as a Swatch, even if it's not in the Swatches list. The first test to perform is if it is a Color; you can use instanceof or hasOwnProperty for that. A next sane test is if the color space is CMYK.

A Gradient, as asked in the comment, is slightly more complicated. It contains a list of gradientStops, and each one has a stopColor (a simple color or a mixedink) and a location. If it's a simple color, you can retrieve its value as with a plain fill.

if (app.documents.length && app.selection.length >= 1 && app.selection[0].hasOwnProperty('fillColor'))
{
    swatch = app.selection[0].fillColor;
    if (swatch instanceof Color &&
        swatch.space == ColorSpace.CMYK)
        alert ("color: "+swatch.colorValue.join(','));
    else if (swatch instanceof Gradient)
    {
        list = [];
        for (i=0; i<swatch.gradientStops.length; i++)
            if (swatch.gradientStops[i].stopColor instanceof Color)
                list.push (i+' = '+swatch.gradientStops[i].stopColor.colorValue.join(','));
            else
                list.push (i+' = ?');
        alert ("gradient:\r"+list.join('\r'));
    }
}
Jongware
  • 22,200
  • 8
  • 54
  • 100
  • I don't know if I am doing something wrong, but does this work with text areas? – Frederik Witte Dec 01 '14 at 13:47
  • Sure it does; I just tried. Do your text areas typically have a CMYK fill? Note that the script is rigged to *not* do anything if (a) you don't have any documents open, (b) not have anything selected, (c) the selection does not have a fill color, and/or (d) this fill color is not CMYK. – Jongware Dec 01 '14 at 14:21
  • Ah, I think I used the wrong words here. I don't mean the text-frames itself, I mean the text inside – Frederik Witte Dec 01 '14 at 14:37
  • Well, it also works -- but only when you select a single character! .. I just adjusted the script to additionally allow a selection *longer* than '1' (character/object) but do note that still only the first one is reported. That is, it will not show 'the' CMYK values for a mix of characters or objects. – Jongware Dec 01 '14 at 14:43
  • Ahhh that makes sense. I forgot that every single character can have different colors.Thank you! – Frederik Witte Dec 01 '14 at 15:00
  • Would you mind sharing a good documentation about this topic? In the indesign javascript tutorial there is not much about this... I would be so grateful! – Frederik Witte Dec 03 '14 at 15:13
  • Alas. Other than those Getting Started documents, Adobe does not provide such a thing; and on my end, it's all experience, as I have been writing Javascripts for InDesign for over a decade now. – Jongware Dec 03 '14 at 15:23
  • But how did you learn it then? I mean how did you learn for example how to get a color, if you don't even have a documentation about it? – Frederik Witte Dec 03 '14 at 15:27
  • Sorry, this is getting a bit too chatty for my taste ... But see http://www.jongware.com/idjshelp.html. Adobe also has a bit more than *just* the Get Started Guide. – Jongware Dec 03 '14 at 16:43
  • Hey, I didn't talk about the getting started guide, I'm through the actual javascript guide :P But thank you for your time! – Frederik Witte Dec 03 '14 at 16:45