11

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?

m_goldberg
  • 107,779
  • 16
  • 103
  • 257
Vladimir
  • 1,503
  • 1
  • 12
  • 28

1 Answers1

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
  • God, it's even in the help and I couldn't find it.. Thanks! – Vladimir Mar 24 '13 at 18:34
  • 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 $Failed out of Developer\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`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. – John Fultz Jul 14 '22 at 05:32