11

The goal

To create a notebook whose options, when the notebook is opened, are fixed regardless of edits done to the notebook.

E.g. WindowSize -> Automatic and WindowMargins -> 30 should be a starting value even if one has saved that notebook from full screen mode.

The motivation

Notebooks are shared, e.g. through Google Drive, and opened on different devices. It is annoying to open a notebook, which was previously used with 250% magnification, taking half of 4K screen, on a 13" laptop.

The question

Is there a good approach which is flexible, doesn't trigger dynamic warning and if possible works from a stylesheet?

I've thought that maybe something like WindowSize -> Dynamic[dynamic, Initialization :> (dynamic = init)] where dynamic and init are references to specific TaggingRules.

It won't work because notebook options are overwritten. Moreover, Initialization can be triggered when the Kernel is restarted which is not desirable.

Edit:

I have not received any answers so I'm moving tests I've done to an answer. I think they fit there and the goal is to reduce unanswered stack. But I will gladly accept any answer that fulfills all stated requirements.

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • Like the notebooks in Documentation Center? – Silvia Sep 06 '16 at 10:35
  • @Silvia they are not saveable at all. I want to use them normally. – Kuba Sep 06 '16 at 10:38
  • Oh sorry I misunderstood your purpose... The FileChangeProtection option (for Notebook) looks interesting, might lead to a solution for your question. – Silvia Sep 06 '16 at 10:56
  • @Silvia no worries. I can't find any info about that option :) – Kuba Sep 06 '16 at 10:58
  • I saw it in Doc notebooks. They use it as FileChangeProtection -> None, I'm guessing maybe it also accepts a function value. – Silvia Sep 06 '16 at 11:09

1 Answers1

3

Approaches (none is perfect):

  1. NotebookDynamicExpression, something along those lines:

    NotebookDynamicExpression :> Refresh[
      SetOptions[EvaluationNotebook[], WindowSize -> Automatic], 
      None
    ]
    

    I don't like it. The reasons are:

At the end the method is poor because it triggers Enable Dynamic Updating warning in the notebook (on an untrusted path) while nothing serious is going on.

  1. NotebookEventActions

    NotebookEventActions :> {"WindowClose" :> (
      SetOptions[EvaluationNotebook[], WindowSize -> Automatic, WindowMargins -> 30];
      NotebookSave[]
      ),
      PassEventsDown -> True
    }
    

    Is not general enough as it misses things like NotebookClose[]. It also flashes before it is closed but this is not so important.

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • The "WindowClose" approach could instead be done on {"MenuCommand", "Save"}? That way any time it is saved that will fire (although I suppose this might be super annoying...). It might be a bit more robust. – b3m2a1 Jul 20 '18 at 21:11
  • @b3m2a1 yes, I do not want to readjust the window after each save :) – Kuba Jul 21 '18 at 06:19
  • 1
    One other option is to add a context-menu item that would do this. That way people could just right-click and apply via the menu. It still requires a bit of work on their part, but is totally applicable via stylesheet, isn't dynamic, and is robust. – b3m2a1 Jul 21 '18 at 06:27
  • @b3m2a1 sounds useful. My use case involves people that can be bothered with such stuff :) that is why I aim in a more automatc solution here. – Kuba Jul 21 '18 at 07:57