Skip to main content
25 of 30
added 79 characters in body
b3m2a1
  • 48k
  • 3
  • 98
  • 251

I thought I'd keep a running tally of what I know about these packets (excluding the obvious ones like CreatePalettePacket) just for reference.


Text / Box interchange

###ReparseBoxStructurePacket

This one parses a string (as far as I can tell just a string) into a box structure. We can use this to, for example, parse out the string which comes as the last output of NeedCurrentFrontEndSymbolsPacket and convert it directly to a box expression:

Cell[ BoxData@ FrontEndExecute[ FrontEnd`ReparseBoxStructurePacket[ FrontEndExecute@FrontEnd`NeedCurrentFrontEndSymbolsPacket[] // Last]], "Output" ] // CellPrint 

###ExportPacket

This one is enormously useful. I first saw it here.

The basic usage is:

ExportPacket[ expr:_Cell|_Notebook, fmt_, opts___?OptionQ ] 

You can also call it like:

ExportPacket[ expr:_Cell|_Notebook, fmt_, file_ opts___?OptionQ ] 

in which case it dumps to a file.

You can learn more about the supported formats here and here

GetBoundingBoxSizePacket

This computes the display size of Cell expressions. It's just called as:

FrontEnd`GetBoundingBoxSizePacket[c_Cell] 

It returns the horizontal size, the vertical content size, and what I believe is the vertical content padding as its third result. I use it here and here


Syntax-coloring

###UpdateKernelSymbolContexts

Used to set symbol coloring in the FE. See this for more info

###AddUsedToGenerateSideEffectGraphicsFunctions

This tells the front end that this function used to generate side effects, back when Graphics were side effects.

It's used like:

FrontEnd`AddUsedToGenerateSideEffectGraphicsFunctions[ { {context1, { function1, function2, ... } }, {context2, { function3, function4, ... } }, ... } ] 

As an example you can try:

FrontEndExecute@ FrontEnd`AddUsedToGenerateSideEffectGraphicsFunctions[ { {"Global`", {"notARealGraphics"}} } ] 

And now if you type:

notARealGraphics[]; 

that final semicolon will be colored red

AddOptionInformationToFunctions

This is used like AddUsedToGenerateSideEffectGraphicsFunctions:

FrontEnd`AddOptionInformationToFunctions[ { {context1, { {function1, {opName1, ...}}, {function2, {opName2, ...}}, ... } }, {context2, { {function3, {opName3, ...}}, {function4, {opName4, ...}} ... } }, ... } ] 

Here's an example:

FrontEndExecute@ FrontEnd`AddOptionInformationToFunctions[ { { "Global`", { {"func", {"a", "b", "c"}} } } } ] 

func[notABOrC->] will then have notABOrC colored red. This is clearly how SyntaxInformation "OptionNames" work.


##Autocompletion

###CA`CADumpTriePacket

Seems to extract data from an autocompletion .trie file:

Used like

FrontEndExecute@ CA`CADumpTriePacket[trieFile] 

e.g.:

FrontEndExecute@ CA`CADumpTriePacket@First@ FileNames["*.trie", PacletFind["AutoCompletionData"][[1]]["Location"], Infinity][[1]] 

BEWARE: If the file isn't a properly formatted trie file this will crash your system.


##Resource finding

###FindFileOnPath

Used as:

FrontEndExecute@ FrontEnd`FindFileOnPath[ fileName, path ] 

Where path is one of:

{ "AddOnHelpPath", "AutoloadPath", "CharacterEncodingsPath", "ConfigurationPath", "ConvertersPath", "NotebookPath", "NotebookSecurityOptionsTrustedPath", "NotebookSecurityOptionsUntrustedPath", "PalettePath", "PreferencesPath", "PrivatePathsAFM", "PrivatePathsAutoCompletionData", "PrivatePathsBitmaps", "PrivatePathsFonts", "PrivatePathsSystemResources", "PrivatePathsTextResources", "PrivatePathsTranslationData", "SpellingDictionariesPath", "StyleSheetPath", "SystemHelpPath" } 

Beware: if you provide the wrong path it crashes Mathematica instantly

Note that path can be things built from FileNameJoin.


Box edits

###BoxReference

There's a collection of edit mechanisms predicated on FrontEnd`BoxReference* packets.

These all use FE`BoxReference (as far as I've seen). The standard usage of this as I've seen looks like:

FE`BoxReference[obj, {{id}}, FE`SearchStart->startspec FE`BoxOffset->{offset} ] 

Where the options can be dropped.

  • obj as far as I can tell is a NotebookObject, CellObject, or BoxObject, but you can also get the current spec from from FE`Evaluate@FEPrivate`Self[]
  • The id searches for BoxID->id. It can also be FE`Parent[id]. I don't know what that does.
  • The offset should look like FE`BoxChild[n] or FE`BoxParent[n].
  • The only SearchStart I've seen used is "StartFromBeginning".

Here are some usages:

  • FrontEnd`BoxReferenceBoxObject (BoxReferenceBoxObject[boxref])

finds the BoxObject for FE`BoxReference

  • FrontEnd`BoxReferenceRead (BoxReferenceRead[boxref])

reads the BoxObject for FE`BoxReference

  • FrontEnd`BoxReferenceFind (BoxReferenceFind[boxref])

Selects the BoxObject for FE`BoxReference

  • FrontEnd`BoxReferenceReplace (BoxReferenceReplace[boxref,boxes])

Replaces the BoxObject for FE`BoxReference

  • FrontEnd`BoxReferenceGetOptions (BoxReferenceGetOptions[boxref, ops]?)

I don't actually know how this one works

  • FrontEnd`BoxReferenceSetOptions (BoxReferenceSetOptions[boxref, ops]?)

Same here


Keyboard Input

###SimulateKeyPress

This one is weird and I don't really understand it. We'll start with the basic usage:

FrontEndExecute@ FrontEnd`SimulateKeyPress[ keys:_String|_Symbol|_Integer ]; 

Any Symbol passed is simply converted to its fully qualified string form (except for System` level symbols which simply take their SymbolName), the same is true for integers.

The system then simulates a key press for each character in the string. What is odd is that the characters either all come out as if Shift was held or in their lower-case form. That is, the $FrontEndSession will either come out as $FRONTENDSESSION or as 4frontendsession. I have been unable to figure out what decides this.

Note that holding a modifier has the same effect as if one was typing.

On top of the weirdness with capitalization, there is a second valid usage form:

FrontEndExecute@ FrontEnd`SimulateKeyPress[ keys:_String|_Symbol|_Integer, list_List, ___ ]; 

Note that having anything other than a list in the second position does nothing. Oddly, though, I can't figure out what that list is for. Initially I had assumed it was a list of modifier keys. But no set of modifier key strings seems to do anything. In fact I can't find any effect whatsoever of having anything in that list. Note that any argument can follow that list, though. But that second position list is clearly important for something. I just cannot figure out what.


Cursor click/options

###SimulateMouseMove / SimulateMouseDrag / SimulateMouseClick

SimulateMouseMove moves the mouse to a given screen position. Have yet to find a use for it, but it does allow one to play tricks on a user by putting something like:

Dynamic[ FrontEndExecute@ FrontEnd`SimulateMouseMove[ $FrontEndSession, MousePosition[] ], UpdateInterval -> 0 ] 

in a notebook. There might be an option for moving to a certain named position, e.g., Center but I haven't found it. I just wrote that into a wrapper function.

SimulateMouseDrag and SimulateMouseClick are similarly obvious:

SimulateMouseDrag takes a list of positions {start, end} and simulates a mouse drag between the two.

SimulateMouseClick just clicks on a position.

A sample usage:

FrontEndExecute@{ FrontEnd`SimulateMouseDrag[ $FrontEndSession, {{1200, 30}, {1200, 50}} ], FrontEnd`SimulateMouseClick[ $FrontEndSession, {1200, 50} ] } 

On my Mac, with screen rectangle of {{4, 1440}, {23, 900}} this drags a window docked at the top right corner down 20 points.

The click is necessary, I found, to get the OS to recognize the mouse drag.

###SetMouseAppearance FrontEndExecute[FrontEnd`SetMouseAppearance[expr_]] sets the mouse appearance

###FindAndClickDefaultButton / FindAndClickCancelButton

See this

These find and click the first button with "ButtonType" -> "Cancel" or "ButtonType" -> "Default" in the Appearance list.


Attached cells

AttachedCellParent

This is exactly what you'd expect. Except for 1 big warning. If the CellObject no longer exists it'll crash Mathematica.

###AttachCell / DetachCell

Found here

Used as

FrontEndExecute@ FrontEnd`AttachCell[obj: (_CellObject | _BoxObject), cellExpr_, { radialDistanceFromAnchor: (_Integer | _Scaled), alignment: {Center | Left | Right, Center | Bottom | Top} }, anchor: { Center | Left | Right | _Scaled | _Integer | _Real, Center | Bottom | Top | _Scaled | _Integer | _Real }, "ClosingActions"-> { ( "ParentChanged" | "EvauatorQuit" | "OutsideMouseClick" | "SelectionDeparture" | "MouseExit") ... } ] 

From Kuba, when attaching to a notebook using the "ParentChanged" setting will crash Mathematica as detailed here

FrontEnd`DetachCell simply detaches an attached cell.


Menu edits

###AddMenuCommands Described here

Used as

FrontEndExecute@ FrontEnd`AddMenuCommands[menu,{items}] 

where menu is the target menu and items are MenuItem/Item expressions or `Delimiter.

See also: https://mathematica.stackexchange.com/a/6227/38205

Which shows how to do this with a "MenuList*" menu.

###ResetMenusPacket

This can be used to both reset the menus and set an arbitrary Menu structure.

Used as:

FrontEndExecute@ ResetMenusPacket[{menu}] 

where menu is either a Menu expression looking like

Menu["Mathematica", {menu_items_and_submenus}] 

or Automatic as far as I've discovered. I'm sure there are more use cases but these are the ones I've found.

More info here


##Obvious but useful

###NotebookGetLayoutInformation

Provides useful information for anyone working with writing papers in mathematica as it gives access to cell sizes and page break info.

###NotebookDynamicToLiteral

Converts the current dynamic selection to its static displayed form.

###SelectObject

Moves the selection to a given FE object

###SelectedObject FrontEndExecute@FrontEnd`SelectedObject[nb] returns whatever objects are selected in a notebook

###ObjectChildren

Provides access to the object hierarchy in a notebook. Particularly useful for getting the boxes in a given cell / box. At the $FrontEnd level though this will give you all notebooks which is useful in and of itself.


A note on GetFEKernelInit

A large amount of useful functionality is hidden in here which I simply haven't gotten around to investigating. For example there's this:

System`DeclareKnownSymbols[l_] := MathLink`CallFrontEnd[ FrontEnd`UpdateKernelSymbolContexts[ $Context, FrontEnd`Private`ResolvedContextPath[], {{$Context, {}, {}, l, {}}}] ] 

which includes that FrontEnd`UpdateKernelSymbolContexts which had been crashing my copy of Mathematica repeatedly

You can find the file by:

FrontEndExecute@ FrontEnd`FindFileOnPath[ "GetFEKernelInit.tr", "PrivatePathsTextResources" ] 

#Warning

Lots of these crash the system if used improperly. For example a bare FrontEnd`UpdateDynamicObjects or FrontEnd`UpdateDynamicObjectsSynchronous will crash the system. You have to pass it the results from Internal`GetTrackChanges (e.g. Dynamic[a++; Internal`GetTrackChanges[]]) which seems to be the Box ID number.

b3m2a1
  • 48k
  • 3
  • 98
  • 251