How to get Mathematica to wait until I answer a prompt?



I am trying to write some code that does the following...

  1. Closes the current notebook, then
  2. Prompts user to decide if they want to save change before closing, then
  3. Waits for user to submit their choice, then after user submits choice..
  4. Opens another notebook.

For example, the snippet of my code that I am working on looks something like this...

NotebookClose[InputNotebook[], Interactive -> True];

I run the program... But it doesn't do what I want.It doesn't wait for me answer the prompt. That is... it doesn't satisfy #3. I'm not sure how to force Mathematica to wait for an answer before opening the next notebook. Any ideas?

Michael McCain

Posted 2016-10-02T02:24:11.803

Reputation: 4 568

I can't reproduce that. It happens step by step on Windows. – Kuba – 2016-10-05T19:19:31.890

@Kuba- Interesting. I checked again. I'm getting the same result I did before... The new notebook opens before I have a chance to answer NotebookClose Dialog. I am running M10.3 on Mac OS 10.12.1 – Michael McCain – 2016-10-05T19:25:50.907

2On windows "save changes" dialog is both Kernel and FrontEnd blocking one. On mac it appears to be neither. It doesn't look like a dialog at all (no frame elements). I'd ask WRI whether shuch big difference should be the case. – Kuba – 2016-10-07T07:50:20.000



This works on MacOS Sierra running MMA 10.3

SpecialClose[nb2Open_String] := Module[
   nb, nbID , nbIDList

  nb = EvaluationNotebook[];
  nbID = nb[[2]];
   (*id of the evaluation notebook,
  We need to use only the notebooks id since the notebook-
  object's first element is a kernel link that chnages

  (*This task will interactively close the notebook
  We use a scheduled task so that it does not interfere with the evaluation
   NotebookClose[nb, Interactive -> True];
   (*Lets remove the task once its done*)
   (*Lets give it some time, 
   so that it does not interfere with the kernel, if I change the value to
   0 i get a message asking if a want to close a nb with a running    

  (*This task is like a daemon that will check if the nb is 
    closed an then try to open the new one*)
   nbIDList = Notebooks[][[;; , 2]];
    MemberQ [nbIDList, nbID] === False, 
    (*The notebook is closed. 
    Note that it wont work if you cancel the close operation *)

    (*Lets open the other notebook*)

    (*Lets remove the task once its done*)


   , .5 (*lets repeat this every .5 seconds*)



Posted 2016-10-02T02:24:11.803

Reputation: 1 559