11
$\begingroup$

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?

$\endgroup$
2
  • $\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$ Mar 23, 2013 at 16:09
  • $\begingroup$ @J.M. yes programmatically. I wasn't precise. $\endgroup$
    – Vladimir
    Mar 23, 2013 at 17:58

1 Answer 1

16
$\begingroup$
"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.

$\endgroup$
4
  • $\begingroup$ God, it's even in the help and I couldn't find it.. Thanks! $\endgroup$
    – Vladimir
    Mar 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 N
    Nov 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 of Developer`CellInformation[nb]. Perhaps because the selection changes when I hit shift enter to run the code. (I used nb = InputNotebook[] to set nb to the current notebook.) $\endgroup$
    – Kvothe
    May 6, 2022 at 9:47
  • $\begingroup$ @Kvothe Developer`CellInformation also works on CellObject, allowing you to get information on cells regardless of whether they're selected. You can use various invocations of Cells[] to acquire the desired list of CellObjects. $\endgroup$
    – John Fultz
    Jul 14, 2022 at 5:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.