Can one effectively edit a Front End Resource that is already loaded?

A number of special definitions are loaded from .tr files on Front End initialization and are accessible via FrontEndResource. These are used among other things for various menus and toolsbars, e.g.:

FrontEndResource["FEBitmaps", "HomeIconHot"] // ToExpression

FrontEndResource["FEExpressions", "HelpViewerToolbar"] // CellPrint

FrontEndResource["ColorSchemeSelector"] // CellPrint


The .tr resource files can be edited to change these as I proposed for Can you make the new "Choose color scheme" helper larger? and Is it possible to insert new colour schemes into ColorData? It would be much nicer to be able to edit these after they are loaded from within Mathematica. One can Unprotect FrontEndResource and modify the definition but this does not actually change what is displayed in the Front End.

As a simple example I would like to make the Color Scheme Selector a little wider so that it does not have a pointless horizontal scroll bar on my system. I attempt:

Unprotect[FrontEndResource];

With[{FER := FrontEndResource["ColorSchemeSelector"]},
FER = FER /. {180, Automatic} -> {195, Automatic};
]


The manually called resource is now wider:

FrontEndResource["ColorSchemeSelector"] // CellPrint


However the auto-complete popup for Color is not:

On examination this is not too surprising since the actual definition of FrontEndResource is:

FrontEndResource[name_] :=
MathLinkCallFrontEnd[FrontEndValue[FEPrivateFrontEndResource[name]]];

FrontEndResource[name_, sub_] :=
MathLinkCallFrontEnd[FrontEndValue[FEPrivateFrontEndResource[name, sub]]];


It seems logical that the Front End would make this lower level call directly, bypassing SystemFrontEndResource. My problem is that I don't know how to redefine this FrontEndValue. Names finds a FrontEndSetValue which sounds promising but I haven't figured out how to use it.

How can I change this Front End Resource in a way that affects all appearances of the menu?

• Closely related: How to suppress Tooltip in built-in templates?, unfortunately I've failed to find equivalent of FrontEndExecute@FrontEndFlushTextResourceCaches[] for expressions' caches. – Kuba Jul 19 '16 at 21:01
• @Kuba Thanks for reminding me of that question, or more specifically your answer; I had forgotten. Even with that I see that you had to write to a .tr file rather than directly editing things in memory. Perhaps these definitions are simply read only but I am hoping for authoritative confirmation of that before coming to a conclusion. – Mr.Wizard Jul 20 '16 at 7:13
• @Mr.Wizard I've only ever seen it used as FrontEndSetValue[FEPrivateSet[CurrentValue[spec],val]]. That works fine, but doesn't really help us here. Once I figure out how to make the packets work I'll post a solution editing at the DownValues level on FrontEndValue, but I have no experience with these WTSP packets so it's all very confusing. – b3m2a1 May 11 '17 at 21:50
• @Mr.Wizard I get around that by prepending my temporary path to {PrivatePaths, "TextResources"} then by calling FrontEndFlushTextResourceCaches[]. It reloads everything as one would hope--as long as one uses a resource name already known to the system. – b3m2a1 May 11 '17 at 21:55
• @Mr.Wizard one caveat is that I haven't been able to get successful reloading with KeyEventTranslations.tr although I'm gonna keep playing with it. Reloading the menus works fine if you call ResetMenusPacket[{Automatic,Automatic}] (found in the PacletManager code) so I'm working on editing the MenuSetup.tr right now. – b3m2a1 May 11 '17 at 21:57

This is more comment than answer as you want in-memory modification, but it is possible to force the text resources to be reloaded. You do this with, e.g.,

FrontEndExecute@
FrontEndFlushTextResourceCaches[]


So you can make a new text resource in $UserBaseDirectory and have that reload. For example, let's make a new TR for the file headers: $headersTR =
FrontEndExecute@
FrontEndFindFileOnPath[
"PrivatePathsTextResources"
];
$newHeadersTR = StringReplace[$headersTR,
$InstallationDirectory ->$UserBaseDirectory
];
CreateDirectory[DirectoryName@$newHeadersTR, CreateIntermediateDirectories -> True]; Export[$newHeadersTR,
"
(*?__?*)

@|
",
"Text"
];


FrontEndExecute@FrontEndFlushTextResourceCaches[]

"(*?__?*)
"


And that'll change the package header in all your auto-generated .m files.

Note that this isn't in-memory modification, but I'm not wholly these are always stored in memory, as sometimes deleting a file causes the FE to spit an error when you try to load a resource from that file.

On the other hand, something definitely is stored in memory. E.g., try deleting that new package header TR and loading the header again:

DeleteFile[$newHeadersTR] FrontEndResourceString["PackageHeader"] "(*?__?*) "  So that's there until the cache is flushed. Update: Here's a way to get it to spit an error: Export[$newHeadersTR,
"
(*?__?*)

@|
",
"Text"
];
FrontEndExecute@FrontEndFlushTextResourceCaches[]
DeleteFile[$newHeadersTR]; FrontEndResourceString["PackageHeader"]$Failed


Clearly the FlushTextResourceCaches found that "PackageHeader" was in $newHeadersTR, but didn't load it. My guess would be that it got added to "ResourceMap.pbf" (see below) or something, but the load doesn't get performed until later. Cache files: Just as one last note on these caches, there're these files: FileNames["ResourceMap.pbf", FileNameJoin@{$UserBaseDirectory, "FrontEnd"},
Infinity
]


which are what actually get overridden when you call:

FrontEndExecute@
FrontEndFlushTextResourceCaches[]


And they're some scrambled binary format, but if you look at them in plain-text view you see the file names where the resources are stored, e.g. I found:

"/Applications/Mathematica.app/Contents/AddOns/Applications/NotebookTemplating/FrontEnd/TextResources/NotebookTemplatingResources.tr"


in it.

So presumably this file gets used as the "ResourceMap"` to the front-end to tell it from where to load resources it doesn't have in memory. That may also explain why deleting a cached resource file sometimes throws errors.

• Possibly the only answer possible at this time. Thanks for taking the time to write it up in detail. – Mr.Wizard Aug 18 '17 at 19:42