Keep variable between two script calls

How do I save a varibale between two independent script calls? For example I would like to save some custom folder and option and re-use it for the next time as long as DOpus keeps running (it does not matter if I need to reselect it after reboot for example).

If it's something that is just to cache things for speed and can be re-created if lost, using a global variable is the simplest way. The script's global variables (and OnInit function etc.) will be created if it is called and doesn't already exist, and then destroyed if it hasn't been called for a certain amount of time. This can be good for things like script columns which are called repeatedly when you refresh a folder, and need to be as fast as possible, but then are not called at all for a while.

If you need the data to last longer than that, storing it in a script variable is the way to go: Script.var. When doing that you can specify the lifetime, so it can persist across reboots if you desire, or only be there until the program exits.

You can also store variables with the Lister and with Folder Tabs, if it makes more sense to tie them and their lifetime to a particular window or folder-view.

(in addition to what Leo said) There's also global scope - pick the one that suits your application best. The Vars docs describes the different scopes available.

1 Like

Let's say I would like to keep script variable "KeepMe" between two script calls. And here is simple example as to how I understand it should work. However, that gives me the error: variable is undefined: 'Script'. It doesn't help if I use ScriptInitData, but it works with DOpus object instead.

What is wrong with this?

Function OnClick(clickData)
 If ScriptInitData.vars("KeepMe").Exists Then
  sKeepMe = ScriptInitData.vars("KeepMe").Get
  ScriptInitData.vars("KeepMe").Set = myDefaultValue
 End If
End Function

Of course I would like to use the variable as local as possible.

Have a look at this post

1 Like

Only script add-ins have a Script object (ScriptInitData).

Thanks guys. Then DOpus global it is. :slight_smile:

@leo, @jon, @aussieboykie - with your help here is my very first DOpus script button with this final touch, I would appreciate your optional feedback. Thanks in advance. :slight_smile:

Link: Comic Book CBx-To-CBx Convert & Resize v1.40