How to select and delete all Output cells in multiple notebooks programmatically without needing to (manually) opening them?
How to define a hot key for deleting all Output cells within a notebook from that notebook?
Answer
Without adressing the hotkey issue (you could add a menu entry, or simply put an appropriate Button in your notebook), here is one variety that uses NotebookDelete and can be modified to include different CellStyles as well. The first argument nb defines which notebook to work on, the second argument styles defines the styles of to-be-deleted cells:
CleanNotebook[nb_: SelectedNotebook[],styles_: {"Output"}] :=
(NotebookFind[nb, #, All, CellStyle];
NotebookDelete[nb];) & /@ styles
Together with NotebookOpen, NotebookSave and NotebookClose and related functions you should be able to work on arbitrary notebooks programmatically.
Example:
docpath =
ToFileName[{$InstallationDirectory, "Documentation", "English",
"System"}, "ExampleData"];
nb = NotebookOpen[ToFileName[docpath, "document.nb"]]
CleanNotebook[nb]
Take care what other styles you choose, they´ll be gone for good...
Edit: You may also use the Option Visbible->False to supress the opening of a window for this notebook. This seems useful for batch processing (especially if Dynamic stuff is involved, because this will probably not trigger if invisible), but takes additional care to save and close programmatically because invisble windows do not show up in the menu bar.
Example:
doc = ToFileName[{$InstallationDirectory, "Documentation", "English",
"System", "ExampleData"}, "document.nb"];
bak = ToFileName[$TemporaryDirectory, "document_clean.nb"];
nb = NotebookOpen[doc, Visible -> False];
CleanNotebook[nb];
NotebookSave[nb, bak]
NotebookClose[nb]
NotebookOpen[doc];
NotebookOpen[bak, Visible -> True];
There is still an issue with the Visible option being saved in the backup notebook and hiding when reopened (thus re-setting Visible->True).

Comments
Post a Comment