3
$\begingroup$

I have a While-Loop lurking through a notebook. I want to find out - among other things - if a cell is not visible because it is a member of a closed group. I tried various things, see xxx below. But maybe the CurrentValue-Aproach is not the right thing at all, maybe some FrontEndToken-thing would work.

Can someone help or point to the right thing.

Here is an example. The fist cell is a "Chapter"-Cell, the second is a "Text"-Cell which is the "head" of a group which is closed via cell brackets double-click.

My favorite was CellOpen but it gives True in both cases.

xxx="ShowSelection"; (* Both True *)

xxx="Background"; (* Works well *)

xxx="CellOpen"; (* Both True *)

xxx="Visible";  (* Both True *)

xxx="CellGroupData";  (* Both $Failed *)

xxx="ShowClosedCellArea"; (* Both False *)

xxx="CellGroup"; (* Both $Failed *)

Row[{"Test is: ",xxx }]

SelectionMove[book,Before,Notebook];

SelectionMove[book,Next,Cell]; Last@SelectedCells[]

SelectionMove[book,Next,Cell]; Last@SelectedCells[]

CurrentValue[Last@SelectedCells[],xxx]

SelectionMove[book,Next,Cell]; Last@SelectedCells[]

CurrentValue[Last@SelectedCells[],xxx]
$\endgroup$
  • $\begingroup$ Although I would still be interested in a possible solution I figured out to use SelectionMove[..., CellGroup] and then pick the first cell from the selection. That's what I wanted, to avoid closed cells $\endgroup$ – Darwin1871 May 11 '17 at 22:18
1
$\begingroup$

My favorite was CellOpen but it gives True in both cases.

You misinterpret the meaning of the option CellOpen. This option is NOT related to cell grouping at all, it just hides the contents of a Cell when is set to False for that Cell.

One way to find out whether the cell is a member of a cell group is to attempt to select the cell group and then check whether the attempt is failed. Using the method from this answer,

nb = CreateDocument[CellGroup[{a, b}], CellGrouping -> Manual]

cellObj = First@Cells[nb]
SelectionMove[cellObj, All, CellGroup]
If[Length@SelectedCells[nb] == 0, 
 Print["The cell is not a member of a cell group!"],
 If[MatchQ[NotebookRead[nb], Cell[CellGroupData[_, Closed | False]]], 
  Print["The cell is a member of a closed cell group!"]]]

Additionally one can find out whether the cell is the first cell in the group (which is the only visible cell when the group is closed via group bracket double-click):

"FirstCellInGroup" /. Developer`CellInformation@cellObj 
| improve this answer | |
$\endgroup$
  • $\begingroup$ Note that the second argument to CellGroupData can be a Dynamic expression, so that would have to be checked, too. $\endgroup$ – b3m2a1 Oct 7 '17 at 16:07
  • $\begingroup$ @b3m2a1 Do you have an idea how it could be done (I mean, obtaining the value of Dynamic expression)? $\endgroup$ – Alexey Popkov Oct 7 '17 at 16:09
  • 1
    $\begingroup$ That's a hard question. The simplest option is Setting but in general dynamic evaluation obviously can depend on the cell evaluating it and all sorts of other factors. I've seen Internal`DynamicEvaluate before but don't know how to make it work. $\endgroup$ – b3m2a1 Oct 7 '17 at 16:14
  • $\begingroup$ @b3m2a1 Simple application of Setting to the output of NotebookRead fails, tried with nb = CreateDocument[CellGroup[{Dynamic[a], b}, Dynamic[a]], CellGrouping -> Manual]. The problem seems to be hard indeed. $\endgroup$ – Alexey Popkov Oct 7 '17 at 16:51

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.