1
$\begingroup$

I would like to programmatically get the titles that appear at the top of notebook windows, particularly for notebooks that have not yet been saved (if they have been saved, I can get the information I am after by taking the correct Part of NotebookFileName[NotebookObjectOfInterest]). For example, in a session where a Message notebook has been produced by the front end, I would like to do operations on this notebook. The problem is that Notebooks[] returns a randomly-ordered (as far as I can tell) list of all open notebooks, and I don't know how to specify the one corresponding to Messages. The notebook objects themselves seem to know about their titles, as when you evaluate Notebooks[], the NotebookObjects that result display their titles when rendered as an output cell (and I can even see the corresponding text when I perform CTRL-SHFT-e on this cell), but I cannot for the life of me determine how to get at this information programmatically (Evaluating Notebooks[] // FullForm returns a list of NotebooksObjects where the human-readable titles are not accessible). As a bonus, I would also like to be able to determine if the notebook has been saved since its last modification (corresponding to the "*" that appears on the titlebar of notebooks with unsaved modifications. Currently, I can determine if a notebook has been saved at least once (by calling NotebookFileName on it and testing for $Failed), but don't know how to test for the most recent modification time.

Final point of attack: is there a way to see the cell expression that WOULD be produced by an evaluation without having the front end actually produce the cell? That would get me most of the way towards a solution (though it would be inelegant and require pattern matching the cell expression produced by Notebooks[]).

Thanks!

$\endgroup$
2
  • 2
    $\begingroup$ Hi. Please try to rephrase your question. I think it can be asked in 2 lines. p.s. WindowTitle /. AbsoluteOptions /@ Notebooks[] $\endgroup$
    – Kuba
    Nov 14, 2014 at 23:18
  • $\begingroup$ To paraphrase someone, I wrote you a long question because I didn't have time to write you a short one. +1 for AbsoluteOptions; I was not aware of that use of it. Still hoping for a way to extract the "*" modification flag from the titlebar, or to determine in another way that the Notebook has been modified since last save. $\endgroup$
    – Paco Jain
    Nov 15, 2014 at 0:56

1 Answer 1

3
$\begingroup$

You need the values of the options "WindowTitle" and "ModifiedInMemory". These can be obtained using the function NotebookInformation.

For notebooks that have not been modified since the last save the value of the option "ModifiedInMemory" is True. So, you can do:

{"WindowTitle", "ModifiedInMemory"} /. (NotebookInformation /@ Notebooks[])

to get all the window titles together with the information on whether there has been unsaved modifications.

To get the WindowTitles of the notebooks that have not been saved, you can use

Pick @@ Transpose[{"WindowTitle", "ModifiedInMemory"} /. (NotebookInformation /@ Notebooks[])]
$\endgroup$
2
  • $\begingroup$ Thanks so much! It's remarkable that I didn't find out about NotebookInformation after 2+ hours of going through the documentation and searching stack exchange, but it's just what I need. Thanks again for pointing it out. FYI, I'm using this to write my own autosave scheduled task that goes above and beyond the built-in version, and will post the code as a self-answered question on SE once I have it the way I want it. $\endgroup$
    – Paco Jain
    Nov 16, 2014 at 22:17
  • $\begingroup$ @rdjain1, my pleasure. Thank you the Accept. Welcome to mma.se. Looking forward to seeing you post. $\endgroup$
    – kglr
    Nov 16, 2014 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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