[texworks] SCRIPTING: readFile() xxxGlobal() and NewFile hook
st.loeffler at gmail.com
Sun Dec 19 20:36:56 CET 2010
On 2010-12-19 15:00, Jonathan Kew wrote:
> On 19 Dec 2010, at 09:34, Paul A Norman wrote
>> First, TW.readFile() is working really nicely so far thank you. Yet to
>> try for paths outside document and script's directory trees. Working
>> with default settings first.
>> Returning an object for inspection from the readFile() call, works as
>> a nice clean design solution when all things are considered - well
>> done I reckon Stefan!
>> Haven't worked with writeFile() yet.
>> And TW.app.xxxGlobal("xXx") are all working nicely! Haven't tried
>> TW.script versions yet.
Thanks for letting us know - it sounds good.
>> In my preferences, TeXworks is set to open a new file when the
>> application starts.
>> This script (below) however does not execute when the application
>> starts. Is that perhaps to do with the way the application
>> initialises, and doesn't open the NewFile the same way as the menu
>> command would do it later, if used by the User?
> I'd consider that a bug; a NewFile hook ought to be called for all NewFile operations, including one that happens automatically at launch.
> Without actually going through the code, I'd guess this is because the scripts don't get loaded until the first scriptable window (source or PDF document) is created. To fix this, we should probably set up the script manager from the main() function, before calling app.exec(). Or something like that.
That was my first guess as well, but it turned out to be wrong. The
script manager is initialized from TWApp::init, which is invoked at the
beginning of main() when the TWApp object is created.
Instead, there are two varieties of newFile. Those in TeXDocument (which
correspond to what happens when you click, e.g., on the "New" icon), and
those in TWApp (which are essentially used by global menus on the Mac, I
think, but also for the launch action, as there is no *Document object
present then). I've added the hooks to the TWApp variant in r725, so
they should be run now.
Eventually, I think we should consolidate the new functions (and
probably others), but that should have time until after 0.4.
>> Was planning to rely on this for initialisation purposes.
> For general-purpose use, that doesn't seem like a good choice (even if it worked) because it depends on the user's launch preference (and manner of starting the application); it won't fire if the user directly opens a document, for example.
> An "ApplicationLaunched" hook would be a better approach. I don't think that should be hard to add. It could be called at the beginning of TWApp::launchAction(). Note that if filenames are specified on the command line, these would be opened *before* the Launched hook is called, so there may already be document windows at this time.
OK, I'll look into that. In principle, it should be easy. However, there
may be some peculiarities (possibly no target object yet, for example).
It might require moving some stuff around (essentially, move runHooks
from TWScriptable to TWScriptManager).
More information about the texworks