3
$\begingroup$

I have a few notebooks, say nb1, nb2, nb3, nb4. I would like to make a Index with a popupmenu so that when a clicked in each item,labeled nb1, nb2,..., then the respective notebook open.

I've been very cheeky trying to create some code, but of course this far from working well. My code is

DynamicModule[{var = "nb1.nb", 
  nb1lst = {"nb1.nb", "nb2.nb", "nb3.nb", "nb4.nb"}},
 Column[{
   PopupMenu[
    Dynamic[var], {1 -> "nb1", 2 -> "nb2", 3 -> "nb3", 4 -> "nb4"}],
   PaneSelector[{
     1 -> 
      Dynamic@NotebookOpen[
        FileNameJoin[{NotebookDirectory[], 
          TextString[nb1lst[[var]]]}]],
     2 -> 
      Dynamic@NotebookOpen[
        FileNameJoin[{NotebookDirectory[], 
          TextString[nb1lst[[var]]]}]],
     3 -> 
      Dynamic@NotebookOpen[
        FileNameJoin[{NotebookDirectory[], 
          TextString[nb1lst[[var]]]}]],
     4 -> 
      Dynamic@NotebookOpen[
        FileNameJoin[{NotebookDirectory[], 
          TextString[nb1lst[[var]]]}]]},
    Dynamic[var]]}]
 ]

Well, can you help me?

Edit I found an easy answer using Kuba suggestion (Action Menu)

Edit This answer is that is that I was looking for

Module[{nblist = {"nb1.nb", "nb2.nb", "nb3.nb"}}, 
 ActionMenu["Choose a File...", 
  MapThread[
   RuleDelayed, {nblist, 
    Unevaluated[
       NotebookOpen[FileNameJoin[{NotebookDirectory[], #}]]] & /@ 
     nblist}] ]]
$\endgroup$
2
  • 1
    $\begingroup$ Does it work if you change var = "nb1.nb" in the first line to var = 1? $\endgroup$
    – kglr
    Jan 31, 2017 at 2:43
  • 1
    $\begingroup$ Additionaly, you need ActionMenu. $\endgroup$
    – Kuba
    Jan 31, 2017 at 6:29

2 Answers 2

3
$\begingroup$

I recommend making an index with hyperlinks rather than with approach you are attempting. It is much easier with hyperlinks. I make a hyperlink-based index for all the notebooks that comprise one of my Mathematica projects in that project's master notebook. Here is an example.

index

Clicking on the grayed notebook file name opens the notebook. Each hyperlink you see in the above image is actually a input/output cell pair with the input cell hidden. Here is the hyperlink with its input cell open.

index_code

Here the code in a form that you copy and use as a template for creating your own indexes.

Hyperlink[
  "TextToolForMSE.nb", 
  {FileNameJoin[{NotebookDirectory[], "TextToolForMSE.nb"}], None}, 
  BaseStyle -> "Subsubsection",
  ActiveStyle -> GrayLevel[.6]]
$\endgroup$
1
  • $\begingroup$ Thanks. Yes, an index with hyperlinks is an option, but I am looking for something more small and versatil, like a popup menu, may be with hyperlinks $\endgroup$
    – wmora2
    Jan 31, 2017 at 14:05
2
$\begingroup$

I've used Kuba suggestion: to use "ActionMenu" and this works

DynamicModule[{nb1lst = {"nb1.nb", "nb2.nb", "nb3.nb"}},
     ActionMenu["LittleIndex",
      {"nb1" :> 
        NotebookOpen[
         FileNameJoin[{NotebookDirectory[], TextString[nb1lst[[1]]]}]],
       "nb2" :> 
        NotebookOpen[
         FileNameJoin[{NotebookDirectory[], TextString[nb1lst[[2]]]}]],
       "nb3" :> 
        NotebookOpen[
         FileNameJoin[{NotebookDirectory[], TextString[nb1lst[[3]]]}]]
       }]
     ]
$\endgroup$

Your Answer

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

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