[texworks] SCRIPTING: Happy New Year to ALL! - openFileFromScript

Paul A Norman paul.a.norman at gmail.com
Tue Jan 4 07:22:42 CET 2011

On 4 January 2011 05:15, Stefan Löffler <st.loeffler at gmail.com> wrote:
> On 2011-01-01 06:41, Paul A Norman wrote:
>> Happy New Year to ALL!
> Yeah, happy new year! Hopefully, we'll see a Tw release this year :P.
>> Can't yet test openFileFromScript() (thanks for putting that in Stefan)
>> Debugger says ...
>> "Uncaught exception at
>> D:/LaTeXPortable/LatexUtils/TeXworks/TeXworks/config/scripts/Scripting
>> - Experiments/tw_OBJECT.js:19: TypeError: cannot call
>> openFileFromScript(): argument 2 has unknown type `TWScriptAPI*'
>> (register the type with qScriptRegisterMetaType())
>> 19    var   ret =  TW.app.openFileFromScript('c:/hold/MyFile.txt', null,
>> -1, true);"
>> Tried second argument with various, however can see that it is
>> complaining that something is not set/registered in Scripting engine?
>> Would this argument effectively disappear from User's scope if already
>> registered? and then the following be renumbered back one?
> No. You should pass TW to that parameter. The reason for this is that in
> the TW.app context, we have no direct access to the calling script, and
> therefore can't perform any security checks. For that we need TW (or,
> more precisely, TW.script, at the moment; but because this prerequisite
> might change in the future, we stick to TW for the moment).
> So you should put something like this in your script:
> var ret =  TW.app.openFileFromScript('c:/hold/MyFile.txt', TW, -1, true);
> I tested (something like) this, and it worked for me.
> You are right, though, that passing null shouldn't crash the script. So
> I modified the code in r727 so that arbitrary objects can be passed (but
> only TW makes sense).

I should have dug more deeply, I mistook the error message  ...

  " argument 2 has unknown type `TWScriptAPI*' "

... to mean that the actual call was looking in .cpp for an unkown
type not established in Qt yet.

Duh - TW ok!   and TW.script - well I might not have got there :)

>> 3rd argument desired cursor position?
> Yes
>> 4th argument whether to prompt user if file read permission not set to
>> True in Preferences?
> Yes. More generally, this flag determines if the user should be asked if
> the file should be opened if read permissions are insufficient, or if
> the function should just fail silently without bothering the user (the
> default).

Most excellent!

The idea behind this is twofold:
>  * You'll often want to open a single file for the user to inspect
> (otherwise you could just use readFile). In this case, there is no
> reason to force the user to enable global reading, but instead we give
> him the chance to decide on a per-file basis if he wants to grant access
> to that file to the script. For this, set the askUser flag to true.
>  * If, for some reason, you want to open a (larger) number of (related)
> files, you don't want to bother the user each time asking for
> permission. Instead, the function should fail silently, the script
> should catch that, and terminate with a message telling the user how to
> enable global reading. So by default, this function behaves exactly like
> readFile.

I'll write it up later.

> Stefan

More information about the texworks mailing list