Message Boards Message Boards

How to find my notebook stylesheet?

Posted 5 years ago

A very simple question which I cannot answer: How can I find the name of my notebook stylesheet?

P1. Within a notebook "Styles etc.nb" It is easy to write:

    meNb = EvaluationNotebook[]
    NotebookObject[...Styles etc.nb ...]

    sdo = Options[meNb, StyleDefinitions]
    {StyleDefinitions -> 
      FrontEnd`FileName[{"Wolfram"}, "ProgrammingLabDefault.nb", 
       CharacterEncoding -> "UTF-8"]}

P2. The documentation fur Styledefinitions tells, I would get the stylesheet filename from that:

    Options[nb, StyleDefinitions]
    {StyleDefinitions -> "Default.nb"}

P3. But I get that interpreter type "FileName". I don't know how to evaluate that further. I would write:

    sd = Last[sdo[[1]]]
    FrontEnd`FileName[{"Wolfram"}, "ProgrammingLabDefault.nb", 
     CharacterEncoding -> "UTF-8"]

P4. I cannot Interpret that interpreter type "FrontEnd`FileName":

    Interpreter["FileName"][sd]
    Failure[... Enter a valid value...RestrictionFailure...]

or

    Interpreter["FrontEnd`FileName"][sd]
    ... FrontEnd`Filename is not a known type for interpreter 

P5. What I need is to take the filename parts out of P3. and write something like:

    ExpandFileName[ FileNameJoin[{"Wolfram", "ProgrammingLabDefault.nb"}]]
    C:\Users\Werner\OneDrive\Dokumente\Wolfram\ProgrammingLabDefault.nb
POSTED BY: Werner Geiger
6 Replies
Posted 5 years ago

What I could do is extract the parts of sd:

In[49]:= sd
Out[49]= FrontEnd`FileName[{"Wolfram"}, "ProgrammingLabDefault.nb", 
 CharacterEncoding -> "UTF-8"]

In[56]:= sdf = Append[sd[[1]], sd[[2]]]
Out[56]= {"Wolfram", "ProgrammingLabDefault.nb"}

In[57]:= sdf = FileNameJoin[sdf]
Out[57]= "Wolfram\\ProgrammingLabDefault.nb"

In[58]:= sdf = ExpandFileName[sdf]
Out[58]= "C:\\Users\\Werner\\OneDrive\\Dokumente\\Wolfram\\\
ProgrammingLabDefault.nb"

But this is somewhat brute force and no reasonable solution.

POSTED BY: Werner Geiger

Nothing fancier but you can use this:

https://mathematica.stackexchange.com/a/139349/5478

POSTED BY: Kuba Podkalicki
Posted 5 years ago

Werner, you could try this to get the name of the stylesheet:

sd0[[1, 2, 2]]

I have tested by creating a notebook in Textbook style

In[1]:= sd = Options[EvaluationNotebook[], StyleDefinitions]
Out[1]= {StyleDefinitions -> 
  FrontEnd`FileName[{"Book"}, "Textbook.nb", CharacterEncoding -> "UTF-8"]}

enter image description here

In[3]:= sd[[1, 2, 2]]
Out[3]= "Textbook.nb"
POSTED BY: Hans Milton
Posted 5 years ago

Thanks, Hans & Kuba. Both methods are solutions but not better than mine in my second post above

sdf = Append[sd[[1]], sd[[2]]]

Somewhat crazy.

The "Interpreter" reference documentation shows many examples, where Interpreter is resolved to what is expected. For example:

In[1]:= Interpreter["Number"]["4.5e7"]
Out[1]= 4.5*10^7

Hence I would expect

Interpreter["FileName"][sd]

to be expanded into the full filename. But it isn't. It gives a RestrictionFailure instead.

Anyway, I can live with my strange solution.

POSTED BY: Werner Geiger
Posted 5 years ago

I have to apologize to you, Kuba. It was nonsense to say that your solution is not better than mine.

In fact, my solution is wrong and yours normally works. Thanks a lot. I had tried my solution with a stylesheet from a subdirectory Wolfram\ProgrammingLabDefault.nb only.

We still have a problem, when we add any own additional StyleDefinitions. As an example:

setPS1[] := SetOptions[
  EvaluationNotebook[],
  StyleDefinitions -> Notebook[
    {
     (* Style Environment Names with correct Magnifications *)
     Cell[StyleData[All, "Printout"], Magnification -> 1.0],
     Cell[StyleData[All, "Working"] , Magnification -> 1.0 ]
     }
    ]
  ]

If we execute that once, the findStyleSheet-function will no longer work, since CurrentValue[thisNb,StyleDefinitions] does no longer give the StyleDefinitions for the stylesheet name, but that own StyleDefinitions prepended.

I think I can easily solve that circumstance, but I have no time at the moment.

POSTED BY: Werner Geiger
Posted 5 years ago

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[...], ...]}
POSTED BY: Werner Geiger
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract