7
$\begingroup$

I want to work on a notebook programmatically with CellObjects.

lets create a notebook for example:

nb = CreateDocument[{TextCell["a", "Title"], Cell["b", "Subtitle"], 
 Cell["c", "Text"]}];

this gives the CellObjects of the notebook:

cells = Cells[nb]

Now i want to have a function like:

 getCellStyle/@cells

Out:

 {"Title", "Subtitle", "Text"}

I know that i could work with selections as suggested in this question: How can I get the style of selected cells?

But its more practical in my case to work directly with the CellObjects. I think there must be a way to get the style, since the CellObjects look in the FrontEnd like CellObject["style"]

I just want to extract that "style".

$\endgroup$
1
  • $\begingroup$ related: 32957 $\endgroup$
    – Kuba
    Commented Apr 2, 2015 at 8:02

2 Answers 2

6
$\begingroup$

Something more documented:

NotebookRead[ PreviousCell[] ][[2]]
"Section"

It seems there is something exactly what you need, quite not ready probably since in Experimental` context:

Experimental`CellStyleNames[ PreviousCell[] ]
"Output"

and Experimental`CellStyleNames[] gives you styles of all cells in notebook!, quite useful sometimes. Keep in mind that it is undocumented.

p.s. closer look reveals that it is nothing more than CellInformation approach:

ClearAttributes[Experimental`CellStyleNames, ReadProtected];

Block[{$ContextPath},

 AppendTo[$ContextPath, "Experimental`"];

 AppendTo[$ContextPath, "NotebookTools`UtilitiesDump`"];

 ?? Experimental`CellStyleNames
 ]

enter image description here

take a closer look at the very first definition, now you have new way to set cell style:

(Experimental`CellStyleNames[#] = "Title") & @ PreviousCell[]

$\endgroup$
7
  • $\begingroup$ Nice discovery. To clarify: ``Experimental```CellStyleNames@Cells[nb] works, i.e., directly on the CellObjects. So getCellStyles = Experimental`CellStyleNames $\endgroup$ Commented Apr 1, 2015 at 8:38
  • 1
    $\begingroup$ not exactly ... $\endgroup$ Commented Apr 1, 2015 at 8:41
  • $\begingroup$ @RolfMertig Agree, but PreviousCell is a cell object. $\endgroup$
    – Kuba
    Commented Apr 1, 2015 at 8:42
  • $\begingroup$ sure. How do I escape a quote in comments? $\endgroup$ Commented Apr 1, 2015 at 8:45
  • $\begingroup$ @RolfMertig You have to wrap with one more that you want to display inside, and if here is one at the end you have to add whitespace. $\endgroup$
    – Kuba
    Commented Apr 1, 2015 at 8:45
4
$\begingroup$
Last /@ FilterRules[Developer`CellInformation[nb], "Style"]
(* {"Title", "Subtitle", "Text"} *)

Or

SelectionMove[nb, All, Notebook]; "Style" /.  Developer`CellInformation[nb]
(* {"Title", "Subtitle", "Text"} *)

Working with CellObjects:

RawBoxes[ToBoxes[#][[1, -1]]] & /@ Cells[nb]
(* {"Title", "Subtitle", "Text"}  *)
$\endgroup$
5
  • $\begingroup$ thank you for your solution, it works for the most cases, but you did not used the CellObjects. I specify my question, why i need to work with CellObjects... $\endgroup$
    – sacratus
    Commented Apr 1, 2015 at 0:28
  • 1
    $\begingroup$ @sacratus, just added a method that works with CellObjects. Not sure how robust it is though. $\endgroup$
    – kglr
    Commented Apr 1, 2015 at 0:31
  • $\begingroup$ Nice this is what i wanted, but instead of RawBoxes one should use ToExpression so that the function returns a String instead of RawBoxes. Thank You! $\endgroup$
    – sacratus
    Commented Apr 1, 2015 at 0:43
  • $\begingroup$ @sacratus it works with cell objects: "Style" /. Developer`CellInformation@PreviousCell[] $\endgroup$
    – Kuba
    Commented Apr 1, 2015 at 7:07
  • $\begingroup$ The last one is RawBoxes["\"Input\""] for example. So it looks ok but it isn't. $\endgroup$
    – Kuba
    Commented Nov 15, 2015 at 9:27

Your Answer

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

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