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
    Apr 2 '15 at 8:02
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$ Apr 1 '15 at 8:38
  • 1
    $\begingroup$ not exactly ... $\endgroup$ Apr 1 '15 at 8:41
  • $\begingroup$ @RolfMertig Agree, but PreviousCell is a cell object. $\endgroup$
    – Kuba
    Apr 1 '15 at 8:42
  • $\begingroup$ sure. How do I escape a quote in comments? $\endgroup$ Apr 1 '15 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
    Apr 1 '15 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
    Apr 1 '15 at 0:28
  • 1
    $\begingroup$ @sacratus, just added a method that works with CellObjects. Not sure how robust it is though. $\endgroup$
    – kglr
    Apr 1 '15 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
    Apr 1 '15 at 0:43
  • $\begingroup$ @sacratus it works with cell objects: "Style" /. Developer`CellInformation@PreviousCell[] $\endgroup$
    – Kuba
    Apr 1 '15 at 7:07
  • $\begingroup$ The last one is RawBoxes["\"Input\""] for example. So it looks ok but it isn't. $\endgroup$
    – Kuba
    Nov 15 '15 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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