Simple Answer
So I mentioned in a comment that your method will work. The only issue is that there is a style specification with higher precedence that gets applied after your styles. If you look at "Package.nb"
they use "InitializationCell"
to specify the CellFrameColor
and whatnot. If you override that style instead you're golden.
Since 11.0 (I believe) the "InitializationCell"
style gets applied to anything with InitializationCell -> True
. "InitializationCell"
is much like StandardForm
in that it's applied after standard Cell
styles (and so has higher precedence).
Overkill Answer
On the other hand, you can actually edit the "Package.nb"
styles in place without overwriting the file. This will be a code-intensive answer, so I'll put it all in one block at the end.
First off we need a way to find the style cells. This is relatively easy. We'll do a very simple match. Matching more syntax and vectorizing would be better (I've done that for myself) but requires many more lines of code.
cellMatchQ[Cell[StyleData[name : Except[_Rule], ___], ___], style_] :=
Switch[{name, style},
{_String, _String | _StringExpression | _Alternatives},
StringMatchQ[name, style],
_,
MatchQ[name, style]
];
StylesheetCells[nb_NotebookObject, style_] :=
Select[Cells[nb],
cellMatchQ[NotebookRead@#, style] &
];
Then we need a system for editing the cells:
StylesheetApplyEdits[cells : {__CellObject}] :=
With[{e = EvaluationCell[]},
Do[
SelectionMove[c, All, Cell,
AutoScroll -> False];
FrontEndTokenExecute[ParentNotebook@c, "ToggleShowExpression"];
FrontEndTokenExecute[ParentNotebook@c, "ToggleShowExpression"];,
{c, cells}
];
SelectionMove[e, After, Cell]
];
Options[StylesheetEdit] =
Options@Cell;
StylesheetEdit[cells : {__CellObject}, ops : OptionsPattern[]] :=
(
Do[SetOptions[c, ops], {c, cells}];
StylesheetApplyEdits@cells;
);
StylesheetEdit[nb_NotebookObject, style_, ops : OptionsPattern[]] :=
Replace[StylesheetCells[nb, style], {
{} -> Null,
c : {__} :> StylesheetEdit[c, ops]
}];
Note that the ApplyEdits
call is necessary so the cascade updates properly.
Now we'll use a method for selecting stylesheet notebooks to thread this all together:
$PackageStyleNotebook :=
SelectFirst[FrontEndExecute@FrontEnd`ObjectChildren[$FrontEnd],
Quiet@NotebookFileName@# ===
FileNameJoin@{$InstallationDirectory, "SystemFiles", "FrontEnd",
"StyleSheets", "Package.nb"} &]
If there are any packages open, this will return the NotebookObject
that defines their styles.
Now we can use this as such:
StylesheetEdit[$PackageStyleNotebook, "InitializationCell",
CellFrameColor -> Pink
]
Note that this doesn't save any style changes. So every time you close the last of your package notebooks all your styles will get erased. But this can be applied to any style notebook. Which is what makes it fun and powerful. For myself I've also added a StylesheetNew
function that will write appropriately configured cells to a notebook, and that can be used with this functionality to easily add new styles.
This is overkill, but fun and flexible overkill.
Now, as promised, here's everything in a block:
cellMatchQ[Cell[StyleData[name : Except[_Rule], ___], ___], style_] :=
Switch[{name, style},
{_String, _String | _StringExpression | _Alternatives},
StringMatchQ[name, style],
_,
MatchQ[name, style]
];
StylesheetCells[nb_NotebookObject, style_] :=
Select[Cells[nb],
cellMatchQ[NotebookRead@#, style] &
];
StylesheetApplyEdits[cells : {__CellObject}] :=
With[{e = EvaluationCell[]},
Do[
SelectionMove[c, All, Cell,
AutoScroll -> False];
FrontEndTokenExecute[ParentNotebook@c, "ToggleShowExpression"];
FrontEndTokenExecute[ParentNotebook@c, "ToggleShowExpression"];,
{c, cells}
];
SelectionMove[e, After, Cell]
];
Options[StylesheetEdit] =
Options@Cell;
StylesheetEdit[cells : {__CellObject}, ops : OptionsPattern[]] :=
(
Do[SetOptions[c, ops], {c, cells}];
StylesheetApplyEdits@cells;
);
StylesheetEdit[nb_NotebookObject, style_, ops : OptionsPattern[]] :=
Replace[StylesheetCells[nb, style], {
{} -> Null,
c : {__} :> StylesheetEdit[c, ops]
}];
$PackageStyleNotebook :=
SelectFirst[FrontEndExecute@FrontEnd`ObjectChildren[$FrontEnd],
Quiet@NotebookFileName@# ===
FileNameJoin@{$InstallationDirectory, "SystemFiles", "FrontEnd",
"StyleSheets", "Package.nb"} &]
"InitializationCell"
, not"Code"
. That's where"CellFrame*"
is defined in"Package.nb"
. $\endgroup$InitializationCell->True
set will inherit from styledata from "InitializationCell", no matter what its actual style is. I think either the behavior or one of the stylesheets has changed in version 11.1, but maybe it only does now behave as was originally intended... $\endgroup$