Is there any way I can programatically determine whether a notebook has unsaved edits?

Is that asterisk on the right of the notebook name represented in a notebook option or something, like HasUnsavedEdits?

  • $\begingroup$ As you suspected, the asterisk to the right of the notebook file name only appears when there are unsaved edits. $\endgroup$ – Eli Lansey Feb 2 '12 at 17:12
  • $\begingroup$ I know, I wanna know if I can programatically ask if the notebook "has the asterisk". Just edited to make it clear, my bad $\endgroup$ – Rojo Feb 2 '12 at 17:14
  • $\begingroup$ Ah, OK. The edits clarified the question. $\endgroup$ – Eli Lansey Feb 2 '12 at 17:19
  • $\begingroup$ I'm curious what your need for this is. Care to share? $\endgroup$ – Eli Lansey Feb 2 '12 at 17:37
  • 1
    $\begingroup$ @EliLansey I am adding a little function on my personal package of helper functions, to allow for automatic backuping of notebooks. You can set up the frequency of saves, if you want backup copies on a subdirectory, how often do you want them, and of course, stop the task... I wanted this info to avoid backing up a file again that hasn't changed since the last backup. "FileModificationTime" and "MemoryModificationTime" also come in handy. I hate not having undos, and having powercuts and blue screens of death $\endgroup$ – Rojo Feb 2 '12 at 17:50

I think that

"ModifiedInMemory" /. NotebookInformation@SelectedNotebook[]

does what you want (ie, returns False if the notebook is saved, True if it is not saved). Although maybe not quite, try NotebookInformation[CreateDocument["hi"]]

But it seems to work once you modify a notebook that's been saved once. I could be wrong though...

  • $\begingroup$ Yeah, thanks. I didn't know about NotebookInformation $\endgroup$ – Rojo Feb 2 '12 at 17:22
  • $\begingroup$ The document you created that way doesn't have the asterisk, nor pops up the Save Us dialog when you close it. It's a weird behaviour, but that information is exactly what I was looking for $\endgroup$ – Rojo Feb 2 '12 at 17:28
  • $\begingroup$ @Rojo there are notebook options that you can for these things, I think. But I'm not that familiar with this sort of thing. Thanks for the accept $\endgroup$ – acl Feb 2 '12 at 17:30

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.