# Return list from FrontEndExecute

This is a question that's been bugging me for quite some time, now.

When we call the FE in a batched mode we can only get one result out. For instance, we could try to get two stylesheet files. First unbatched:

FrontEndExecute /@
{
FrontEndFindFileOnPath["Default.nb", "StyleSheetPath"],
FrontEndFindFileOnPath["ReverseColor.nb", "StyleSheetPath"]
}

{
"/Applications/Mathematica.app/Contents/SystemFiles/FrontEnd/StyleSheets/Default.nb",
"/Applications/Mathematica.app/Contents/SystemFiles/FrontEnd/StyleSheets/ReverseColor.nb"
}


Then we can try batching this:

FrontEndExecute@
{
FrontEndFindFileOnPath["Default.nb", "StyleSheetPath"],
FrontEndFindFileOnPath["ReverseColor.nb", "StyleSheetPath"]
}

\$Failed


And... it fails. So how can I get both results out in a batched call? Can I?

### What have I tried

FrontEndExecute is a just an alias for MathLinkCallFrontEnd. But it passes NoResult as the second argument.

I tried to figure out if there were other possible options for that:

hasMCall =
Quiet@
ToExpression[
DeleteDuplicates@Join[Names["**"], Names["***"]],
StandardForm,
Function[Null,
If[
MemberQ[DownValues[#],
MathLinkCallFrontEnd | MathLinkCallFrontEndHeld,
∞,
],
#,
Nothing
],
HoldFirst
]
];

Cases[DownValues /@ hasMCall,
c : (MathLinkCallFrontEnd | MathLinkCallFrontEndHeld)[__, b_] :> b,
∞
] // DeleteDuplicates

{FENoResult, SystemFEDumpNoResult}


But I couldn't find any in the DownValues. Beyond this, I've tried a bunch of different second arguments having to do with "Result" or whatever. They've all failed.

### Batched FrontEndExecute

If you're not used to batching calls to the FE, we do it because often it's more efficient than passing commands one by one. Basically we just push all the calls in at once. As a first example, let's consider making a bunch of useless cells. We could generally write this like so:

makeBunchOfCellsUnbatched[] :=
(
FrontEndExecute@
SelectionMove[EvaluationNotebook[], After, Notebook,
AutoScroll -> False
];
Do[
NotebookWrite[EvaluationNotebook[],
Cell["", "MyStyle"],
AutoScroll -> False
],
200
];
SelectionMove[EvaluationCell[], After, Cell];
FrontEndExecute@
FrontEndNotebookResumeScreenUpdates[EvaluationNotebook[]]
)


But we could write this with a batched call like so:

makeBunchOfCells[] :=
FrontEndExecute@
Flatten@{
FrontEndEvaluationNotebook[]],
FrontEndSelectionMove[FrontEndEvaluationNotebook[], After,
Notebook,
AutoScroll -> False
],
Table[
FrontEndNotebookWrite[FrontEndEvaluationNotebook[],
Cell["", "MyStyle"],
AutoScroll -> False
],
200
],
FrontEndSelectionMove[FrontEndEvaluationCell[], After, Cell],
FrontEndNotebookResumeScreenUpdates[FrontEndEvaluationNotebook[]]
}


And when we compare the runtimes:

makeBunchOfCellsUnbatched[] // AbsoluteTiming // First

11.5724

makeBunchOfCells[] // AbsoluteTiming // First

4.05879


We see this is much more efficient.

We can also consider a batched SetOptions:

setCellOptions[
moveAfter : True | False : True,
ops : OptionsPattern[]
] :=
InternalWithLocalSettings[
Null,
FrontEndExecute@
Flatten@{
FrontEndNotebookSuspendScreenUpdates[
FrontEndEvaluationNotebook[]],
Map[
FrontEndSetOptions[#,
FilterRules[{ops},
Except[Alternatives @@ Keys@Options@Cells]]
] &,
Cells[FrontEndEvaluationNotebook[],
Sequence @@ FilterRules[{ops}, Options@Cells]
]
],
If[moveAfter,
FrontEndSelectionMove[FrontEndEvaluationCell[], After,
Cell],
Nothing
]
},
FrontEndExecute@
FrontEndNotebookResumeScreenUpdates[FrontEndEvaluationNotebook[]]
];

setCellOptionsUnbatched[
moveAfter : True | False : True,
ops : OptionsPattern[]
] :=
InternalWithLocalSettings[
Null,
FrontEndExecute@
FrontEndEvaluationNotebook[]];
FrontEndExecute /@
Flatten@{
Map[
FrontEndSetOptions[#,
FilterRules[{ops},
Except[Alternatives @@ Keys@Options@Cells]]] &,
Cells[FrontEndEvaluationNotebook[],
Sequence @@ FilterRules[{ops}, Options@Cells]
]
],
If[moveAfter,
FrontEndSelectionMove[FrontEndEvaluationCell[], After,
Cell],
Nothing
]
},
FrontEndExecute@
FrontEndNotebookResumeScreenUpdates[FrontEndEvaluationNotebook[]]
];


After making a bunch of cells we get results like this:

setCellOptionsUnbatched[
Background -> RandomColor[],
CellStyle -> "MyStyle"
] // RepeatedTiming // First

1.834

setCellOptions[
Background -> RandomColor[],
CellStyle -> "MyStyle"
] // RepeatedTiming // First

0.3


And so if you're making a lot of use of the FE it's generally best to Sow your calls or something and then push them all in at once. It is for this reason that I want to be able to get a batched result, too.

• might be a naive comment, but SetAttributes[FrontEndExecute, Listable] then FrontEndExecute@ { ..., ... } works. – egwene sedai Jan 26 '18 at 22:13
• Yes but that doesn’t batch. That’s really what I want. Otherwise I could just Map it. – b3m2a1 Jan 26 '18 at 22:24

It turns out that for some call types this is possible by simply sticking a FrontEndValue in front. CurrentValue is a case where that works and here's an example of that:

enb = EvaluationNotebook[];

MathLinkCallFrontEnd@FrontEndValue@
Thread[FrontEndCurrentValue[enb, {FontColor, FontSize, FontWeight}]] // RepeatedTiming

{0.0004, {Automatic, 12, "Plain"}}

CurrentValue[enb, #] & /@ {FontColor, FontSize, FontWeight} // RepeatedTiming

{0.00088, {Automatic, 12, "Plain"}}


You can also set CurrentValues in batch like:

ec = EvaluationCell[];
MathLinkCallFrontEnd@FrontEndSetValue[
KeyValueMap[
FEPrivateSet[FrontEndCurrentValue[ec, #], #2] &,
<|
FontColor -> Red,
FontSize -> 12,
FontWeight -> "Thin"
|>
]
]


And revert them like:

MathLinkCallFrontEnd@FrontEndSetValue[
KeyValueMap[
FEPrivateSet[FrontEndCurrentValue[ec, #], #2] &,
<|
FontColor -> Inherited,
FontSize -> Inherited,
FontWeight -> Inherited
|>
]
]


Unfortunately this doesn't work for some tokens:

MathLinkCallFrontEnd@
FrontEndValue@{
FrontEndFindFileOnPath["Default.nb", "StyleSheetPath"],
FrontEndFindFileOnPath["Default.nb", "StyleSheetPath"]
}

{FrontEndFindFileOnPath["Default.nb", "StyleSheetPath"], FrontEndFindFileOnPath["Default.nb", "StyleSheetPath"]}
`

Still not clear how to make this work