Command: SetVar (set, toggle, delete vars from anywhere)

SetVar - a script command to set, toggle or delete DO variables from anywhere (buttons, scripts or external applications with dopusrt.exe). It does update buttons as well.

This is like an extended version of the built-in @set command or scripting Vars objects.

Description:
You can use this command to handle global variables in buttons more easily.
Another usecase is to set/delete/toggle variables from external applications, so you can trigger button states and icons or script behaviour from outside of DO.

Features & Params:
SET/S,DELETE/S,TOGGLE/S,EXISTS/S,NAME/K,PERSIST/S,DEPERSIST/S,NOCREATE/S,NOSET/S,TOGGLEHIGH/K,TOGGLELOW/K,TOGGLEDEL/S,GLOBAL/S,LISTER/S,SOURCE/S,DEST/S,VALUE/R,

  • SET, set a variable (default operation)

  • DELETE, delete a variable

  • TOGGLE, toggle a variables value

  • EXISTS, check variables existence

  • NAME, name of the variable

  • VALUE, value to set (make sure its always the last parameter if used)

  • PERSIST, switch variable to persistent

  • NOPERSIST, switch variable to non-persistent

  • NOCREATE, do not create a variable if it does not yet exist when setting or toggling

  • NOSET, do not set a value (can be used in conjcuntion with DE/PERSIST e.g.)

  • TOGGLEHIGH, the "high" value when toggling (default = 1)

  • TOGGLELOW, the "low" value when toggling (default = 0)

  • TOGGLEDEL, deletes the variable for "low" toggle state

  • GLOBAL (use global scope, default)

  • LISTER (use lister scope)

  • SOURCE (use source tab scope)

  • DEST (use destination tab scope)

Usage:
To set a variable named "MyVar" to "MyValue":

SetVar NAME=MyVar VALUE=MyValue

To set a lister scoped variable named "MyVar" to "MyValue":

SetVar NAME=MyVar LISTER VALUE=MyValue

To delete a variable named "MyVar" to "MyValue":

SetVar NAME=MyVar DELETE

To toggle a variable named "MyToggle" between the value "0" and "1":

SetVar NAME=MyToggle TOGGLE

To toggle a variable named "MyToggle" between the high-value "exists" and always delete the variable if toggle state is low:

SetVar NAME=MyToggle TOGGLE TOGGLEDEL TOGGLEHIGH=exists

To toggle a variable named "MyToggle" between the value "low" and "high":

SetVar NAME=MyToggle TOGGLE TOGGLELOW=low TOGGLEHIGH=high

To enable persistence of a variable named "MyVar", without changing its value:

SetVar NAME=MyToggle PERISIST NOSET

To test for existence of a variable named "MyVar" (returns 0 for "exists" and 1 for "not found"):
(A button script will be stopped if the command is used with EXISTS and the variable does not exist.)

SetVar NAME=MyToggle EXISTS
//works in scripts as well (while not being very handy)
DOpus.Output("Exists: " + DOpus.Create.Command.RunCommand("SetVar NAME=MyVar EXISTS") );

To set a variable named "MyVar" from outside of DO:

../dopus/dopusrt.exe /cmd SetVar NAME=MyVar VALUE=TheValue

Some more sensible combinations are possible, try them for yourself.. o)
In case you want to "look" at what happens or just learn how to use it, enable the debug output from the script config section.



Installation:
To install the command, download the *.js.txt file below and drag it to Preferences / Toolbars / Scripts.
After that the command shall be ready to use in buttons, scripts and externally from dopusrt.exe of course.
Because the scripts filename changed for v0.2:
Remove the former script Command.Generic_SetVariable.js from your /dopusdata/script addins folder!

cya,
tbone

Download:

