I think I have a reasonable function findStyleSheet now. It works for stylesheets in the standard StyleSheets-directory and their subdirectories. And it handles the case where own style definitions had been prepended to the StyleSheet.
The function is based on Kuba's work, see above. I have changed:
- The function name to findStyleSheet (from FindStyleSheet).
- Calling with no arguments or a given notebook (normally EvaluationNotebook[])
is handled within the first routine.
- In the case of own style definitions, CurrentValue[...] is called again to
get to the FileName.
For testing, there are many Echo-Brackets in the function. They shall, of course, all be removed.
findStyleSheet[nb_NotebookObject: Null] :=
Block[{thisNb, sdv, sdv2, sd, sdffn},
Echo[thisNb =
If[Head[nb] === NotebookObject, nb, EvaluationNotebook[]],
"thisNb: "];
Echo[sdv = CurrentValue[thisNb, StyleDefinitions], "sdv: "];
If[Head[sdv] === Notebook,
Echo[sdv = CurrentValue[sdv, StyleDefinitions], "sdv-2"]
];
Echo[sdffn = findStyleSheet[sdv], "sdffn=fSS[sdv]: "];
{sdffn, FileExistsQ[sdffn], thisNb}
]
(* --- a Stylesheet Filename *)
findStyleSheet[stylesheet : (_FileName | _FrontEnd`FileName)] :=
Block[{sdfn},
Echo[stylesheet, "stylesheet: "];
Echo[sdfn = findStyleSheet @ ToFileName @ stylesheet, "sdfn: "]
]
(* --- already an existing file *)
findStyleSheet[string_String?FileExistsQ] := Block[{},
Echo[string, "string: "];
string
]
(* --- this is the core one but I extended definitions *)
findStyleSheet[string_String] := Block[{$Path},
Echo[$Path = ToFileName /@ AbsoluteCurrentValue[StyleSheetPath],
"$Path: "]; FindFile[string]
];
findStyleSheet[___] :=
Failure["argpatt",
"Message" -> StringTemplate[General::argpatt][findStyleSheet]]
Call it with no argument or any notebook object. it returns a list:
{the stylesheets full filename,
True|False if that file exists (just to be sure),
the given or derived notebook (normally EvaluationNotebook[]}
As an example:
findStyleSheet[]
{"C:\\Program Files\\Wolfram Research\\Wolfram Programming \
Lab\\12.0\\SystemFiles\\FrontEnd\\StyleSheets\\Wolfram\\\
ProgrammingLabDefault.nb", True,
NotebookObject[FrontEndObject[...], ...]}