5
$\begingroup$

I have a notebook used as "to do list" at whose top I want place a button to add automatically, at the end of the notebook, a cell containing the current date and time, and one for the "to do" note.

I have been for a long time experimenting with code similar to this:

btn = Button["add note",
    Block[{},
    (* here *) SelectionMove[EvaluationNotebook[], All, Cell];
    CellPrint @ TextCell[DateString[], "Item", Editable->False];
    CellPrint @ TextCell["", "Text", Deletable -> False];
    ];
  ]

where the instruction above marked as (* here *) has been replaced each time with

SelectionMove[EvaluationNotebook[], Next, 5000];

SelectionMove[EvaluationNotebook[], Next, Cell, 5000];

SelectionMove[EvaluationNotebook[], All, Cell];

.... and many others.

Nevertheless, none of them places the cell precisely at the end of the notebook and moves the cursor inside the last one. Any suggestion, please ?

Of course, under no circumstance, notes written before may be at risk of deletion !

$\endgroup$
7
$\begingroup$

Try this:

Button["add note", SelectionMove[InputNotebook[], After, Notebook];
     NotebookWrite[InputNotebook[], {Cell[DateString[], "Item"], Cell["", "Text"]}]
]
$\endgroup$
3
  • $\begingroup$ Please, can you explain the reason why the two cells have to be wrapped inside Notebook ? $\endgroup$ Feb 19 '16 at 23:28
  • 1
    $\begingroup$ They don't, just removed it :) $\endgroup$
    – M.R.
    Feb 19 '16 at 23:28
  • $\begingroup$ Ok. Thanks a lot. I will ad a backup instruction like CopyFile[NotebookFileName[EvaluationNotebook[]], FileNameJoin[{NotebookDirectory[], "something with a counter"}]]; also. $\endgroup$ Feb 19 '16 at 23:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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