figure_cleaning_window_anim_150_wht_16983If we are writing JSL scripts that are going to be used by people other than ourselves then we should take care to de-clutter the user environment when our scripts finish.

I’ll illustrate this principle using a trivial example:

Whilst the above code is simple it is intended to illustrate a common structure of scripts.

What I want to ensure is that the data table that I created is closed when a user is finished with my script.  I can detect when the user has finished by detecting an OnClose event that occurs when the distribution window is closed.  That allows me to do something like this:

That’s the principle.  But there is a problem.  The table reference dt is valid within the script but the script has finished execution prior to the window closing.  The variable will be undefined when it is accessed by the OnClose event.

One approach is to reference the table by name rather than by reference:

However, this may not always work.  If I run a script multiple times then whilst I think I am creating a table called “Demo” JMP might name it “Demo 2”.  So the smart thing to do it to re-check the table name:

That is more robust, but now I have gone in a circle because again I am using a variable (dtName) which is out of scope when the OnClose event occurs.

One solution to this is to use a pattern of code for dynamical JSL, which will insert the evaluation of the variable rather than the variable itself:

This is the approach that I typically use.  It might look complex but it is a standard pattern of code and easy to copy and paste to the end of a script.

Share the joy:

Leave a Reply