5
$\begingroup$

Is there a more idiomatic way to get stylesheet's path from its name? (assuming it is accessible for the FrontEnd)

Module[{
        found
      , i = 0
      , dirs = Reverse[ToFileName /@ AbsoluteCurrentValue[StyleSheetPath]]
    }
  , While[
        found = FileNames["Default.nb", dirs[[++i]]]
      ; Length[found] < 1 && i != Length[dirs]
    ]
  ; Last @ Prepend[found, $Failed]
]

The code above is suspiciously long for something as basic as that.

This does not count as it is too slow:

FileNames[
    "Default.nb"
  , Reverse[ToFileName /@ AbsoluteCurrentValue[StyleSheetPath]]
]
$\endgroup$
  • 2
    $\begingroup$ This seems to work: Block[{$Path = Join[$Path, Reverse[ToFileName /@ AbsoluteCurrentValue[StyleSheetPath]]]}, FindFile["Default.nb"] ] $\endgroup$ – Leonid Shifrin Feb 27 '17 at 15:04
  • 1
    $\begingroup$ @LeonidShifrin @Kuba shouldn't Block[{$Path = ToFileName /@ AbsoluteCurrentValue[StyleSheetPath]}, FindFile["Default.nb"]] be enough? $\endgroup$ – Rolf Mertig Feb 27 '17 at 20:14
  • $\begingroup$ @LeonidShifrin and Rolf, either way it won't be so easy, it seems that paclets directories are put at the end of StyleSheetPath while the list starts with $UBDir -> $BDir -> $IDir. So either we need to sort them or paclets styles are really the least specific. Though I'm too tired to check. $\endgroup$ – Kuba Feb 27 '17 at 21:19
  • $\begingroup$ @RolfMertig ok, checked, paclet directories are really the least specific. So Reverse is wrong. $\endgroup$ – Kuba Feb 27 '17 at 22:00
2
$\begingroup$

Built on Rolf's and Leonid's suggestions, a general function for that:

FindStyleSheet[] := FindStyleSheet[EvaluationNotebook[]];

FindStyleSheet[nb_NotebookObject] := FindStyleSheet[
    CurrentValue[nb, StyleDefinitions]
];

FindStyleSheet[stylesheet : (_FileName | _FrontEnd`FileName)] := 
  FindStyleSheet @ ToFileName @ stylesheet;

FindStyleSheet[string_String?FileExistsQ] := string;


    (*this is the core one but I extendend definitions*)

FindStyleSheet[string_String] := Block[
    {$Path = ToFileName /@ AbsoluteCurrentValue[StyleSheetPath]}
  , FindFile[string]
];



FindStyleSheet[___]:=Failure[
    "argpatt"
  , "Message" -> StringTemplate[General::argpatt][FindStyleSheet]
]

So even if you set the stylesheet to Wolfram / Demonstrations it will return:

FindStyleSheet[]
"[...] \\ Mathematica \\ 11.0 \\ SystemFiles \\ FrontEnd \\ StyleSheets \\ Wolfram \\ Demonstration.nb"
|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.