Parenthesize what I have highlighted
I have this palette open all the time:
CreatePalette@Row@
{
Button["(\[SelectionPlaceholder])",
FrontEndExecute[
FrontEndToken[SelectedNotebook[], "SelectionParenthesize"]]],
Button["[\[SelectionPlaceholder]]",
FrontEndExecute[
FrontEndToken[SelectedNotebook[], "SelectionBracket"]]],
Button["{\[SelectionPlaceholder]}",
FrontEndExecute[
FrontEndToken[SelectedNotebook[], "SelectionBrace"]]],
Button["\"\[SelectionPlaceholder]\"",
With[{e = NotebookRead@SelectedNotebook[]},
NotebookWrite[SelectedNotebook[], RowBox@{"\"", e, "\""}];
SelectionMove[SelectedNotebook[], All, Expression, 2]]]
}
By the way, in some text editors (e.g. Sublime Text, Texmate), when you make a selection and press a (, [, { or ", you automatically get the selection wrapped with the corresponding character. I really wish that the Mathematica editor worked like that.
UPDATE: I have simplified the code and modified the last button (wrap selection in quotes) so that the modified string is selected. This is how the other three buttons behave. I borrowed some of @halirutan answer's code in order to make this work (thanks!).
Please start playing with NotebookRead
and NotebookWrite
and look what expressions are returned when you select different things. You could use a simple button like this
Button["Print Selection", Print[NotebookRead[SelectedNotebook[]]]]
Now, you want to change you selection by putting parenthesis around it. A first very simple version looks like this:
Button["Parenthesize",
With[{e = NotebookRead[SelectedNotebook[]]},
NotebookWrite[SelectedNotebook[], RowBox[{"(", e, ")"}]]
]]