9
$\begingroup$

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];
NotebookOpen["/Users/michaelmccain/Desktop/3.6.nb"];

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?

$\endgroup$
  • $\begingroup$ I can't reproduce that. It happens step by step on Windows. $\endgroup$ – Kuba Oct 5 '16 at 19:19
  • $\begingroup$ @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 $\endgroup$ – Michael McCain Oct 5 '16 at 19:25
  • 2
    $\begingroup$ On 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. $\endgroup$ – Kuba Oct 7 '16 at 7:50
5
+50
$\begingroup$

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
 notebook*)
  RunScheduledTask[
   NotebookClose[nb, Interactive -> True];
   RemoveScheduledTask[$ScheduledTask];
   (*Lets remove the task once its done*)
   ,
   {.2} 
   (*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    
   evaluation*)
   ];


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

    (*Lets open the other notebook*)
    NotebookOpen[nb2Open];

    RemoveScheduledTask[$ScheduledTask]
    (*Lets remove the task once its done*)

    ];

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


  ]
$\endgroup$

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.