5
$\begingroup$

When manipulating a Notebook from the Kernel using such functions as NotebookLocate, SelectionMove, NotebookSelection, NotebookDelete etc. by default user-interaction with the Notebook is allowed and it is possible that the user will occasionally change the current selection which was programmatically set from the kernel leading to unexpected and potentially dangerous results. Consider the following example:

Do[Print[l], {l, {a, b, c}}];
SelectionMove[EvaluationNotebook[], After, EvaluationCell];
SelectionMove[EvaluationNotebook[], Next, Cell];
Pause[3]; (* <- some time-consuming evaluation here *)
If[CurrentValue[NotebookSelection[], CellTags] === {}, 
  NotebookDelete[EvaluationNotebook[]]];

The above code prints three Cells, then selects the first printed Cell, performs some other computations, checks whether this Cell has CellTags and then decides whether it is necessary to delete that Cell. During these operations the user can for example touch the touchpad of his laptop and change the current selection what can result in deleting some important information and loosing the work.

Is it possible to disable user-interaction with the Notebook until evaluation will not finish? It would be perfect to allow the user to abort the evaluation despite he cannot interact with the Notebook.

$\endgroup$
  • 1
    $\begingroup$ as a possible solution you can move the window away from the screen. That's of course far from perfect... $\endgroup$ – Nikolay Gromov Sep 21 '15 at 11:43
1
$\begingroup$

I don't know if this will help - it might be a start...

After three or four other words, I looked at the following:

options = Options[Notebook];
heads = First[#] & /@ options;
strings = ToString[#] & /@ heads;
matches = Select[strings, StringMatchQ[#, ___ ~~ "Select" ~~ ___] &]

Which produced:

{"Selectable", "WindowClickSelect", "ShowSelection", \
"StructuredSelection", "SelectAfterDrawing", "PaneSelectorBoxOptions"}

When I looked at the docs for Selectable:

Selectable is an option for displayed objects, cells, and notebooks that specifies whether their contents can be selected interactively using the front end.

When I tried this:

nb = Notebooks["Orange Browser"][[1]];
SetOptions[nb, Selectable -> False]

I couldn't select anything in that notebook, but I could still press the buttons, so I don't know...

$\endgroup$
  • $\begingroup$ It looks like SetOptions[EvaluationNotebook[], WindowClickSelect -> False, Editable -> False] solves the problem at least in the sense that with these settings current selection cannot be changed and it is not possible to delete/paste/cut anything. It requires more testing though. $\endgroup$ – Alexey Popkov Sep 24 '15 at 17:54
1
$\begingroup$

In version 10 and above you can use Cell Handles to program the notebook without depending on which cell is currently selected.

Do[Print[l], {l, {a, b, c}}];
x = NextCell[EvaluationCell[]];
Pause[3];(* <-some time-consuming evaluation here*)
If[CurrentValue[x, CellTags] === {}, NotebookDelete[x]]

With the above, in the 3 second pause you can select any cell because there is already a cell handle in x to the cell you want to evaluate for delete.

Hope this helps.

$\endgroup$

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.