5
$\begingroup$

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

$\endgroup$
  • $\begingroup$ You may have found a bug, but don't use the bugs tag for suspected bugs. It is reserved for marking confirmed bugs. $\endgroup$ – m_goldberg Aug 19 '14 at 12:45
  • $\begingroup$ If you change the action to Print[DownValues[DuplicateNotebook]], it strangely prints {}. $\endgroup$ – Szabolcs Aug 19 '14 at 13:08
6
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ 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! $\endgroup$ – Ranza Aug 20 '14 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.