11
$\begingroup$

I have hundreds of Mathematica notebooks accumulated over the years, and I'd like to search over them.

I've been using various versions of text search (grep, SilverSearcher, Spotlight), but there are limitations. For instance, just now I needed to find where I have used the utility function toc. Searching for text string toc[ doesn't work because the raw text in .nb is RowBox[{"toc", "["

Older versions of Mathematica offered to install notebook indexing plug-in for Spotlight, but I no longer get this option.

Any suggestions?

$\endgroup$
  • 1
    $\begingroup$ NotebookImport can import notebooks as plain text, that might be a start. $\endgroup$ – C. E. Sep 4 at 18:00
  • 1
    $\begingroup$ The CreateSearchIndexer/TextSearch approach doesn't seem to work. I have just tried TextSearch[index, "toc[" ] and it returns all the files that contain "toc[" and all the files that contain only "toc" without possible distinction. $\endgroup$ – andre314 Sep 4 at 19:33
12
$\begingroup$

You can convert your notebooks to packages as shown here:-

convert[notebook_, package_] := Module[{nb, str},
  nb = NotebookOpen[notebook, Visible -> False];
  SelectionMove[nb, All, Notebook];
  str = First[
    FrontEndExecute[
     FrontEnd`ExportPacket[NotebookSelection[nb], "InputText"]]];
  NotebookClose[nb];
  Export["text.txt", str];
  Quiet[DeleteFile[package]];
  RenameFile["text.txt", package];]

Example converting one notebook:

convert["notebook.nb", "notebook.m"]

Then grep the .m files for toc[.

| improve this answer | |
$\endgroup$

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.