2
$\begingroup$

I want to create a palette with buttons and have it apply to the selected word. No matter what combination I try it always applies it to the entire cell.

Imagine that in the sentence:

 "The quick brown fox jumps over the lazy dog."

The word "brown" is selected. I want the font to change only for that word.

The code I have tried is below and in all cases it changes the font for the entire cell.

CreatePalette[
  Row[{
    Spacer[30],
    Column[
     {
      Button["Courier Selection",
       SetOptions[NotebookSelection[InputNotebook[]], 
        FontFamily -> "Courier New"]
       ],
      Button["Courier Next Word",
       SelectionMove[InputNotebook[], Next, Word];
       SetOptions[NotebookSelection[InputNotebook[]], 
        FontFamily -> "Courier New"]
       ],
      Button["Courier First Word",
       SelectionMove[InputNotebook[], Before, CellContents];
       SelectionMove[InputNotebook[], Next, Word];
       SetOptions[NotebookSelection[InputNotebook[]], 
        FontFamily -> "Courier New"]
       ],
      Button["Courier Entire Cell",
       SelectionMove[InputNotebook[], Before, CellContents];
       SelectionMove[InputNotebook[], All, Word];
       SetOptions[NotebookSelection[InputNotebook[]], 
        FontFamily -> "Courier New"]
       ]
      }
     ]
    }],
  WindowTitle -> "Set Font for Selected Word",
  WindowSize -> {170, 120}
  ];
$\endgroup$
2
$\begingroup$

You can do this with Front End Tokens. Here is a button that turns the selected text red:

Button["Color", FrontEndTokenExecute["FontColor", Red]]

In a Notebook:

enter image description here

$\endgroup$
  • $\begingroup$ Example works great. Extended to include FontFamily, ... I was unable to understand the capability from the documentation. Example makes it crystal clear. $\endgroup$ – Jack LaVigne Jan 15 '15 at 1:24
  • $\begingroup$ @Jack The documentation is not complete in this area so trial and error is necessary. $\endgroup$ – Mr.Wizard Jan 15 '15 at 1:26
1
$\begingroup$

For a variety of reasons I still use V9 most of the time and when I ran your code on my Mac (OS 10.10) I found the same problem. Mr Wizard has provided an alternative way for you to achieve what you want but the question remains why your code didn't work. I made some changes to your code that should make it work:

CreatePalette[
  Column[{Button["Courier Selection", 
     SetOptions[NotebookSelection[InputNotebook[]], 
      FontFamily -> "Courier New"]],

Button["Courier Next Word", 
 SelectionMove[InputNotebook[], Next, Word]; 
 SelectionMove[InputNotebook[], All, Word];
 SetOptions[NotebookSelection[InputNotebook[]], 
  FontFamily -> "Courier New"]],

Button["Courier First Word", 
 SelectionMove[InputNotebook[], Before, CellContents];
 SelectionMove[InputNotebook[], All, Word];
 SetOptions[NotebookSelection[InputNotebook[]], 
  FontFamily -> "Courier New"]],

Button["Courier Entire Cell", 
 SelectionMove[InputNotebook[], All, CellContents];
 SetOptions[NotebookSelection[InputNotebook[]], 
  FontFamily -> "Courier New"]]}], 
  WindowTitle -> "Set Font for Selected Word", 
  WindowSize -> {170, 120}];

...But I ran into similar problems to you. So I decided to try some of the NotebookSelection examples in the docs

enter image description here

and what I got was with V8:

enter image description here

...with V9:

enter image description here

...and with V10:

enter image description here

So it seems they somehow managed to break NotebookSelection in V9 -- in the OS X version at least --but have fixed it in V10. You didn't mention which version you are using but I presume V9??

So there were a few minor problems with the code but more importantly a bug in V9. Using the code above this will work for you in V8 or V10.

$\endgroup$
  • $\begingroup$ Makes me feel better. I saw the same example as you in the documentation but was sure that I was doing something wrong (usually the case). At the moment I am using Mathematica 9.0 $\endgroup$ – Jack LaVigne Jan 15 '15 at 1:26
  • $\begingroup$ behaviour is the same on Windows, at least for me: V8 and V10 do the right thing, V9 changes both words to bold... $\endgroup$ – Albert Retey Jan 16 '15 at 9:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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