Mathematica Programming » Assorted Tricks

Info Scraping

Mathematica, being so symbolic, is a particularly easy language to scrape and get information from, particularly given the rich pattern language it supports.

Because of this, we can learn a lot by simply pulling relevant info from the source files of the program. First we need a way to get the source files:

 internalFiles[namePattern_,directoryExtensions___,depth:_Integer|∞:∞]:=
FileNames[namePattern,
 FileNameJoin@{
  $InstallationDirectory,
  directoryExtensions
  },
 depth];

Then we can pull things from these according to a pattern:

 expressionsSearch[pat_,files__]:=
Table[
 f->Quiet@
 Cases[
  Switch[FileExtension@f,
   "m"|"wl",
   Import[f,"HeldExpressions"],
   _,
   Import[f]
   ],
  pat,
  ∞
  ],
 {f,Replace[Hold[files],
   Except@Hold[_List]internalFiles[files]
   ]}
 ]//DeleteDuplicates;

We’ll use this to figure out how CurrentValue is used in Mathematica’s built-in stylesheets:

 Join@@Last/@expressionsSearch[c:_CurrentValueHoldForm[c],
 "*.nb","SystemFiles","FrontEnd","StyleSheets"]//DeleteDuplicates//Sort
 {CurrentValue["ControlsFontFamily"],CurrentValue["ControlsFontSize"],CurrentValue["HyperlinkModifierKey"],CurrentValue["MenuFontFamily"],CurrentValue["MenuFontSize"],CurrentValue["MouseOver"],CurrentValue["PanelBackground"],CurrentValue["PanelFontFamily"],CurrentValue["PanelFontSize"],CurrentValue["ShiftKey"],CurrentValue["ToolbarBackground"],CurrentValue["TooltipFontFamily"],CurrentValue["TooltipFontSize"],CurrentValue["TooltipFrameMargins"],CurrentValue[Enabled],CurrentValue[Evaluatable],CurrentValue[{"SystemColor","DialogText"}],CurrentValue[{"SystemColor","DialogTextDisabled"}],CurrentValue[{"SystemColor","Menu"}],CurrentValue[{"SystemColor","MenuText"}],CurrentValue[{"SystemColor","Tooltip"}],CurrentValue[{"SystemColor","TooltipFrame"}],CurrentValue[{"SystemColor","TooltipText"}],CurrentValue[{StyleHints,"CodeFont"}],CurrentValue[EvaluationNotebook[],WindowElements],CurrentValue[EvaluationNotebook[],WindowMargins],CurrentValue[EvaluationNotebook[],WindowSize],CurrentValue[EvaluationNotebook[],{AutoStyleOptions,"StringStyle"}],CurrentValue[EvaluationNotebook[],{TaggingRules,"ExampleCounter"}],CurrentValue[EvaluationNotebook[],{TaggingRules,"SlideShowSettings","WindowSize"}]}

There’s a lot of info in the source files and by doing things like this you can mine a lot of it.