I have a small program like this:

nb1 = SelectedNotebook[];
nb = NotebookOpen["data 2015 - 2018.nb"];

After the 2nd command the front-end switches to notebook nb. I want the front-end to return to (or rather to stay with) notebook nb1 however but SetSelectedNotebook does not do the job. I have tried all kinds of other possibilities but thus far everything failed. Can anybody help?

  • $\begingroup$ Interesting. It seems to work for me. Which version are you using, and on which OS and hardware? $\endgroup$ – Albert Retey Oct 28 '18 at 19:17
  • $\begingroup$ Indeed the trouble is with SetSelectedNotebook which is not working. Ik don't get a beep or message. The OS I am using is Mac OS High Sierra version 10.13.3, the hardware is a MacBook Pro (mid 2015) 2,5 GHz Intel Core i7, memory 16 GB 1600 MHz DDR3. $\endgroup$ – pkr Oct 29 '18 at 6:07
  • $\begingroup$ Do you use an initialization cell in nb? $\endgroup$ – hieron Oct 29 '18 at 11:53
  • $\begingroup$ I do not use an initialization cell in either nb of nb1. $\endgroup$ – pkr Oct 30 '18 at 1:48
  • $\begingroup$ Sorry, looks like I can't help as it works for me (Windows 10 laptop, tried several Mathematica versions). Have you tried whether the SetSelectedNotebook works when using CreateDocument instead of NotebookOpen? That would avoid discussions about path and content of the notebook you try to open and probably even give more indication about the nature of the problem... $\endgroup$ – Albert Retey Oct 30 '18 at 9:50

The second line won`t work. Executing code should emit a beep (even if the file exists in current directory), then read "Why the Beep"

For example the second line should be something like:

nb = NotebookOpen@FileNameJoin@{$UserDocumentsDirectory,"Wolfram","Mathematica","data 2015 - 2018.nb"} 

To find out your correct path you may use:

Menu > Insert > FilePath ...

Some path and file related commands, depending on your needs:

(* more under doc "CurrentValue" *)


(* To get some platform independance use Directory Variables in "FileNameJoin" *)


(* more *)

I hope this helps you.

| improve this answer | |
  • 1
    $\begingroup$ Why do you say so? As long as "data 2015 - 2018.nb" is on CurrentValue[NotebookPath] it should work, or? Moreover, if it fails how is OP having problems with keeping selection on current notebook if it is the only one? $\endgroup$ – Kuba Oct 27 '18 at 22:00
  • $\begingroup$ You are right, and it works until you change directory with FileNameSetter, this changes NotebookBrowseDirectory. Its better to define the filenames with FileNameJoin or FrontEnd`FileName, otherwise it will be always a potential source of failure. (i.e. older version with same name in a backup directory) $\endgroup$ – hieron Oct 28 '18 at 2:16
  • $\begingroup$ I am not sure we are on the same page, OP has a problem because NotebookOpen works and SetSelectedNotebook does not. Your answer aims at NotebookOpen though. $\endgroup$ – Kuba Oct 28 '18 at 17:14
  • $\begingroup$ SetSelectedNotebook does not work??? Then nb1 has changed? First I would try it without semicolons, then I would check for hidden changes of varible "nb1" during initialization. Without OP we will never know. $\endgroup$ – hieron Oct 29 '18 at 11:27
  • $\begingroup$ nb1 has not changed but nb was opened and it is the selected one now. When OP tried to set nb1 to be a selected one again, it failed. $\endgroup$ – Kuba Oct 29 '18 at 11:35

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