Here's how to add a permanent menu item and keyboard shortcut for Silvia's solutionSilvia's solution. (This can be easily adapted to Jens' solution for Mac OSsolution for Mac OS.)
1. Add Package
Copy Silvia's first code block that starts with Needs["NETLink`"]
to a file and save it as UniCodeCopy.m
in one of the directories included in $Path
.
2. Initialize the Front End
Save the following to the file $UserBaseDirectory/Autoload/FrontEnd/init.m
(create it if needed):
Needs["UniCodeCopy`"]
CopySelectionAsUnicode[] :=
Module[{selection, code},
FrontEndExecute[FrontEndToken["CopySpecial", "InputText"]];
selection = NotebookGet[ClipboardNotebook[]][[1, 1, 1]];
code = StringReplace[selection, {"\\\n" -> "",
"\n" ~~ space : " " ... :> StringJoin["\n", ConstantArray[space, 4]]}];
UniCodeCopy`WriteToClipboardUnicode[code];]
FrontEndExecute[
FrontEnd`AddMenuCommands["Copy",
{MenuItem["Copy as Unicode",
FrontEndExecute[CopySelectionAsUnicode[]],
(* Alt + U *)
MenuKey["u", Modifiers -> {"Command"}],
System`MenuEvaluator -> Automatic,
Method -> "Queued"]}]]
For key bindings, the possible modifiers are "Shift", "Control", "Command" (Alt) and "Option" (Alt)
.
(It took me a few hours to figure this out. I hope this proves useful for other struggling novices.)