How can I get the style such as Input, Text, Code, etc., of a selected cell? If a group of cells is selected, how can I get styles of all the cells in the group?
Asked
Active
Viewed 369 times
11
-
Programmatically? If it's just one cell, then a manual method: you can right-click on the cell's bracket, mouse over to "Style", and then see what's ticked... – J. M.'s missing motivation Mar 23 '13 at 16:09
-
@J.M. yes programmatically. I wasn't precise. – Vladimir Mar 23 '13 at 17:58
1 Answers
16
"Style" /. Developer`CellInformation[nb]
where nb is the NotebookObject for the notebook you care about. Since you could have multiple cells selected, this will always return a list. It's also possible to have no cells selected, in which case CellInformation will return $Failed and the above code would throw an error.
John Fultz
- 12,581
- 58
- 73
-
-
Thanks @John Fultz . I used your answer in formulating a routine to answer "Is there a shortcut to select all cells above the current cursor position?", picking out just the "Input" style cells to evaluate (which is what the asker was trying to do). – Bill N Nov 25 '16 at 18:39
-
Can I use this within one notebook. I have yet to manage to get anything different from
$Failedout ofDeveloper\CellInformation[nb]. Perhaps because the selection changes when I hit shift enter to run the code. (I usednb = InputNotebook[]` to set nb to the current notebook.) – Kvothe May 06 '22 at 09:47 -
@Kvothe
Developer`CellInformationalso works onCellObject, allowing you to get information on cells regardless of whether they're selected. You can use various invocations ofCells[]to acquire the desired list ofCellObjects. – John Fultz Jul 14 '22 at 05:32