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?
-
$\begingroup$ 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... $\endgroup$– J. M.'s eventual burnout ♦Mar 23, 2013 at 16:09
-
$\begingroup$ @J.M. yes programmatically. I wasn't precise. $\endgroup$– VladimirMar 23, 2013 at 17:58
1 Answer
"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.
-
$\begingroup$ God, it's even in the help and I couldn't find it.. Thanks! $\endgroup$– VladimirMar 24, 2013 at 18:34
-
$\begingroup$ 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). $\endgroup$– Bill NNov 25, 2016 at 18:39
-
$\begingroup$ Can I use this within one notebook. I have yet to manage to get anything different from
$Failed
out 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.) $\endgroup$– KvotheMay 6, 2022 at 9:47 -
$\begingroup$ @Kvothe
Developer`CellInformation
also 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 ofCellObject
s. $\endgroup$ Jul 14, 2022 at 5:32