[texworks] Scripts: Globals
Stefan Löffler
st.loeffler at gmail.com
Mon Nov 1 16:44:04 CET 2010
Howdy,
Am 2010-10-31 11:44, schrieb Paul A Norman:
> I am looking for some help on Globals please.
>
> 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.
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.
> And is a QTScript object able to be passed as a QVariant?
I just tested your code fragment below and yes, it seems so.
> I.e. how would I get this to work please?
>
> Script One:--
>
> var $MyObject = {message : "Hello"};
>
> TW.?app/?script.setGlobal("holdObject", $MyObject);
>
> Script Two :--
>
> var MyNewCopy = TW.?app/?script.getGlobal("holdObject");
>
> TW.information(null, "Message", MyNewCopy.message);
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 ;)).
HTH
Stefan
More information about the texworks
mailing list