How to improve on this Stylesheet Manipulation for Message Styling
$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?
stylesheet coding-style
$endgroup$
add a comment |
$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?
stylesheet coding-style
$endgroup$
add a comment |
$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?
stylesheet coding-style
$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
stylesheet coding-style
edited Apr 19 at 10:51
mikado
6,9171929
6,9171929
asked Apr 19 at 8:35
Julian MooreJulian Moore
9471515
9471515
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$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}|>]
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}|>]
$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
Apr 19 at 9:50
$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
Apr 19 at 9:52
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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}|>]
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}|>]
$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
Apr 19 at 9:50
$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
Apr 19 at 9:52
add a comment |
$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}|>]
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}|>]
$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
Apr 19 at 9:50
$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
Apr 19 at 9:52
add a comment |
$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}|>]
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}|>]
$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}|>]
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}|>]
answered Apr 19 at 9:45
b3m2a1b3m2a1
29.2k360167
29.2k360167
$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
Apr 19 at 9:50
$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
Apr 19 at 9:52
add a comment |
$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
Apr 19 at 9:50
$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
Apr 19 at 9:52
$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
Apr 19 at 9:50
$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
Apr 19 at 9:50
$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
Apr 19 at 9:52
$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
Apr 19 at 9:52
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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