How to access a button variable from a script

Apologies for this basic question, but I can't work it out. Suppose that I have an ordinary DOpus button:

@Set MyVarA=koala @Set MyVarB=goanna @Set MyVarC=kangaroo MyAddinScriptFunctionwhere MyAddinScriptFunction is a function within a script addin.

How do I call up the three variables MyVarA, MyVarB and MyVarC and their values from within the script function? I only know how to do this if the variables have scope glob or lst.

I don't think those command-local variables would be visible to the separate script command.

You could pass them as arguments to the command instead.

I guess this should work:

[code]var MyVarA="koala";
var MyVarB="goanna";

function OnClick(data){
var cmd = data.func.command;
data.func.sourcetab.vars.set("MyVarA", MyVarA);
data.func.sourcetab.vars.set("MyVarB", MyVarB);
cmd.RunCommand('MyAddinScriptFunction');
}[/code]
Within your script addin you can access the variables via the sourcetab.vars collection.
It works the other way round at least, so I'd guess this would do as well.

Regarding you initial attempt to access the tab scoped variables set by "@set", did you looked into the sourcetab.vars collection from the script command to see if they show up? If not, this still may be worth a try.

Another option is to pass parameters to your script command. For example, when triggered by this button..

[code]var MyVarA="koala";
var MyVarB="goanna";

function OnClick(data){
var cmd = data.func.command;
data.func.sourcetab.vars.set("MyVarA", MyVarA);
data.func.sourcetab.vars.set("MyVarB", MyVarB);
cmd.RunCommand('testbed wallaby "duck billed platypus" dingo');
}[/code]
..the attached testbed script command will output this:

testbed: version 1.0 starting.. testbed: MyVarA = koala testbed: MyVarB = goanna testbed: parm 1 = wallaby testbed: parm 2 = duck billed platypus testbed: parm 3 = dingo
Regards, AB

testbed.js.txt (3.08 KB)

Thanks leo, tbone and aussieboykie. I should have added that I know that I can do it by parameters, but that I want to do it using variables set by @Set in an ordinary DOpus button because of the mess that you get into with multiple parameters when the value of some variables contains spaces. To be very specific, here is my 'Standard Function' DOpus button:

@Set Glob:MyGlobalVar=Wallaby @Set Lst:MyListerVar=Kangaroo @Set MyTabVar=Dingo TestSnippetA where TestSnippetA is my addin function given by

[code]function onTestSnippetA (scriptCommandData) {
var oEnumSourceTabVars = new Enumerator (scriptCommandData.func.sourcetab.vars)
oEnumSourceTabVars.moveFirst ()
oTabVars = oEnumSourceTabVars.item ()
DOpus.Output ("oTabVars = " + oTabVars) // First four lines test the collection data.func.sourcetab.vars

var sMyGlobalVar = DOpus.vars.get ("MyGlobalVar") // This works
DOpus.Output ("MyGlobalVar = " + sMyGlobalVar)
var sMyListerVar = scriptCommandData.func.sourcetab.lister.vars.get ("MyListerVar") // This works
DOpus.Output ("MyListerVar = " + sMyListerVar)
var sMyTabVar = scriptCommandData.func.sourcetab.vars.get ("MyTabVar") // This does not work!!!
DOpus.Output ("MyTabVar = " + sMyTabVar)
}[/code]
First four lines: You can see that on tbone's advice I am testing the collection data.func.sourcetab.vars. The output there is oTabVars = undefined, presumably indicating that the collection is empty.

Last six lines: The output here is

MyGlobalVar = Wallaby MyListerVar = Kangaroo UniversalStemRenamer: Error at line 33, position 3 UniversalStemRenamer: Invalid procedure call or argument (0x800a0005) indicating that I am correctly referencing the global and lister variables from within the script, but I am doing something wrong when trying to get at the third variable.

QUESTION: What is the correct code in the second-last line?

Of course I can workaround by using @Set with global or lister variables, but surely there is correct way to get at a variable that is neither global nor lister.

Mhh, interesting.
If "@set" works with lister and global scope as you described, I agree and it sounds like it should work with tabbed scoped vars as well. Did you try with "src:" prefix? I can imagine that if no prefix is used, the "oldschool" set-approach is used, which works on find/replace in the button commands (I think) - Leo and Jon, correct me if I'm wrong.

Success! You've cracked it, tbone. Thanks very much. For the record, I changed the 'Standard Function' DOpus button to:

@Set Glob:MyGlobalVar=Wallaby @Set Lst:MyListerVar=Kangaroo @Set Src:MyTabVar=Dingo TestSnippetA that is, I inserted the scope Src: into the third line. The output is now what I want:

oTabVars = MyTabVar MyGlobalVar = Wallaby MyListerVar = Kangaroo MyTabVar = Dingo - - - - - - - -

Conversely, I can set a variable within the script addin TestSnippetA by

var sMyTabVar = scriptCommandData.func.sourcetab.vars.set ("MyScriptVar", "Shark") and then I can access it lower down in the 'Standard Function' DOpus button by, for example,

{DlgString|Here is {$Src:MyScriptVar}} which yields the message Here is Shark above the input box.


A FINAL POINT: I presume, then, that a variable that is @set without any mention of scope, for example by @Set MyVar=Possum, cannot be accessed from within the script addin that occurs lower down in the button. Leo, it would be good if that could be confirmed.

"I don't think those command-local variables would be visible to the separate script command." :slight_smile:

Thanks, leo. That clears everything up. I don't want the variables hanging around afterwards, so I will use Src:MyVar and turn each variable off again within the script.

Why not pass arguments to the script? That seems to be what the variables really are.

No worries any more — everything is working now exactly as I want, deleting variables within the script as they are read. I wanted to avoid arguments for two reasons. First, some of the arguments that I want to use are DOpus commands with spaces and quotation marks around paths, and I just couldn't bear the fussing about making sure that arguments terminated where intended.

Secondly, the number of arguments is variable, and that is tricky to handle. Of course it may be possible to pass all the arguments as a single mega-argument which is then split up again into its components within the script, but it would get fiddly.

By the way, the initial purpose of doing all this was to cut down the number of buttons, which are multiplying like rabbits in my main menu, by making it possible for a normal DOpus button to call up a dialogue with a variable message, and a variable number of buttons, each with an associated task requiring a variable number of commands. But there are many other similar purposes — the very simple point is to use a fixed script with many buttons.

As to 'what the variables really are', most mathematicians will just smile at such an idea.