FontSwitcher

Selwyn Hollis
July 2002
slhollis@mac.com

FontSwitcher.nb is a simple little palette that lets you avoid scrolling down your font menu every time you want to change the font of a word or phrase. It is really intended just for text, but it can also be used in input cells, output cells, and the like—just be aware that there may be undesired consequences.

You can change the font on a cell, or on several individual cells at once, but not on grouped cells.

The “Strip All StyleBoxes” button only works if you select the cell bracket of a single text cell. Its effect is like “Remove All Options,” except that things like margins and cell dingbats are left alone.

So, why not buttons for Bold and Italic? Because there are already keyboard shortcuts for those.

Open this cell group to see the code that generates the Courier button. Nothing too fancy, but it does what I wanted it to do, and it’s reasonably robust. To make a similar button for a different font, just replace all instances of "Courier".

\(ButtonBox[
        StyleBox["Courier", \ FontFamily -> "Courier", 
          FontSize -> 
            10], 
ButtonFunction :> 
          Module[{
data = NotebookRead[InputNotebook[]], 
              it, 
f = 
                Which[
Head[#] === 
                      Cell, 
	
                    Insert[# //. {StyleBox[x_, 
                              FontFamily -> "Courier"] -> x, 
                          StyleBox[x_, a___, 
                              FontFamily -> "Courier", b___] -> 
                            StyleBox[x, a, b]}, 
	
                      FontFamily -> "Courier", 
                      3], 
Head[#] === String, \ 
                    TextData[
                      StyleBox[#, 
                        FontFamily -> "Courier"]], 
Head[#] === StyleBox, \ 
                    TextData[
                      Insert[#, FontFamily -> "Courier", 
                        2]], 
Head[#] === BoxData, \ 
                    BoxData[
                      StyleBox[#[\([1]\)] //. {StyleBox[x_, ___] -> x}, 
                        FontFamily -> "Courier"]]
] &}, 
If[
              FreeQ[data, CellGroupData[__], 
                Infinity], 
it = 
                If[Head[data] === List && Length[data] > 0\  && 
                    Head[data[\([1]\)]] =!= Cell, 
                  StringJoin[data //. {StyleBox[x_, ___] -> x}], 
                  data]; 
If[Head[it] === List, 
                NotebookWrite[InputNotebook[], f /@ it, All], 
                NotebookWrite[InputNotebook[], f[it], 
                  All]]]
], 
        ButtonEvaluator -> Automatic, Active -> True] // 
      DisplayForm\)

Enjoy!