How to improve on this Stylesheet Manipulation for Message Styling Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Programming scripts to create and modify stylesheets: problems with contextsChanging Color of error messagesAutomatically including custom stylesheet with notebookSharing styles between notebooks (Mathematica v9)Is it possible to create a styled cell with pre-defined content?How edit stylesheet to change automatic numbering of Reference type to numeric, not alphabetic?Where is the file Default.nb?Questions on applying a private Stylesheet to another NotebookHow can I Programmatically Delete Styles from a Notebook's Stylesheet?How to change the default Notebook stylesheetWhy does Mathematica ignore my stylesheet for DisplayFormulaNumbered cells?Merging list valued style options

Hangman Game with C++

Triggering an ultrasonic sensor

Why does the remaining Rebel fleet at the end of Rogue One seem dramatically larger than the one in A New Hope?

How to compare two different files line by line in unix?

How would a mousetrap for use in space work?

Strange behavior of Object.defineProperty() in JavaScript

Would it be possible to dictate a bech32 address as a list of English words?

Is there any word for a place full of confusion?

Parallel Computing Problem

Why limits give us the exact value of the slope of the tangent line?

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

"Lost his faith in humanity in the trenches of Verdun" — last line of an SF story

Is there a kind of relay that only consumes power when switching?

Amount of permutations on an NxNxN Rubik's Cube

Is this another way of expressing the side limit?

Is it possible for SQL statements to execute concurrently within a single session in SQL Server?

What's the meaning of "fortified infraction restraint"?

Monolithic + MicroServices

Question about debouncing - delay of state change

Find 108 by using 3,4,6

If Windows 7 doesn't support WSL, then what does Linux subsystem option mean?

Why do we bend a book to keep it straight?

How does the math work when buying airline miles?

AppleTVs create a chatty alternate WiFi network



How to improve on this Stylesheet Manipulation for Message Styling



Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Programming scripts to create and modify stylesheets: problems with contextsChanging Color of error messagesAutomatically including custom stylesheet with notebookSharing styles between notebooks (Mathematica v9)Is it possible to create a styled cell with pre-defined content?How edit stylesheet to change automatic numbering of Reference type to numeric, not alphabetic?Where is the file Default.nb?Questions on applying a private Stylesheet to another NotebookHow can I Programmatically Delete Styles from a Notebook's Stylesheet?How to change the default Notebook stylesheetWhy does Mathematica ignore my stylesheet for DisplayFormulaNumbered cells?Merging list valued style options










4












$begingroup$


The answer by @Kuba changing color of error messages did not seem to work for me (MMA 11.0.1 Win 10 64-bit).



I couldn't work out why and implemented something else that does work for me, but it seems very ugly and I'm sure I could learn much by seeing how others would improve on it.



I think @Kuba's answer should have worked because I created the Default.nb as proposed in $UserBaseDirectory and my stylesheets ultimately inherit from it (Default.nb -> JM Stylsheet -> JM Clearer for TeamViewer).



However, I also directly modified my own stylesheets by using the "enter a style name" box, entering MessageMenuLabel and MessageText and styling them appropriately. The new styles worked and I saved stylesheets but on restarting MMA these styles were gone again.



Finally I wrote this (my 1st attempt to programmatically manipulate style definitions) making further use of @Kuba's answer to this question as follows



