13
$\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$
3
  • 1
    $\begingroup$ NotebookImport can import notebooks as plain text, that might be a start. $\endgroup$
    – C. E.
    Sep 4, 2020 at 18:00
  • 2
    $\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, 2020 at 19:33
  • $\begingroup$ The plugin for Spotlight is included in the app bundle. There is nothing to install separately. $\endgroup$
    – ihojnicki
    Dec 3, 2023 at 16:12

1 Answer 1

15
$\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[.

$\endgroup$

Your Answer

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

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