JMP Add-in — Isolating multiple instances with namespacesHi JMP Community,
I'm building a JMP add-in that creates windows, stores global variables, and calls functions. Everything works fine with a single instance — but when the user launches the add-in twice, the two instances share the same global scope and variables start conflicting (one window overwrites the other's state).
I initalized my script with Names default to here(1), I have even created my own new space (ns = newspace() ) and put every variables in it but I still do have the conflict. I know about window namespace but I want also to avoid conflict on the variables outside the window
// Launch this script twice — second run breaks the first window
strLabel = "Instance A";
counter = 0;
win = New Window( "Test " || strLabel,
btn = Button Box( "Click me",
counter++;
btn << Set button name( strLabel || " clicked " || Char( counter ) || "x" );
)
);Automation and ScriptingRe: JMP Add-in — Isolating multiple instances with namespacesIf every window is independent then I recommend using the window namespace, designed just for this:
Names default to here(1):
// Functions outside the window are still accessible
rand = function({}, floor(random uniform(10)));
win = New Window("Values",
show toolbars(0), show menu(0),
// You can put statements above display elements
window:randomvalue = rand();
v list box(
text box("Enter a value for this box:"),
// All references to display elements should be in the window namespace
window:teb = text edit box(""),
// Examples reading and writing values.
button box("Update random value", window:randomvalue = rand() ),
button box("Get values from this window",
window:tb << Set Text(
"Random value: " || char(window:randomvalue) ||
"\!NEntered Value: " || (window:teb << Get Text)
)
),
window:tb = text box("")
)
);
// You can get to this from outside the window, but make sure this namespace is protected too - for example from inside another window.
show(win:randomvalue, win:teb << Get Text);Re: JMP Add-in — Isolating multiple instances with namespacesEasiest option is to use window namespace. I think the second easiest (if we ignore context box) would be to use named namespace IF you can name them. Next would be to utilize one named namespace and collect the values into lists / associative arrays and access those. Anonymous namespaces should also work but these will require some extra work to access correct namespace within the window.
Re: JMP Add-in — Isolating multiple instances with namespacesHello thank you for your anwser. I will implement window namespace but the part where I am extracting variable outside the window is a bit difficult as the window name is the same if I run multiple instance
Re: JMP Add-in — Isolating multiple instances with namespacesOne thing that helps here is calling everything from the window rather than starting a script and then asking for input part way through. Even if you don't need user input right away, you could open a window that says 'Starting...', and then in the On Open() script put everything you have before the window today, but with window or local scoped variables. With long scripts this can get out of hand, but then you might consider breaking those into different functions that are defined before the window, or even in different files.
As an example , the Validation Column for time series process data add-in is written this way. Note how UpdateUI is defined in this script on line 274 before New Window is called (sorry if that breaks for future readers), and then it is called when a button is pressed on line 670 and when other fields are updated.
Note that this function calls window: variables all over the place - that function's primary purpose is updating the user interface. For most add-ins I do not recommend this, pass all of the info it needs as arguments/parameters and then use all local variables inside the function and only pass results back.
Re: JMP Add-in — Isolating multiple instances with namespacesYou might also be totally fine with just relying on here namespace if you are running the add-in. It won't work if you run it from the same script window BUT if you run the script from separate script windows, it should.