I have created an action menu that allows you to change the style of a cell. It works. However, I would like it to dynamically update the action menu "name". I can't get the name to refresh. Anyone have any ideas? Here is my code.

 Dynamic[CurrentValue[Cell, "CellStyleName"]]
    # :> FrontEndTokenExecute[InputNotebook[], "Style", #] & /@ {
       "Subtitle", "Section", "Text", "Input", "Subsection", 
       "DisplayFormula", "DisplayFormulaNumbered"
 Appearance -> "PopupMenu"
  • $\begingroup$ It's not only about refreshing, it's a problem of getting CurrentValue[Cell, "CellStyleName"] from the Cell you really want it. E.g. when you ust this to change the style of the cell that ActionMenu is in, it will update. $\endgroup$ – Kuba Oct 8 '15 at 9:24
  • $\begingroup$ I see. To further clarify... I am attempting to replicate the functionality built into Mathematica. When one clicks on the format menu and then hovers over Style, you can see that the correct style is checked for the current cell in the notebook. $\endgroup$ – Michael McCain Oct 8 '15 at 9:28
  • $\begingroup$ I think I see what you are saying. $\endgroup$ – Michael McCain Oct 8 '15 at 9:32
  • 1
    $\begingroup$ I wondered how formatting toolbar menu is done and I've not found any sources in Installation directory. Maybe it's not easy/possible to this in pure Mathematica, at least in efficient way. You can always put UpdateInterval and check SelectedCells[] styles but this isn't neat for me. $\endgroup$ – Kuba Oct 8 '15 at 9:38
  • 1
    $\begingroup$ I see. I'll mess around with this some more and see if I can get anything to work. It seems I am looking for a function that replicated InputNotebook[] but at the cell level. Something like InputCell[] but this doesn't exist. Thank you for edit. $\endgroup$ – Michael McCain Oct 8 '15 at 9:41

After a couple hours of tinkering around... I think I got it. Here it is...

currentStyle = 
  DynamicModule[{label = "         "}, 
   Dynamic[With[{sel = Cells[NotebookSelection[InputNotebook[]]]}, 
     If[sel =!= {}, 
      label = CurrentValue[sel[[1]], "CellStyleName"]]];
    label, UpdateInterval -> .5]];

 Evaluate[# :> 
      "Style", #] & /@ {"Subtitle", "Section", "Text", "Input", 
    "Subsection", "DisplayFormula", "DisplayFormulaNumbered"}], 
 Appearance -> "PopupMenu"]

Thank you for your help Kuba. I would not have known where to start.

  • $\begingroup$ Let me then leave the comment about Dynamics here: Quick general tip for DockedCells. I'd not put there crucial Dynamic info, like Dynamic @ CurrentValue[something]. They are not trustworthy (Dynamics) in case of Kernel crash: Dynamic cells after Kernel restart/quit:. $\endgroup$ – Kuba Oct 8 '15 at 12:25

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.