5

Consider the following minimal example package:

BeginPackage["Test`"];

DuplicateNotebook::usage = "Makes a copy of the notebook";

Begin["`Private`"];

DuplicateNotebook[] := 
  NotebookPut @ NotebookGet[EvaluationNotebook[]];
  ex2 = 
    Grid[{{Item[ButtonBar[{
        Style["Duplicate", 10] :> DuplicateNotebook[],
        Style["Quit Kernel", 10] :> FrontEndTokenExecute["EvaluatorQuit"]}], 
      Alignment -> {Right, Top}]}}];
  SetOptions[EvaluationNotebook[], 
    DockedCells -> Cell[BoxData[ToBoxes[ex2]], "SystemDockedCell"]];

End[];

EndPackage[];

So what's happening?

The function DuplicateNotebook[] defined in the package doesn't get called when the button is clicked, but calls of type FrontEndTokenExecute["EvaluatorQuit"] work.

What's more, this code was working perfectely well in Mathematica V9, but now in Mathematica V10, I can't get it working without putting the function body directly into DockedCells, which of course is a very messy solution. Therefore I'm looking for better workarounds.

This seems to be a problem with DockedCells context, because putting the cell in the notebook (not in DockedCells) works. However adding context information to the call didn't work (I've tried Test`DuplicateNotebook[])

Zipped version of the test package http://cl.ly/2C3U2O2n0y21

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Ranza
  • 1,205
  • 9
  • 22

1 Answers1

6

It appears that the action is not evaluated in the same kernel that we are using for computation. We can change the action to Print[$SessionID] and compare it with the $SessionID of the main kernel. They will differ.

Could it be that the action is evaluated in the private kernel instance used by the front end? See here:

I tested this in version 8 and version 9 as well. The service kernel was introduced in version 9. Version 9 behaves the same as version 10. In version 8, the button behaves as you'd expect: it duplicates the notebook.

This all suggests that the button action is evaluated in the service kernel. FrontEndTokenExecute["EvaluatorQuit"] works because it is ultimately evaluated by the front end, not the kernel.

This all happens only if you use the "SystemDockedCell" style, but not otherwise (so there's your fix). For this reason I suspect that this is a deliberate design decision and not a bug.

Looking at the "SystemDockedCell" style in Core.nb, it has the option Evaluator -> "System" set.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
  • I'd never suspect that a style change could make such a difference and I probably changed the style around the time of Mathematica V10 coming out, so it looked like it worked in Mathematica V9, but not in Mathematica V10. Anyway, thanks! – Ranza Aug 20 '14 at 10:31