# How to create a button that colors a selected piece of text in a cell?

I use Mathematica 9.0.1 and I want change the color of a selection in a text cell using a button. But when I use this code it changes the color of the entire cell.

Row[Button[Graphics[{#, Rectangle[]}, ImageSize -> 20],
SetOptions[NotebookSelection[InputNotebook[]], FontColor -> #], Appearance -> None] & /@
{ColorData["HTML"]["RoyalBlue"], ColorData["HTML"]["OliveDrab"],
RGBColor[0.419608, 0, 0.733333], Black}]


What do I do wrong? It used to work in Mathematica 8 and 9.0.0. I want it to work exactly like the Text Color command in the Writing Assistant palette (or the Format >> Text Color menu command).

-
Where are the words in your example which should be colored? Perhaps you add a picture to your question and a picture of the desired output - this would be helpful, at least for me. –  partial81 Apr 8 '13 at 8:54
To be specific i would like to create a button which will color any kind of text.Not the entire cell but only the part which i choose with the mouse. –  mac Apr 8 '13 at 9:24

CurrentValue seems to be needed, then it works:

    Row[Button[Graphics[{#,Rectangle[]},ImageSize->20],
CurrentValue[NotebookSelection[InputNotebook[]],FontColor]=#,
Appearance->None]&/@{ColorData["HTML"]["RoyalBlue"],
ColorData["HTML"]["OliveDrab"],RGBColor[0.419608,0,0.733333],Black}]

-
It seems to work fine in Mathematica 9.0.1! –  mac Apr 8 '13 at 12:36

I don't have v9 to test, but perhaps this?

Button["red", FrontEndTokenExecute[InputNotebook[], FontColor, Red]]

-
+1, works perfectly in v9. –  István Zachar Apr 8 '13 at 10:50
Works for me too! –  mac Apr 8 '13 at 12:35

The following

Cell[TextData[{"This is some example text in ",
StyleBox["blue", FontColor->RGBColor[0, 0, 1]]}],
"Text",CellChangeTimes->{{3.57440047356804*^9, 3.574400478269321*^9}}]


will result in

Check out the Writing Assistant Palette. There you can find a lot of formatting options.

-
Pardon me but if I paste this literally, it won't produce the desired cell output. One has to wrap it in CellPrint. Also, CellChangeTimes is unncessary. –  István Zachar Apr 8 '13 at 9:20
@IstvánZachar You're right. Thanks for the correction. I just copied the output of the formated cell using CMD + Shift + E. –  chromate Apr 8 '13 at 9:26