Context Menu
The content of context menus is defined in the ContextMenus.tr in the $InstallationDirectory\SystemFiles\FrontEnd\TextResources folder. It is possible to modify this file with A) a text editor (e.g. Notepad++) or B) programmatically.
In order to get a new context menu item with the functionality you described:
First copy the original ContextMenus.tr form the $InstallationDirectory
to your $UserBaseDirectory
If[! DirectoryQ[
FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources"}]],
CreateDirectory[
FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources"}]]]
contextMenusFile =
CopyFile[
FileNameJoin[{$InstallationDirectory, "SystemFiles", "FrontEnd", "TextResources",
"ContextMenus.tr"}],
FileNameJoin[{$UserBaseDirectory, "SystemFiles", "FrontEnd", "TextResources",
"ContextMenus.tr"}]]
then
A) open ContextMenus.tr with an editor
SystemOpen[contextMenusFile] (*to open the file with your default editor*)
SystemOpen[FileNameDrop[contextMenusFile, -1]] (*to just open the directory*)
and find the "Input" -> {
section. Now insert the following part between MenuItem["Evaluate &in Place", Evaluate[All]],
and MenuItem["Evaluate Initiali&zation Cells", "EvaluateInitialization"],
(or wherever you prefer) and save the file.
MenuItem["Evaluate in &New Notebook",
FrontEndExecute[{
FrontEndToken["Copy"],
FrontEndToken["New"],
FrontEndToken["Paste"],
FrontEndToken["HandleShiftReturn"]
}]],
B) programmatically insert the new MenuItem
into the existing list
contextMenus = Import[contextMenusFile, "HeldExpressions"];
insertPosition =
Flatten[Position[contextMenus,
MenuItem["Evaluate Initiali&zation Cells", "EvaluateInitialization"]]]
newContextMenus =
Insert[contextMenus,
MenuItem["Evaluate in &New Notebook",
FrontEndExecute[{FrontEndToken["Copy"], FrontEndToken["New"],
FrontEndToken["Paste"], FrontEndToken["HandleShiftReturn"]}]],
insertPosition];
and export it with
Export[contextMenusFile, newContextMenus, {"Package", "HeldExpressions"}, "Comments" -> None]
After you restarted Mathematica the menu item "Evaluate in New Notebook" appears whenever you right-click on a selected input.
A left-click on "Evaluate in New Notebook" will create a new notebook with your selected input as its first input. It will then automatically be evaluated.
Extra tip: To close the window of that new notebook without saving and without the pop-up of the "Save changes to ..." dialog, just have Shift pressed when you click on the close window x (in Windows).
Keyboard Shortcut
In order to get the same functionality as a keyboard shortcut (Alt + n):
Add
Item[KeyEvent["n", Modifiers->{Command}],
FrontEndExecute[{
FrontEndToken["Copy"],
FrontEndToken["New"],
FrontEndToken["Paste"],
FrontEndToken["HandleShiftReturn"]
}]]
to the list in KeyEventTranslations.tr, which is located in the $InstallationDirectory\SystemFiles\FrontEnd\TextResources\Windows folder.
SelectedCells
,CreateDocument
andNotebookEvaluate
. $\endgroup$