cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
JMP is taking Discovery online, April 16 and 18. Register today and join us for interactive sessions featuring popular presentation topics, networking, and discussions with the experts.
Choose Language Hide Translation Bar
bobmorrane
Level V

How to keep formatting and comments with New Script () ?

Hello everyone,

 

it's me again. Another day, another question :)

So I made a nice long script that creates a new data table which contains some scripts. I use the New Script () function to do this.

dt<< New script ("myscript",

// comments
//beautiful indentation
   (
       (
        (  (  instructions) )
         )
      )
 ) 

);

 

My issue is that when I check the script Inside the table dt, all comments are lost, as well as the layout, my personal style of indentation, empty lines, etc.

These scripts are intended for users who are not necesarily familiar with JSL, so I need to keep the comments and the original layout.

 

I was trying to do something like putting my scripts in a string and using Append() or Append Text() but was unsuccessful.

 

mystring = " \[ // all my
                    // script
                       ]\" ;

myscript = dt<<New Script ("my script",
//some instructions
);
myscript  << Append Text (mystring)

Unfortunately this did not work. Any ideas ? Some tricks using Expr/ Eval , etc maybe ?

 

 

 

~~Bob~~
1 ACCEPTED SOLUTION

Accepted Solutions
XanGregg
Staff

Re: How to keep formatting and comments with New Script () ?

JSL Quote() might help:

 

New Table( "formatted script",
	New Script( "my script",
		Eval( Parse(JSL Quote(
// example
x = 1;

		Print( :column 1[1], y );
		
)			)
		)
	),
	New Column( "Column 1", Set Values( [5] ) )
)

 

View solution in original post

11 REPLIES 11
Jeff_Perkinson
Community Manager Community Manager

Re: How to keep formatting and comments with New Script () ?

Unfortunately, JMP is not designed to keep comments and formatting for scripts attached to the data table. This has been requested in the JMP Wish List: Preserve comments and whitespace in JSL code. Be sure to kudo that to indicate your support. I'll make sure the developers are aware of the desire. 

-Jeff
bobmorrane
Level V

Re: How to keep formatting and comments with New Script () ?

Thanks Jeff,

 

 I was affraid this would be the answer. However I feel there could be a DIY solution to bypass this...

 

For example, I tried putting all my script into an Expr () statement and it exported fine. Of course in the final script, everything is still between the brackets of the Expr.

 

But perhaps a text editing script could read this and delete the Expr and the brackets to have the original text ?

so my question is, is this possible? Can a script edit another script in this way ?

 

Or perhaps I could have my data table's scripts saved as separate .jsl files and then have the table automatically import them ?

 

Come on, there must be a way to do this :)

 

 

~~Bob~~
bobmorrane
Level V

Re: How to keep formatting and comments with New Script () ?

Update :

 

it looks like JMP scripts can't be edited by another script. I was trying to write my script in a script box Inside a window.  But as far a s I can see, the contents of a script box cannot be saved to a data table.

 

It looks like this is not feasible after all.

 

A couple of half baked backup options though :

  • By using \[...]\, I can save my script to the data table as a text string. Users can then go and have a look to see the comments. They could also copy and paste that string to a new script and obtain the original layout and comments. Not so great though cause it requires some input from the user.

 

  • Another fallback option would be to make a comment function in the script that prints comments into the log. Also not so great of an option, as by default, scripts ran from a data table don't have a built-in log.

 

  • Or I could just have my source code available as a resource in a word or pdf document. 

 

 

 

 Any other ideas, insights ??

 

 

 

 

~~Bob~~

Re: How to keep formatting and comments with New Script () ?

We ran into this issue in our JMP training courses about scripting. We imposed a particular style and format to make the code as clear as possible so we wanted to maintain it at all times. Our solution was to take advantage of the JMP journal that was created for each course. We added a button for each script that we provide in the course. The button script stores the formatted script  as a character string in a script file and then opens or runs it. Here is a very simple example to illustrate what the journal button does:

 

script = "dt = Open( \!"$SAMPLE_DATA/Big Class.jmp\!" );
dist = dt << Distribution( Y( :weight, :height ) );";

Save Text File( "My Script.jsl", script );
Open( "My Script.jsl" );

 

This solution works well for our purpose in the training. I don't think it will help you, though.

bobmorrane
Level V

Re: How to keep formatting and comments with New Script () ?

Thanks markbailey,

this is a useful piece of code. I guess one could save the .jsl files in the same folder as the data table they are used with. That would be a reasonnable way of doing things.
~~Bob~~

Re: How to keep formatting and comments with New Script () ?

The table scripts could run the scripts that are saved this way.

 

Include( "My Path/My Script.jsl" );

 

You can put the scripts anywhere (e.g., a single repository folder) and include the full path.

Re: How to keep formatting and comments with New Script () ?

You said, "it looks like JMP scripts can't be edited by another script. I was trying to write my script in a script box Inside a window.  But as far a s I can see, the contents of a script box cannot be saved to a data table."

 

It is possible. It might not be obvious. It requires scripting techniques that are not basic, though. Here is an example to illustrate how it might be done. It 

 

Names Default to Here( 1 );

script window = New Window( "Script", << Modal,
	sb = Script Box( "print(\!"Hello\!");" ),
	H List Box(
		Button Box( "OK", s = sb << Get Lines ),
		Button Box( "Cancel" )
	)
);

If( script window["Button"] == -1,
	Throw( "User cancelled" );
);

Print( s );

n = N Items( s );
script = s[1];

For( line = 2, line <= n, line++,
	script ||= "\!n" || s[line];
);

Print( script );

Eval( Parse( script ) );

Eval(
	Substitute(
		Expr( Current Data Table() << New Script( "My Script", sss ) ),
		Expr( sss ),
		Parse( script )
	)
);

 

However, the table script loses the formatting because the Parse() function discards it, just like the JSL interpreter. So this example does not solve your original problem.

bobmorrane
Level V

Re: How to keep formatting and comments with New Script () ?

Hi @Mark_Bailey,

 

Thanks for this piece of code, it is useful indeed.

 

So that solves one point : the content of a script window can be saved into a Script.

But, the first part still seems true "JMP scripts can't be edited by another script". The script you provided writes a new script every time with the New Script () function;

As far as I can see there is no way to refer to a script within a data table and that is the issue. What I mean is that there is no datatable: myscript or datatable<<edit script ("myscript").

 

all one can do is to copy scripts, and then past them in a new script. But, indeed that doesn't solve the issue as it transforms it into a JSL expression, deleting comments and formatting in the process.

 

 

I think it is time to admit there is currently no solution. It is in the JMP wishlist, so as @Jeff_Perkinson mentionned,

everyone interested please make sure to kudo   Preserve-comments-and-whitespace-in-JSL-code-attached-to-data  !!

 

 

 

~~Bob~~

Re: How to keep formatting and comments with New Script () ?

You said, "As far as I can see there is no way to refer to a script within a data table and that is the issue. What I mean is that there is no datatable: myscript or datatable<<edit script ("myscript")." You can send this message to the data table, after running my example code above:

 

dt << Get Script( "My Script" );

Now I can use expression or character string functions to edit the script from the data table.