[texworks] Scripts: Globals

Paul A Norman paul.a.norman at gmail.com
Tue Nov 2 01:24:39 CET 2010


Thanks for that Stefan,

>> What is the difference between
>>
>>     TW.app.setGlobal(QString,QVariant);
>>
>> and
>>
>>     TW.script.setGlobal(QString,QVariant);
>
> Practically, there is not very much difference at the moment -
> conceptionaly, there is, though.
> With TW.script...., things are stored with the script. I.e., the data
> only exist as long as the script does. If the script is reloaded (e.g.,
> because it was moved, or the file changes, ...), the data is thrown
> away. Note that "reload script list" _should_ be intelligent enough to
> reload scripts only when necessary.

What are the advantages of storing data globally in TW.script.xxxGlobal
If a script's own global will do the trick anyway(?) - is this
conceived for some future thing when it will be possible to run
scripts con-currently and use this mechanism to pass objects between
them while active?

> Tw.app.... on the other hand stores data in the application data itself.
> As such, all scripts can access the data. Therefore, extra care is
> necessary to use unique names (e.g., prefix them with the name of your
> script or something). ATM, these TW.app globals are not stored on the
> disk, so the data is not persistent (maybe this will change sometime in
> the future?).
>
> As such, it is usually better to store data in TW.script unless you need
> to access it from another script.

Same question as above - why do it if the running Script has access to
its own variables any way?

>
>> And is a QTScript object able to be passed as a QVariant?
>
> I just tested your code fragment below and yes, it seems so.

Ok, I asked because it was not working for me with more complex
objects including functions, I'll revisit it and check my work again
carefully.

Is there any memory limitation to TW.app.xxxGlobal object sizes?

Is my use of a $ in the global name a problem for TW.app.xxxGlobal("$dahdah") ?

My hope is to use the hook NewFile which is called when Tw opens ( -
and by default makes a new file), to test for gloabls and if not
present TW.app.hasGlobal($dahdah)  then pre-load common objects with
functions and data  which I want following scripts to use, and so
TW.app.setGlobal("$dahdah")  , so that following scripts need only
TW.app.getGlobal("$dahdah") and not have to load and create them again
and again again when instantiated themselves (big time saving
overall).

>From your knowledge of the overall framework, could a whole complex
(memory-wise -> large) dialog be safely/**reliably** stored in this
way?

>
> As mentioned above, to pass data between two scripts, you need to set it
> in TW.app. Just make sure the name is unique (e.g., "holdObject" would
> not be a safe choice ;)).

absolutely - thanks.

>
> HTH
> Stefan
>



More information about the texworks mailing list