Background: I'd like to put together a little utility to automate publishing my notebooks to CDF. Right now this involves three steps:

  1. Copy the current stylesheet definitions (by selecting Format->Edit Stylesheet..., opening up my custom stylesheet, and copying all cells using Ctrl-A), switch the style to the Default style (by selecting it in Format->Stylesheet), and pasting the custom definitions back into the notebook's private stylesheet (by selecting Format->Edit Stylesheet... and pasting the definitions I had copied before in there). The result of these steps is a notebook that has its style definitions "baked in", so it's portable, which is the crucial part, of course.
  2. Remove all input cells - Easy, I have the code for that
  3. Export the notebook in the appropriate CDF format - Also easy, I just use CDFDeploy

Question: So the issue is item 1, where I have trouble figuring out how to do these steps programmatically. I think I know how to read in the Stylesheet I want (well, at least as long as it's always the same, then I can open the notebook file associated with it; ideally my command would automatically find the right notebook file, or even get the definitions from memory, but I don't even know if that's possible), but how do I get the style definitions inserted into the notebook's private stylesheet? I see there's commands to switch to different stylesheets, but I haven't found a command to insert stylesheet definitions in the notebook's local ("built-in") stylesheet.

I think this is different from what the code here does; the solution there mentions as an assumption that "the parent stylesheet (myparent.nb) of private stylesheet for a notebook is not a built in stylesheet; and that the stylesheet has one or more custom styles that inherit from myparent.nb". The second one of these does not apply to my situation. The private stylesheet of my notebooks is usually empty, and thus does not inherit anything from anywhere.

  • $\begingroup$ I'm not sure if this does what I want. I tried the stylesheetSetter function shown in the post you referenced, but can't get it to work. It comes back with error messages: First::nofirst: {} has zero length and no first element., and StringJoin::string: String expected at position 2 in C:\Users\Dietmar\AppData\Roaming\Mathematica\SystemFiles\FrontEnd\StyleSheets\<>First[{}]. $\endgroup$ – Pirx Oct 3 '16 at 20:18
  • $\begingroup$ So how does your notebook look? Notebook[{}, ... StyleDefinitions -> "pathToCustomStyles"]? Or does it have StyleDefinitions->Notebook[...]? $\endgroup$ – Kuba Oct 5 '16 at 19:46
  • $\begingroup$ It's the latter; all style definitions are in a stylesheet .nb file in the user's local Stylesheets folder. $\endgroup$ – Pirx Oct 5 '16 at 19:49
  • $\begingroup$ No, at the end of the notebook file there's a declaration that says StyleDefinitions->Notebook[{ Cell[ StyleData[StyleDefinitions -> "customstylesheetname.nb"]] $\endgroup$ – Pirx Oct 5 '16 at 19:54
  • $\begingroup$ I'm sorry, but it looks like I'm not understanding your question(s). Can you rephrase them for me? I realize the fault for the miscommunication is entirely mine... $\endgroup$ – Pirx Oct 5 '16 at 20:44