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.

Is it possible to collapse all cell groups in the current notebook? I tried

nb = SelectedNotebook[];
SelectionMove[nb, Before, CellGroup]
FrontEndTokenExecute["OpenCloseGroup"]

but nothing changes.

share|improve this question
    
Thanks for the Accept! –  Mr.Wizard Jun 15 '12 at 1:51

2 Answers 2

up vote 10 down vote accepted

Try:

nb = SelectedNotebook[];
SelectionMove[nb, All, Notebook]
FrontEndTokenExecute["SelectionCloseAllGroups"]

Or:

nb = SelectedNotebook[];
FrontEndExecute[FrontEndToken[nb, "SelectAll"]]
FrontEndTokenExecute["SelectionCloseAllGroups"]

The token may be found in Tokens Related to the Cell Menu in the help browser.

This specific operation may be found under the Scope section of the FrontEndToken documentation.

share|improve this answer
    
Note that "OpenCloseGroup" and "SelectionCloseAllGroups" work differently when you have nested cell groups. –  Brett Champion Jun 14 '12 at 15:38
    
@Brett the OP asked to "collapse all cell groups" -- isn't that what "SelectionCloseAllGroups" does? –  Mr.Wizard Jun 14 '12 at 16:13
    
And yet the sample code used "OpenCloseGroup". But the OP might not have been aware of the differences between them. –  Brett Champion Jun 14 '12 at 16:44
    
@Brett Okay. I thought you were politely trying to tell me my answer was wrong. –  Mr.Wizard Jun 14 '12 at 16:51
    
Thanks! I will try tomorrow –  Karsten W. Jun 14 '12 at 21:55

You could use

nb = SelectedNotebook[];
SelectionMove[nb, All, Notebook];
FrontEndTokenExecute[nb, "OpenCloseGroup"]

although it only works if the notebook starts with a cell group, and not with a lone cell. (The menu item behaves the same way.)

If you know there are cellGroupCount groups, you could also do something like:

nb = SelectedNotebook[];
SelectionMove[nb, Before, Notebook]
Do[SelectionMove[nb, Next, CellGroup];
 FrontEndTokenExecute[nb, "OpenCloseGroup"], {cellGroupCount}]
share|improve this answer
    
Thanks for pointing out the dependance on the notebook start, that clarifies things –  Karsten W. Jun 14 '12 at 21:56

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.