13

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?

Yaroslav Bulatov
  • 7,793
  • 1
  • 19
  • 44
  • 1
    NotebookImport can import notebooks as plain text, that might be a start. – C. E. Sep 04 '20 at 18:00
  • 2
    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. – andre314 Sep 04 '20 at 19:33
  • The plugin for Spotlight is included in the app bundle. There is nothing to install separately. – ihojnicki Dec 03 '23 at 16:12

1 Answers1

15

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[.

Chris Degnen
  • 30,927
  • 2
  • 54
  • 108