# Palette created by my code doesn't work

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 = NotebookToolsNotebookCellTags[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];
]

-
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 '14 at 3:17

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 = NotebookToolsNotebookCellTags[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:

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

-
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 '14 at 4:18