11

I know it's possible to programmatically change options on the selection in a notebook. I'd like to create a button that lets me change the style of a cell. It's a style that doesn't have a keyboard shortcut, "ItemParagraph"

Something like

SetOptions[NotebookSelection[SelectedNotebook[]], CellStyle->"ItemParagraph"]

but CellStyle is for NotebookFind. Is there some way to do this? I just couldn't find the cell option I needed. I checked the Options for Cell but there are hundreds and I couldn't see anything that looked right...

Kuba
  • 136,707
  • 13
  • 279
  • 740
Tom De Vries
  • 3,758
  • 18
  • 36
  • Yes, I have a palette open onscreen so a button on that palette would be much faster than going through the Format --> Style --> Item Paragraph menu. – Tom De Vries Sep 24 '13 at 15:28
  • Maybe useful : http://mathematica.stackexchange.com/a/9956/363 – Chris Degnen Sep 24 '13 at 15:28
  • I believe that post shows how to set the default for NEW cells... I am wondering how to select a cell and change whatever option would be to "ItemParagraph" – Tom De Vries Sep 24 '13 at 15:43

4 Answers4

12

I don't know if any of them can be callled documented but I know three ways to do this:

mentioned above:

FrontEndToken way:

(
 SelectionMove[#, All, Cell]; 
 FrontEndTokenExecute[EvaluationNotebook[], "Style", "Title"]
) & /@ Cells[CellStyle -> "Section"]

or

SelectionSetStyle way:

(
 SelectionMove[#, All, Cell];
 MathLink`CallFrontEnd[FrontEnd`SelectionSetStyle[#, "Title"]]
   ) & /@ Cells[CellStyle -> "Section"]

or (which is implemented with code from above)

CellStyleNames way:

(
 SelectionMove[#, All, Cell];
 Experimental`CellStyleNames[#] = "Title";
) & /@ Cells[CellStyle -> "Section"]
Kuba
  • 136,707
  • 13
  • 279
  • 740
  • Using Scan[] along with your suggestion seems to work faster than SelectionMove[] (at least for me). Not sure why. Try Scan[(Experimental`CellStyleNames[#] = "Title") &, Cells[CellStyle -> "Section"]] – B flat Dec 21 '16 at 12:47
  • 1
    This is significantly faster.... Scan[(SetOptions[#, "Title"]) &, Cells[CellStyle -> "Section"]] But it doesn't delete the old style. It appends the new one. Still works though. You can see the double styles if you press Shift+Command+E in any title cell. Do you think there is a way to fix the code to address this? Seems like using SetOptions[] is quite a bit faster, but need to fix the double style created. – B flat Dec 21 '16 at 13:05
  • @MichaelMcCain Yep, that was the problem with SetOptions. I don't know how to fix it :/ – Kuba Dec 21 '16 at 13:17
7

Maybe this button may help you:

Button["ItemParagraph", FrontEndExecute@FrontEndToken[SelectedNotebook[], "Style", "ItemParagraph"]]

The whole cell may be selected with:

SelectionMove[SelectedNotebook[], All, Cell]
panda-34
  • 1,268
  • 7
  • 8
  • Thank you so much, exactly what I was looking for, does just what I need, and helps me to see ways to use that for other things. – Tom De Vries Sep 25 '13 at 21:59
2

A function showing a different way to modify the style of a cell:

ChangeStyle[cell_CellObject, style_String] := 
  NotebookWrite[cell, ReplacePart[NotebookRead[cell], 2 -> style]];
Jean-Pierre
  • 5,187
  • 8
  • 15
1

This solution below was found in the documentation under Cells->Applications. It appends the new style to the old one, which isn't optimal and creates a mess in most cases. I'll leave it here for reference. Kuba noted that Cells[] allow for more than one style and there may not be a mechanism yet for manipulating these styles easily using SetOptions[] or CurrentValue[]

Scan[(CurrentValue[#, StyleNames] = "Title") &, 
 Cells[CellStyle -> "Section"]]

You can also try this brute force approach below which reads the whole notebook. It is fast. But definitely not as straight forward as Kuba's solutions above.

NotebookPut[ NotebookGet[InputNotebook[]] /. Cell[x_, "Section", y__] -> Cell[x, "Title", y], InputNotebook[]];
B flat
  • 5,523
  • 2
  • 14
  • 36
  • Where in documentation? :), p.s. it adds a style instead of replacing it. – Kuba Dec 21 '16 at 13:55
  • 2
    In Cells structure tutorial multiple styles cell syntax is documented so I'd call it a mess in tool we have to deal with this. Since we can have multiple styles one may need to: replace all with one, change the latest, replace a specific one, etc etc. Tools are not there or are obscure. – Kuba Dec 21 '16 at 14:17
  • 1
    You are reading whole notebook which sees like an overkill for most cases. May be good though if you need to change a lot at once. – Kuba Dec 22 '16 at 08:19