Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Assume the following very complex palette:

CreatePalette[
  PasteButton[FontColor -> RGBColor[1, 0, 0]]
];

Its purpose is to insert a FontColor rule. It works like expected in a notebook but I plan to use it inside raw cell expressions. Please try the following two examples. Paste the following code into a notebook

NotebookWrite[
 SelectedNotebook[],
 Cell[Cell["abc", "Text"]]
 ]

You get a text cell saying "abc". Now go with the cursor anywhere between the "abc" and press Ctrl+Shift+E (this is Cell->Show Expression) to see the underlying cell data. Go directly in front of the last closing bracket, make a comma and press the palette-button. You should get

Cell[BoxData[
 FormBox["abc", Text]],FontColor->RGBColor[1,0,0]]

Pressing Ctrl+Shift+E again and the text is red. Works like a charm. Now try the following cell with the same approach:

NotebookWrite[
 SelectedNotebook[],
 Cell[StyleData["StandardForm"]]
 ]

Here I get (Linux 64, Mma 8.0.4)

Cell[StyleData["StandardForm"],
  RowBox[{"FontColor", "->", RowBox[{"RGBColor", "[", 
  RowBox[{"1", ",", "0", ",", "0"}], "]"}]}]
]

which is not what I expected and which does not work.

Question: Can anyone explain me what's going on here? Is the PasteButton aware of the context?

Background: The background is, that I have created a palette which I want to use to adjust style-sheet definitions. That was, when I stumbled over this behaviour.

Fix: One possible way to fix this is to use NotebookWrite directly instead of the PasteButton. Disadvantage of this approach is, that the expression can no longer be inserted into a normal notebook, because there you have to insert cell-expression.

share|improve this question
    
It's very weird. Bug? It doesn't like StyleData cells it tries to paste it as a box expressions –  Rojo May 30 '12 at 1:16
    
Maybe the magic word is "effectively" here: You could use the option BaseStyle->"Paste" and a normal Button and you get the same weird behavior. The doc of "Paste" says "effectively use NotebookApply upon the current selection using the button label as the applied data". –  halirutan May 30 '12 at 1:28
1  
@halirutan You could try Button["paste FontColor", Paste[Defer[FontColor -> RGBColor[1, 0, 0]]]]. –  Heike May 30 '12 at 7:51
    
@Heike This seems to work like I had hoped. Do you have an idea, why and when (even with the Defer) the translation from -> to \[Rule] happens? It seems that wrapping InputForm around it always keeps the -> form. –  halirutan May 30 '12 at 12:21
1  
Bug. Report it to tech support. –  John Fultz Jun 2 '12 at 20:21
add comment

1 Answer

up vote 2 down vote accepted

I think this is a bug in the pasting which is attempting to be smart about what to paste, since in the case of pasting into a cell you actually write the box expressions, while when you are working with the cell expression you want the input-form inserted. In any case, this seems to work in both cases:

 CreatePalette[
 Button["Font Color", 
    NotebookWrite[SelectedNotebook[], ToBoxes[FontColor -> RGBColor[1,0,0]]]
 ]]

Playing around with this I saw no problems with using it in a normal notebook. If it does cause problems with your other code please post code describing the issues.

share|improve this answer
    
Yes, this is similar to what I had as work-around too. –  halirutan Jun 7 '12 at 3:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.