Why doesn't running ClearAll[“Global`*”] from

2019-03-30 13:07发布

问题:

I created a custom palette, with an ActionMenu that executes ClearAll["Global`*"]. It performs the desired action, but the frontend doesn't instantly update the colors from black to blue (standard), as it happens when ClearAll["Global`*"] is executed directly from a notebook cell (it only updates after something else has been executed). Is there a way to initiate a refresh of the frontend display status?

EDIT: Apparently, the same happens when I load a package from a palette (blue doesn’t immediately update to black).

回答1:

Not exactly elegant, but it works (the main loop has to be called once after ClearAll):

ActionMenu["Clear", 
   {"Clear global variables" :> (ClearAll["Global`*"]; 
         Block[{nb}, nb = CreateDocument[ExpressionCell[42, "Input"], 
                Visible -> False]; SelectionMove[nb, All, Notebook]; 
            SelectionEvaluate[nb]; NotebookDelete[nb];])}]

Of course, one should use the new Mathematica 8 NotebookEvaluate function, but it seems not to work here (i.e.,

ActionMenu["Clear", 
   {"Clear global variables" :> (ClearAll["Global`*"]; 
         Block[{nb}, nb = CreateDocument[ExpressionCell[42, "Input"], 
                Visible -> False]; NotebookEvaluate[nb]; 
     NotebookDelete[nb];])}]

gives an error message

Could not process unknown packet "1".