This looks very useful. However, as I'm not sure that I understand everything, let me present my use case to see if this script will work. I have lots of images that I'm working to organize. Many are in sets of 10 or 20 images (or more). However, often the names aren't very helpful. They might be Mountains01.jpg, Mountains02.jpg, and so forth. Chances are, I've already got an image named Mountains01.jpg and just renaming the new files Mountains01 (1).jpg will cause the set to be broken up. So, I've taken to using a little scrap of paper where I keep track of the current set number for my mountain and ocean (and so forth) images. So I might rename those files to Mountains - Set 07 - 01.jpg. It's keeping track of the set number that I've wanted to use variables for. If I could have a button that would insert the right set number (based on a variable) and then increment that set number, it would make my life much easier. Can this button do that (or at least part of that)? Also, what happens when I close DO? Do the variables persist even when DO is closed?

Thanks.

Hi there! o) Well, I guess no. For what you describe, the Setvar command is unlikely to be of help. But what about this: Renaming - Multi File Renaming Questions
I'd like you to try that and if that get's in the direction of what you're looking for, a persistent setnumber could easily be added. Even if that is way off from your goal, please open a new thread in the support section, so we can get into some more details there, ok? o) Thx!

OK. Thanks. I'll take a look at that script and see if I can make heads or tails of it. Hopefully, one of these weekends, the wife and kids will leave me alone long enough for me to try to play around with some scripting. Sigh.

Hi There, excuse my english as I speak french.
I have a question for you as regard your script. I am not really an expert and I believe your script might help. I have a button with a few global variable seted-up. Part of my button I am calling a User defined command. Very simple one. But I wish I could assign a value to a global variable that I already @set in the main button. But I don't thing Dopus allow that after testing it. So what I am looking for is a toggle variable that would be off in the main button and would be turned on if condition is met in the calling User defined function.
Let say in an imaginary example that in the main button I call the User defined command called Is_File_exist (one argument)
The main button call would be
Is_File_exist my_fat_cat.jpg
What I would like to do is declaring a global variable in my main button and be able to set-it up in the User define command if file exist,
so when I return to the main button (function) I can check the variable with @ifset: and if it set I know file exist and than re-set the global variable to nothing before calling again the usedefined command. Is it possible? I hope I was clear enough.
Thanks Denistr2379

What you are about to do, will not work with SetVar unfortunately.

If you have a global variable "MyVar" with some value and your button calls a scriptcommand, where that variable is deleted/unset/changed, the button and any @set statement will not know about it - unless you run the button again. I guess that's because button code is evaluated before it even starts to run, so you cannot make it react to variables you change at runtime with a scriptcommand. Maybe you can get it to work by using the @set modifier to set variable.

Basically you're trying to get something to work like a return value (if I understood correctly), which is a totally sane idea - but currently this is not well supported for commands of any type - which is too bad, I agree! o)

Thanks for your prompt answer Tbone.
You understood me well (despite my poor English). It is really ashame that we cannot get any return vaslue from any function that can be tested on return unless using OPUS Objet like Dopus.vars I believe. I mentioned that to Leo from OPus and his answer was quite direct, All internal command including modifiers commands are very basic and will stay that way because the intention is directed to use Script. But as I mentioned in a thread, I feel this goes beyond the purpose of having a powerful tool to work with it (OPUS program) in a business environment if we have to re-write everything in terms of script to make certain things (function) to work well for certain task you wish to accomplish. Well this is my opinion and still love DOPUS anyway.

Glad to talk to you from Montreal Canada.

Updated to v0.2

  • scope support added
  • command returncodes fixed

Because the scripts filename changed:
Remove the former script Command.Generic_SetVariable.js from your /dopusdata/script addins folder!

@denistr
I'm with you, scripting can be very verbose, even for smaller tasks.
The existing button macro language with all its shortcuts is still nice to have, I also wouldn't mind if it gets pushed a bit. o)
The return value issue applies to script commands as well btw., you cannot use global variables as a replacement for return values. o(

Update regarding return values from script commands:
You can set variables in Tab scope to achieve this. This works in buttons using script commands quite well. Not sure why this solution did not make it into this thread yet, but since I was coming here to read my own documentation on SetVar, I thought it might be worth adding. o)

Is it really 8 years later? Man, time flies!

1 Like