Converting a notebook to plain text programmatically
Saving is done by the front end, which you can exploit programatically by using FrontEndExecute
. I think this is what you need:
nb=InputNotebook[];
fn=FileNameJoin[{"output.txt"}];
FrontEndExecute[FrontEndToken[nb,"Save",{fn,"Text"}]]
Edit: Of course you can also save it as a package by replacing "Text" with "Package".
If you want to completely bypass the FE you can use:
Import["your.nb", "Plaintext"]
This will return a textual representation of the notebook file in the form of a String expression, which you can then post-process or export however you see fit.
The results are not perfect, but they are decent. This is the mechanism used by the Spotlight plugin on Mac, the Google Desktop and other plugins on Windows, and the comparable mechanism on Linux (I forget the name).
Starting with version 10.1 you can also use NotebookImport
to scrape the text from a notebook:
https://reference.wolfram.com/language/ref/NotebookImport.html