[texworks] SCRIPTING: readFile() xxxGlobal() and NewFile hook
jfkthame at googlemail.com
Sun Dec 19 15:00:06 CET 2010
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.
> 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.
> 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.
More information about the texworks