3
$\begingroup$

Here's some code which, when evaluated, types an expression in the cell below it:

FrontEndExecute@FrontEnd`SelectionMove[SelectedNotebook[],Next,CellContents];[email protected];
FrontEndExecute@FrontEnd`NotebookWrite[SelectedNotebook[],"1"];[email protected];
FrontEndExecute@FrontEnd`NotebookWrite[SelectedNotebook[],"+"]; [email protected];
FrontEndExecute@FrontEnd`NotebookWrite[SelectedNotebook[],"2"];[email protected];
FrontEndExecute@FrontEnd`SelectionMove[SelectedNotebook[],All,CellContents];[email protected];
FrontEndExecute@FrontEnd`NotebookApply[SelectedNotebook[],
  FractionBox["\[SelectionPlaceholder]","\[Placeholder]"]];[email protected];
FrontEndExecute@FrontEnd`NotebookWrite[SelectedNotebook[], "3"];

Now I want to put it in a button, so I'll wrap it in Button["type stuff",...]. But then it takes more than a second to see the full output due to something about the front end and updates and kernels and what not.

If instead I write Button["type stuff",...,Evaluator->None], I find out that Pause is not a function available to the front end.

If I strip out all the Pauses, then it works great -- there is some delay -- I would just like to slow it down.

Question

Is there a function available with no Evaluator that can slow it down? Is there a way to switch Evaluators while a Button function is running? Or should I stick with kernel evaluation only and do something with Dynamic to get the progressive typing?


Extraneous questions:

  • I tried to store the big typing code in a symbol, using HoldForm and ReleaseHold in the Button -- I didn't manage to get it right, the front end couldn't ReleaseHold the thing
  • I tried to have a wait parameter t instead of .2, but again the front end couldn't reference the value of t, and I couldn't figure out how to inject t with a Block or something before creating the Button
  • I tried to generate the code with a Riffle[.../@Characters@"1+2",[email protected]], but how to turn the resulting list into an "input expression" -- no, an "input cell" with some newlines -- no, an Inactive@CompoundExpression@@..., but how to Activate it without evaluating it... I'm at a loss.
$\endgroup$

1 Answer 1

3
$\begingroup$

Is this what you are looking for?

typeStuff = 
  Module[{}, 
   Hold[FrontEndExecute@
     FrontEnd`SelectionMove[SelectedNotebook[], Next, CellContents]; 
    [email protected];
    FrontEndExecute@FrontEnd`NotebookWrite[SelectedNotebook[], "1"]; 
    [email protected];
    FrontEndExecute@FrontEnd`NotebookWrite[SelectedNotebook[], "+"]; 
    [email protected];
    FrontEndExecute@FrontEnd`NotebookWrite[SelectedNotebook[], "2"]; 
    [email protected];
    FrontEndExecute@
     FrontEnd`SelectionMove[SelectedNotebook[], All, CellContents]; 
    [email protected];
    FrontEndExecute@
     FrontEnd`NotebookApply[SelectedNotebook[], 
      FractionBox["\[SelectionPlaceholder]", "\[Placeholder]"]]; 
    [email protected];
    FrontEndExecute@
     FrontEnd`NotebookWrite[SelectedNotebook[], "3"];]];

Button["ClickMe", ReleaseHold@typeStuff, Method -> "Queued"]
$\endgroup$
1
  • 1
    $\begingroup$ To parametrize the delay, change Module[{ }, ... to Module[{t = 1}, ... and replace the 0.2 by t. Or change typeStuff to a function that takes t as an argument. $\endgroup$ Commented Dec 30, 2021 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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