Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I think I am lost on some basic issue ...

I created two CellTags, tagone and tagtwo

If I create this palette inputting the following code by hand I get a beautifully working palette that makes me jump to these cells.

 CreatePalette[{
    Button["tagone", NotebookFind[InputNotebook[], "tagone", All, CellTags]], 
    Button["tagtwo", NotebookFind[InputNotebook[], "tagtwo", All, CellTags]]}]

If I do put this in a function like the following the palette gets created with right names shown in the buttons, but pressing the buttons nothing happens ... Apparently there is a problem in this code with the use of the counter variable in the For. In this examples it is called celln and in the error monitor I get

Part specification celln$53002 is neither a machine-sized integer nor a list of machine-sized integers.

I though the variables was fixed in the loop ... I am quite puzzled, any guess? Did I just made a rookie mistake? I think this functions was previously working when I originally developed it in December. Maybe my memory is failing.

 Module[{nb, res, list, origlist, celln},
   nb = InputNotebook[];
   origlist = NotebookTools`NotebookCellTags[nb];
   Print[origlist];
   res = {};
   For[celln = 1, celln <= Length[origlist], celln++,
     Print[ToString[origlist[[celln]]]];
     AppendTo[res, 
      Button[origlist[[celln]],
        NotebookFind[nb, origlist[[celln]], All, CellTags, AutoScroll -> True]]];
   ];
   CreatePalette[res];
 ]
share|improve this question
    
I keep trying variations and it seems like the variable nb and the one for the name of the tag are messed up ... –  Roberto Franceschini Feb 11 at 3:17

1 Answer 1

I'm always suspicious of code using For, so I rewrote your code in a more functional way. It seems to wrong fine.

Module[{tags, buttons},
  tags = NotebookTools`NotebookCellTags[InputNotebook[]];
  buttons = 
    Button[#, NotebookFind[InputNotebook[], #, All, CellTags, AutoScroll -> True]] & /@ tags;
  CreatePalette[buttons];]

I had two cells in my test notebook with tags: tag-1 and tag-2. The palette created looks this:

palette

Clicking on one of the buttons scrolls to the indicated cell and selects it.

share|improve this answer
    
You are right, For is an enemy ... I have done essentially the same rewrite using Map as you did too. So pissed by For ... –  Roberto Franceschini Feb 11 at 4:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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