# SetSelectedNotebook does not work [closed]

I have a small program like this:

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


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?

• Interesting. It seems to work for me. Which version are you using, and on which OS and hardware? – Albert Retey Oct 28 '18 at 19:17
• 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. – pkr Oct 29 '18 at 6:07
• Do you use an initialization cell in nb? – hieron Oct 29 '18 at 11:53
• I do not use an initialization cell in either nb of nb1. – pkr Oct 30 '18 at 1:48
• 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... – Albert Retey Oct 30 '18 at 9:50

The second line wont 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:

CurrentValue@"NotebookDirectory"
CurrentValue@"NotebookFullFileName"
(* more under doc "CurrentValue" *)

NotebookDirectory[]
Directory[]

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

{$$UserDocumentsDirectory,$$HomeDirectory}

(* more *)
Names@"*Director*"
Names@"*Path"


I hope this helps you.

• 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? – Kuba Oct 27 '18 at 22:00
• You are right, and it works until you change directory with FileNameSetter, this changes NotebookBrowseDirectory. Its better to define the filenames with FileNameJoin or FrontEndFileName, otherwise it will be always a potential source of failure. (i.e. older version with same name in a backup directory) – hieron Oct 28 '18 at 2:16
• 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. – Kuba Oct 28 '18 at 17:14
• 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. – hieron Oct 29 '18 at 11:27
• 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. – Kuba Oct 29 '18 at 11:35