(* To preserve the current stylesheet information it has to be
plucked out of the StyleDefinitions; 1st time this is OK as the
StyleDefinitions = just a notebook name, but after adding items it
gets messy and we need to extract the stylesheet notebook name to reapply it.
*)
sdef = CurrentValue[EvaluationNotebook[], StyleDefinitions];
If[! StringQ[sdef], (*
this is typically just the filename of a stylesheet notebook,
but if it isn't... *)
sdef = ToString[sdef];
sdef = StringCases[sdef, "StyleDefinitions -> " ~~ __ ~~ ".nb]]",
1];
sdef = StringReplace[
sdef[[1]], "StyleDefinitions -> " -> "", "]]" -> ""]
];
SetOptions[EvaluationNotebook[], StyleDefinitions -> Notebook[
Cell[StyleData[StyleDefinitions -> sdef]],
Cell[StyleData["MessageMenuLabel"], Bold,
FontColor -> RGBColor[N[174/255], 0.1, 0],
FontSize ->
CurrentValue[StyleDefinitions, "Output", "FontSize"]],
Cell[StyleData["MessageText"],
FontColor -> RGBColor[0.1, 0.1, 0.1]]

]
(* last line needed per Kuba's Programming scripts to create and modify stylesheets answer*)
/. s_Symbol /; Context[s] === "Global`" :>
Symbol["FrontEnd`" <> SymbolName[s]]]
(* Do something illegal to check the message appearance... *)
1/0


Questions Why might the straightforward approach not have worked, and - for educational purposes - how should it be done programmatically & idiomatically?










share|improve this question











$endgroup$
















    4












    $begingroup$


    The answer by @Kuba changing color of error messages did not seem to work for me (MMA 11.0.1 Win 10 64-bit).



    I couldn't work out why and implemented something else that does work for me, but it seems very ugly and I'm sure I could learn much by seeing how others would improve on it.



    I think @Kuba's answer should have worked because I created the Default.nb as proposed in $UserBaseDirectory and my stylesheets ultimately inherit from it (Default.nb -> JM Stylsheet -> JM Clearer for TeamViewer).



    However, I also directly modified my own stylesheets by using the "enter a style name" box, entering MessageMenuLabel and MessageText and styling them appropriately. The new styles worked and I saved stylesheets but on restarting MMA these styles were gone again.



    Finally I wrote this (my 1st attempt to programmatically manipulate style definitions) making further use of @Kuba's answer to this question as follows



    (* To preserve the current stylesheet information it has to be
    plucked out of the StyleDefinitions; 1st time this is OK as the
    StyleDefinitions = just a notebook name, but after adding items it
    gets messy and we need to extract the stylesheet notebook name to reapply it.
    *)
    sdef = CurrentValue[EvaluationNotebook[], StyleDefinitions];
    If[! StringQ[sdef], (*
    this is typically just the filename of a stylesheet notebook,
    but if it isn't... *)
    sdef = ToString[sdef];
    sdef = StringCases[sdef, "StyleDefinitions -> " ~~ __ ~~ ".nb]]",
    1];
    sdef = StringReplace[
    sdef[[1]], "StyleDefinitions -> " -> "", "]]" -> ""]
    ];
    SetOptions[EvaluationNotebook[], StyleDefinitions -> Notebook[
    Cell[StyleData[StyleDefinitions -> sdef]],
    Cell[StyleData["MessageMenuLabel"], Bold,
    FontColor -> RGBColor[N[174/255], 0.1, 0],
    FontSize ->
    CurrentValue[StyleDefinitions, "Output", "FontSize"]],
    Cell[StyleData["MessageText"],
    FontColor -> RGBColor[0.1, 0.1, 0.1]]

    ]
    (* last line needed per Kuba's Programming scripts to create and modify stylesheets answer*)
    /. s_Symbol /; Context[s] === "Global`" :>
    Symbol["FrontEnd`" <> SymbolName[s]]]
    (* Do something illegal to check the message appearance... *)
    1/0


    Questions Why might the straightforward approach not have worked, and - for educational purposes - how should it be done programmatically & idiomatically?










    share|improve this question











    $endgroup$














      4












      4








      4


      1



      $begingroup$


      The answer by @Kuba changing color of error messages did not seem to work for me (MMA 11.0.1 Win 10 64-bit).



      I couldn't work out why and implemented something else that does work for me, but it seems very ugly and I'm sure I could learn much by seeing how others would improve on it.



      I think @Kuba's answer should have worked because I created the Default.nb as proposed in $UserBaseDirectory and my stylesheets ultimately inherit from it (Default.nb -> JM Stylsheet -> JM Clearer for TeamViewer).



      However, I also directly modified my own stylesheets by using the "enter a style name" box, entering MessageMenuLabel and MessageText and styling them appropriately. The new styles worked and I saved stylesheets but on restarting MMA these styles were gone again.



      Finally I wrote this (my 1st attempt to programmatically manipulate style definitions) making further use of @Kuba's answer to this question as follows



      (* To preserve the current stylesheet information it has to be
      plucked out of the StyleDefinitions; 1st time this is OK as the
      StyleDefinitions = just a notebook name, but after adding items it
      gets messy and we need to extract the stylesheet notebook name to reapply it.
      *)
      sdef = CurrentValue[EvaluationNotebook[], StyleDefinitions];
      If[! StringQ[sdef], (*
      this is typically just the filename of a stylesheet notebook,
      but if it isn't... *)
      sdef = ToString[sdef];
      sdef = StringCases[sdef, "StyleDefinitions -> " ~~ __ ~~ ".nb]]",
      1];
      sdef = StringReplace[
      sdef[[1]], "StyleDefinitions -> " -> "", "]]" -> ""]
      ];
      SetOptions[EvaluationNotebook[], StyleDefinitions -> Notebook[
      Cell[StyleData[StyleDefinitions -> sdef]],
      Cell[StyleData["MessageMenuLabel"], Bold,
      FontColor -> RGBColor[N[174/255], 0.1, 0],
      FontSize ->
      CurrentValue[StyleDefinitions, "Output", "FontSize"]],
      Cell[StyleData["MessageText"],
      FontColor -> RGBColor[0.1, 0.1, 0.1]]

      ]
      (* last line needed per Kuba's Programming scripts to create and modify stylesheets answer*)
      /. s_Symbol /; Context[s] === "Global`" :>
      Symbol["FrontEnd`" <> SymbolName[s]]]
      (* Do something illegal to check the message appearance... *)
      1/0


      Questions Why might the straightforward approach not have worked, and - for educational purposes - how should it be done programmatically & idiomatically?










      share|improve this question











      $endgroup$




      The answer by @Kuba changing color of error messages did not seem to work for me (MMA 11.0.1 Win 10 64-bit).



      I couldn't work out why and implemented something else that does work for me, but it seems very ugly and I'm sure I could learn much by seeing how others would improve on it.



      I think @Kuba's answer should have worked because I created the Default.nb as proposed in $UserBaseDirectory and my stylesheets ultimately inherit from it (Default.nb -> JM Stylsheet -> JM Clearer for TeamViewer).



      However, I also directly modified my own stylesheets by using the "enter a style name" box, entering MessageMenuLabel and MessageText and styling them appropriately. The new styles worked and I saved stylesheets but on restarting MMA these styles were gone again.



      Finally I wrote this (my 1st attempt to programmatically manipulate style definitions) making further use of @Kuba's answer to this question as follows



      (* To preserve the current stylesheet information it has to be
      plucked out of the StyleDefinitions; 1st time this is OK as the
      StyleDefinitions = just a notebook name, but after adding items it
      gets messy and we need to extract the stylesheet notebook name to reapply it.
      *)
      sdef = CurrentValue[EvaluationNotebook[], StyleDefinitions];
      If[! StringQ[sdef], (*
      this is typically just the filename of a stylesheet notebook,
      but if it isn't... *)
      sdef = ToString[sdef];
      sdef = StringCases[sdef, "StyleDefinitions -> " ~~ __ ~~ ".nb]]",
      1];
      sdef = StringReplace[
      sdef[[1]], "StyleDefinitions -> " -> "", "]]" -> ""]
      ];
      SetOptions[EvaluationNotebook[], StyleDefinitions -> Notebook[
      Cell[StyleData[StyleDefinitions -> sdef]],
      Cell[StyleData["MessageMenuLabel"], Bold,
      FontColor -> RGBColor[N[174/255], 0.1, 0],
      FontSize ->
      CurrentValue[StyleDefinitions, "Output", "FontSize"]],
      Cell[StyleData["MessageText"],
      FontColor -> RGBColor[0.1, 0.1, 0.1]]

      ]
      (* last line needed per Kuba's Programming scripts to create and modify stylesheets answer*)
      /. s_Symbol /; Context[s] === "Global`" :>
      Symbol["FrontEnd`" <> SymbolName[s]]]
      (* Do something illegal to check the message appearance... *)
      1/0


      Questions Why might the straightforward approach not have worked, and - for educational purposes - how should it be done programmatically & idiomatically?







      stylesheet coding-style






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 44 mins ago









      mikado

      6,9071929




      6,9071929










      asked 3 hours ago









      Julian MooreJulian Moore

      9471515




      9471515




















          1 Answer
          1






          active

          oldest

          votes


















          3












          $begingroup$

          I worked on this a while back and found that it was just too messy to really work with the Notebook expression.



          Here's a better approach: 1) pull the NotebookObject's stylesheet 2) determine if the cell style you want to edit is in there 3) edit that style or make a new cell to edit



          Here's a quick imp for that:



          nbStyleSheet[nb_] :=

          With[cv = CurrentValue[nb, StyleDefinitions],
          If[! MatchQ[cv, _Notebook],
          SetOptions[nb,
          StyleDefinitions ->
          Notebook[Cell[StyleData[StyleDefinitions -> cv]],
          StyleDefinitions -> "PrivateStylesheetFormatting.nb"
          ]]
          ];
          Lookup[NotebookInformation[nb], "StyleDefinitions"][[1]]
          ];

          findStyleData[nb_,
          stylePattern : _?StringPattern`StringPatternQ : "*"] :=

          Module[cells = Cells[nb],
          Association@
          MapThread[
          Replace[
          #,

          Cell[
          StyleData[
          name_String?(StringMatchQ[stylePattern]), ___], ___] :>
          (name -> #2),
          _ -> Nothing

          ] &,

          NotebookRead[cells],
          cells

          ]
          ];

          editStyleCell // Clear
          editStyleCell[nb_, styleCell_, styleEdits_] :=
          MathLink`CallFrontEnd@
          FrontEnd`SetOptions[styleCell, styleEdits],
          (* this is a hack to make these edits apply immediately *)

          FrontEnd`SelectionMove[styleCell, All, Cell],
          FrontEndToken[nb, "ToggleShowExpression"],
          FrontEndToken[nb, "ToggleShowExpression"]


          makeMissingStyles[nb_, names_] :=
          MathLink`CallFrontEnd@
          FrontEnd`SelectionMove[nb, After, Notebook],
          FrontEnd`NotebookWrite[nb, Map[Cell[StyleData[#]] &, names]]


          styleSheetEdit[notebook_, styleEdits_?AssociationQ] :=
          Module[

          names = Keys[styleEdits],
          nb = nbStyleSheet[notebook],
          cells,
          missing
          ,
          cells = findStyleData[nb, Alternatives @@ names];
          missing = Complement[names, Keys@cells];
          If[Length@missing > 0,
          makeMissingStyles[nb, names];
          cells = Join[cells, findStyleData[nb, Alternatives @@ missing]]
          ];
          MapThread[
          editStyleCell[nb, #, #2] &,

          cells,
          styleEdits

          ];
          ];
          styleSheetEdit[styleEdits_?AssociationQ] :=
          styleSheetEdit[EvaluationNotebook[], styleEdits];


          Let me know if you have questions. Meantime you can edit notebook stylesheets like this:



          styleSheetEdit[<|"Input" -> FontColor -> Pink|>]


          enter image description here



          CurrentValue[EvaluationNotebook[], StyleDefinitions]

          Notebook[Cell[StyleData[StyleDefinitions -> "Default.nb"]],
          Cell[StyleData["Input"], FontColor -> RGBColor[1, 0.5, 0.5]],
          Visible -> False,
          FrontEndVersion -> "12.0 for Mac OS X x86 (64-bit) (April 8, 2019)",
          StyleDefinitions -> "PrivateStylesheetFormatting.nb"]


          You can revert changes by setting them to Inherited:



          styleSheetEdit[<|"Input" -> FontColor -> Inherited|>]





          share|improve this answer









          $endgroup$












          • $begingroup$
            Thanks; It'll take me a while to digest this but will get back when I have. Given your rep I guess this must be pretty optimal but it still seems like a lot of effort for something that I would have expected to be easier!
            $endgroup$
            – Julian Moore
            1 hour ago










          • $begingroup$
            @JulianMoore I just added a bunch of tweaks to make it work cleaner. It'd be doable in fewer lines if you wanted it to be less convenient I think.
            $endgroup$
            – b3m2a1
            1 hour ago











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "387"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f195584%2fhow-to-improve-on-this-stylesheet-manipulation-for-message-styling%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3












          $begingroup$

          I worked on this a while back and found that it was just too messy to really work with the Notebook expression.



          Here's a better approach: 1) pull the NotebookObject's stylesheet 2) determine if the cell style you want to edit is in there 3) edit that style or make a new cell to edit



          Here's a quick imp for that:



          nbStyleSheet[nb_] :=

          With[cv = CurrentValue[nb, StyleDefinitions],
          If[! MatchQ[cv, _Notebook],
          SetOptions[nb,
          StyleDefinitions ->
          Notebook[Cell[StyleData[StyleDefinitions -> cv]],
          StyleDefinitions -> "PrivateStylesheetFormatting.nb"
          ]]
          ];
          Lookup[NotebookInformation[nb], "StyleDefinitions"][[1]]
          ];

          findStyleData[nb_,
          stylePattern : _?StringPattern`StringPatternQ : "*"] :=

          Module[cells = Cells[nb],
          Association@
          MapThread[
          Replace[
          #,

          Cell[
          StyleData[
          name_String?(StringMatchQ[stylePattern]), ___], ___] :>
          (name -> #2),
          _ -> Nothing

          ] &,

          NotebookRead[cells],
          cells

          ]
          ];

          editStyleCell // Clear
          editStyleCell[nb_, styleCell_, styleEdits_] :=
          MathLink`CallFrontEnd@
          FrontEnd`SetOptions[styleCell, styleEdits],
          (* this is a hack to make these edits apply immediately *)

          FrontEnd`SelectionMove[styleCell, All, Cell],
          FrontEndToken[nb, "ToggleShowExpression"],
          FrontEndToken[nb, "ToggleShowExpression"]


          makeMissingStyles[nb_, names_] :=
          MathLink`CallFrontEnd@
          FrontEnd`SelectionMove[nb, After, Notebook],
          FrontEnd`NotebookWrite[nb, Map[Cell[StyleData[#]] &, names]]


          styleSheetEdit[notebook_, styleEdits_?AssociationQ] :=
          Module[

          names = Keys[styleEdits],
          nb = nbStyleSheet[notebook],
          cells,
          missing
          ,
          cells = findStyleData[nb, Alternatives @@ names];
          missing = Complement[names, Keys@cells];
          If[Length@missing > 0,
          makeMissingStyles[nb, names];
          cells = Join[cells, findStyleData[nb, Alternatives @@ missing]]
          ];
          MapThread[
          editStyleCell[nb, #, #2] &,

          cells,
          styleEdits

          ];
          ];
          styleSheetEdit[styleEdits_?AssociationQ] :=
          styleSheetEdit[EvaluationNotebook[], styleEdits];


          Let me know if you have questions. Meantime you can edit notebook stylesheets like this:



          styleSheetEdit[<|"Input" -> FontColor -> Pink|>]


          enter image description here



          CurrentValue[EvaluationNotebook[], StyleDefinitions]

          Notebook[Cell[StyleData[StyleDefinitions -> "Default.nb"]],
          Cell[StyleData["Input"], FontColor -> RGBColor[1, 0.5, 0.5]],
          Visible -> False,
          FrontEndVersion -> "12.0 for Mac OS X x86 (64-bit) (April 8, 2019)",
          StyleDefinitions -> "PrivateStylesheetFormatting.nb"]


          You can revert changes by setting them to Inherited:



          styleSheetEdit[<|"Input" -> FontColor -> Inherited|>]





          share|improve this answer









          $endgroup$












          • $begingroup$
            Thanks; It'll take me a while to digest this but will get back when I have. Given your rep I guess this must be pretty optimal but it still seems like a lot of effort for something that I would have expected to be easier!
            $endgroup$
            – Julian Moore
            1 hour ago










          • $begingroup$
            @JulianMoore I just added a bunch of tweaks to make it work cleaner. It'd be doable in fewer lines if you wanted it to be less convenient I think.
            $endgroup$
            – b3m2a1
            1 hour ago















          3












          $begingroup$

          I worked on this a while back and found that it was just too messy to really work with the Notebook expression.



          Here's a better approach: 1) pull the NotebookObject's stylesheet 2) determine if the cell style you want to edit is in there 3) edit that style or make a new cell to edit



          Here's a quick imp for that:



          nbStyleSheet[nb_] :=

          With[cv = CurrentValue[nb, StyleDefinitions],
          If[! MatchQ[cv, _Notebook],
          SetOptions[nb,
          StyleDefinitions ->
          Notebook[Cell[StyleData[StyleDefinitions -> cv]],
          StyleDefinitions -> "PrivateStylesheetFormatting.nb"
          ]]
          ];
          Lookup[NotebookInformation[nb], "StyleDefinitions"][[1]]
          ];

          findStyleData[nb_,
          stylePattern : _?StringPattern`StringPatternQ : "*"] :=

          Module[cells = Cells[nb],
          Association@
          MapThread[
          Replace[
          #,

          Cell[
          StyleData[
          name_String?(StringMatchQ[stylePattern]), ___], ___] :>
          (name -> #2),
          _ -> Nothing

          ] &,

          NotebookRead[cells],
          cells

          ]
          ];

          editStyleCell // Clear
          editStyleCell[nb_, styleCell_, styleEdits_] :=
          MathLink`CallFrontEnd@
          FrontEnd`SetOptions[styleCell, styleEdits],
          (* this is a hack to make these edits apply immediately *)

          FrontEnd`SelectionMove[styleCell, All, Cell],
          FrontEndToken[nb, "ToggleShowExpression"],
          FrontEndToken[nb, "ToggleShowExpression"]


          makeMissingStyles[nb_, names_] :=
          MathLink`CallFrontEnd@
          FrontEnd`SelectionMove[nb, After, Notebook],
          FrontEnd`NotebookWrite[nb, Map[Cell[StyleData[#]] &, names]]


          styleSheetEdit[notebook_, styleEdits_?AssociationQ] :=
          Module[

          names = Keys[styleEdits],
          nb = nbStyleSheet[notebook],
          cells,
          missing
          ,
          cells = findStyleData[nb, Alternatives @@ names];
          missing = Complement[names, Keys@cells];
          If[Length@missing > 0,
          makeMissingStyles[nb, names];
          cells = Join[cells, findStyleData[nb, Alternatives @@ missing]]
          ];
          MapThread[
          editStyleCell[nb, #, #2] &,

          cells,
          styleEdits

          ];
          ];
          styleSheetEdit[styleEdits_?AssociationQ] :=
          styleSheetEdit[EvaluationNotebook[], styleEdits];


          Let me know if you have questions. Meantime you can edit notebook stylesheets like this:



          styleSheetEdit[<|"Input" -> FontColor -> Pink|>]


          enter image description here



          CurrentValue[EvaluationNotebook[], StyleDefinitions]

          Notebook[Cell[StyleData[StyleDefinitions -> "Default.nb"]],
          Cell[StyleData["Input"], FontColor -> RGBColor[1, 0.5, 0.5]],
          Visible -> False,
          FrontEndVersion -> "12.0 for Mac OS X x86 (64-bit) (April 8, 2019)",
          StyleDefinitions -> "PrivateStylesheetFormatting.nb"]


          You can revert changes by setting them to Inherited:



          styleSheetEdit[<|"Input" -> FontColor -> Inherited|>]





          share|improve this answer









          $endgroup$












          • $begingroup$
            Thanks; It'll take me a while to digest this but will get back when I have. Given your rep I guess this must be pretty optimal but it still seems like a lot of effort for something that I would have expected to be easier!
            $endgroup$
            – Julian Moore
            1 hour ago










          • $begingroup$
            @JulianMoore I just added a bunch of tweaks to make it work cleaner. It'd be doable in fewer lines if you wanted it to be less convenient I think.
            $endgroup$
            – b3m2a1
            1 hour ago













          3












          3








          3





          $begingroup$

          I worked on this a while back and found that it was just too messy to really work with the Notebook expression.



          Here's a better approach: 1) pull the NotebookObject's stylesheet 2) determine if the cell style you want to edit is in there 3) edit that style or make a new cell to edit



          Here's a quick imp for that:



          nbStyleSheet[nb_] :=

          With[cv = CurrentValue[nb, StyleDefinitions],
          If[! MatchQ[cv, _Notebook],
          SetOptions[nb,
          StyleDefinitions ->
          Notebook[Cell[StyleData[StyleDefinitions -> cv]],
          StyleDefinitions -> "PrivateStylesheetFormatting.nb"
          ]]
          ];
          Lookup[NotebookInformation[nb], "StyleDefinitions"][[1]]
          ];

          findStyleData[nb_,
          stylePattern : _?StringPattern`StringPatternQ : "*"] :=

          Module[cells = Cells[nb],
          Association@
          MapThread[
          Replace[
          #,

          Cell[
          StyleData[
          name_String?(StringMatchQ[stylePattern]), ___], ___] :>
          (name -> #2),
          _ -> Nothing

          ] &,

          NotebookRead[cells],
          cells

          ]
          ];

          editStyleCell // Clear
          editStyleCell[nb_, styleCell_, styleEdits_] :=
          MathLink`CallFrontEnd@
          FrontEnd`SetOptions[styleCell, styleEdits],
          (* this is a hack to make these edits apply immediately *)

          FrontEnd`SelectionMove[styleCell, All, Cell],
          FrontEndToken[nb, "ToggleShowExpression"],
          FrontEndToken[nb, "ToggleShowExpression"]


          makeMissingStyles[nb_, names_] :=
          MathLink`CallFrontEnd@
          FrontEnd`SelectionMove[nb, After, Notebook],
          FrontEnd`NotebookWrite[nb, Map[Cell[StyleData[#]] &, names]]


          styleSheetEdit[notebook_, styleEdits_?AssociationQ] :=
          Module[

          names = Keys[styleEdits],
          nb = nbStyleSheet[notebook],
          cells,
          missing
          ,
          cells = findStyleData[nb, Alternatives @@ names];
          missing = Complement[names, Keys@cells];
          If[Length@missing > 0,
          makeMissingStyles[nb, names];
          cells = Join[cells, findStyleData[nb, Alternatives @@ missing]]
          ];
          MapThread[
          editStyleCell[nb, #, #2] &,

          cells,
          styleEdits

          ];
          ];
          styleSheetEdit[styleEdits_?AssociationQ] :=
          styleSheetEdit[EvaluationNotebook[], styleEdits];


          Let me know if you have questions. Meantime you can edit notebook stylesheets like this:



          styleSheetEdit[<|"Input" -> FontColor -> Pink|>]


          enter image description here



          CurrentValue[EvaluationNotebook[], StyleDefinitions]

          Notebook[Cell[StyleData[StyleDefinitions -> "Default.nb"]],
          Cell[StyleData["Input"], FontColor -> RGBColor[1, 0.5, 0.5]],
          Visible -> False,
          FrontEndVersion -> "12.0 for Mac OS X x86 (64-bit) (April 8, 2019)",
          StyleDefinitions -> "PrivateStylesheetFormatting.nb"]


          You can revert changes by setting them to Inherited:



          styleSheetEdit[<|"Input" -> FontColor -> Inherited|>]





          share|improve this answer









          $endgroup$



          I worked on this a while back and found that it was just too messy to really work with the Notebook expression.



          Here's a better approach: 1) pull the NotebookObject's stylesheet 2) determine if the cell style you want to edit is in there 3) edit that style or make a new cell to edit



          Here's a quick imp for that:



          nbStyleSheet[nb_] :=

          With[cv = CurrentValue[nb, StyleDefinitions],
          If[! MatchQ[cv, _Notebook],
          SetOptions[nb,
          StyleDefinitions ->
          Notebook[Cell[StyleData[StyleDefinitions -> cv]],
          StyleDefinitions -> "PrivateStylesheetFormatting.nb"
          ]]
          ];
          Lookup[NotebookInformation[nb], "StyleDefinitions"][[1]]
          ];

          findStyleData[nb_,
          stylePattern : _?StringPattern`StringPatternQ : "*"] :=

          Module[cells = Cells[nb],
          Association@
          MapThread[
          Replace[
          #,

          Cell[
          StyleData[
          name_String?(StringMatchQ[stylePattern]), ___], ___] :>
          (name -> #2),
          _ -> Nothing

          ] &,

          NotebookRead[cells],
          cells

          ]
          ];

          editStyleCell // Clear
          editStyleCell[nb_, styleCell_, styleEdits_] :=
          MathLink`CallFrontEnd@
          FrontEnd`SetOptions[styleCell, styleEdits],
          (* this is a hack to make these edits apply immediately *)

          FrontEnd`SelectionMove[styleCell, All, Cell],
          FrontEndToken[nb, "ToggleShowExpression"],
          FrontEndToken[nb, "ToggleShowExpression"]


          makeMissingStyles[nb_, names_] :=
          MathLink`CallFrontEnd@
          FrontEnd`SelectionMove[nb, After, Notebook],
          FrontEnd`NotebookWrite[nb, Map[Cell[StyleData[#]] &, names]]


          styleSheetEdit[notebook_, styleEdits_?AssociationQ] :=
          Module[

          names = Keys[styleEdits],
          nb = nbStyleSheet[notebook],
          cells,
          missing
          ,
          cells = findStyleData[nb, Alternatives @@ names];
          missing = Complement[names, Keys@cells];
          If[Length@missing > 0,
          makeMissingStyles[nb, names];
          cells = Join[cells, findStyleData[nb, Alternatives @@ missing]]
          ];
          MapThread[
          editStyleCell[nb, #, #2] &,

          cells,
          styleEdits

          ];
          ];
          styleSheetEdit[styleEdits_?AssociationQ] :=
          styleSheetEdit[EvaluationNotebook[], styleEdits];


          Let me know if you have questions. Meantime you can edit notebook stylesheets like this:



          styleSheetEdit[<|"Input" -> FontColor -> Pink|>]


          enter image description here



          CurrentValue[EvaluationNotebook[], StyleDefinitions]

          Notebook[Cell[StyleData[StyleDefinitions -> "Default.nb"]],
          Cell[StyleData["Input"], FontColor -> RGBColor[1, 0.5, 0.5]],
          Visible -> False,
          FrontEndVersion -> "12.0 for Mac OS X x86 (64-bit) (April 8, 2019)",
          StyleDefinitions -> "PrivateStylesheetFormatting.nb"]


          You can revert changes by setting them to Inherited:



          styleSheetEdit[<|"Input" -> FontColor -> Inherited|>]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          b3m2a1b3m2a1

          29k360167




          29k360167











          • $begingroup$
            Thanks; It'll take me a while to digest this but will get back when I have. Given your rep I guess this must be pretty optimal but it still seems like a lot of effort for something that I would have expected to be easier!
            $endgroup$
            – Julian Moore
            1 hour ago










          • $begingroup$
            @JulianMoore I just added a bunch of tweaks to make it work cleaner. It'd be doable in fewer lines if you wanted it to be less convenient I think.
            $endgroup$
            – b3m2a1
            1 hour ago
















          • $begingroup$
            Thanks; It'll take me a while to digest this but will get back when I have. Given your rep I guess this must be pretty optimal but it still seems like a lot of effort for something that I would have expected to be easier!
            $endgroup$
            – Julian Moore
            1 hour ago










          • $begingroup$
            @JulianMoore I just added a bunch of tweaks to make it work cleaner. It'd be doable in fewer lines if you wanted it to be less convenient I think.
            $endgroup$
            – b3m2a1
            1 hour ago















          $begingroup$
          Thanks; It'll take me a while to digest this but will get back when I have. Given your rep I guess this must be pretty optimal but it still seems like a lot of effort for something that I would have expected to be easier!
          $endgroup$
          – Julian Moore
          1 hour ago




          $begingroup$
          Thanks; It'll take me a while to digest this but will get back when I have. Given your rep I guess this must be pretty optimal but it still seems like a lot of effort for something that I would have expected to be easier!
          $endgroup$
          – Julian Moore
          1 hour ago












          $begingroup$
          @JulianMoore I just added a bunch of tweaks to make it work cleaner. It'd be doable in fewer lines if you wanted it to be less convenient I think.
          $endgroup$
          – b3m2a1
          1 hour ago




          $begingroup$
          @JulianMoore I just added a bunch of tweaks to make it work cleaner. It'd be doable in fewer lines if you wanted it to be less convenient I think.
          $endgroup$
          – b3m2a1
          1 hour ago

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Mathematica Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f195584%2fhow-to-improve-on-this-stylesheet-manipulation-for-message-styling%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Isurus Índice Especies | Notas | Véxase tamén | Menú de navegación"A compendium of fossil marine animal genera (Chondrichthyes entry)"o orixinal"A review of the Tertiary fossil Cetacea (Mammalia) localities in wales port taf Museum Victoria"o orixinalThe Vertebrate Fauna of the Selma Formation of Alabama. Part VII. Part VIII. The Mosasaurs The Fishes50419737IDsh85068767Isurus2548834613242066569678159923NHMSYS00210535017845105743

          Wolfenstein 3D Contents Availability Essential improvements Game data Video settings Input settings Audio settings Network VR support Issues fixed Other information System requirements NotesReferences    3D Realms Wolfenstein 3D pageGOG.com Community DiscussionsGOG.com Support PageSteam Community DiscussionsWolfenstein WikiOfficial websiteAmazon.comBethesda.netGamersGateGOG.comGreen Man GamingHumble StoreSteamweb browser versionWolfenstein 3D: Super UpgradesherehereUltraWolfhereWolfMenuECWolf Wolf4SDL WolfGL WinWolf3d NewWolf BetterWolf Sprite Fix and Rotation Project    Wolfenstein 3D VRSplitWolfWolfenstein 3D VRWolfenstein 3D VRWolfenstein 3D VR4DOS command shellFreeDOS's MORE.COMMacBin themthis shim fileWine regeditRELEASE: QUAKE II + III, WOLFENSTEIN 3D, RETURN TO CASTLE WOLFENSTEIN - GOG.com NewsMac Family - Wolfenstein Wiki - WikiaNerdly Pleasures: How many FPS? - DOS Games and Framerates

          Король Коль Исторические данные | Стихотворение | Примечания | Навигацияверсии1 правкаверсии1 правкаA New interpretation of the 'Artognou' stone, TintagelTintagel IslandАрхивировано