cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
shaira
Level IV

How to abort script and show error message if save file fails?

Hi,

I made a script that converts JMP to a csv file and saves it to network PCs using their IP address.

dt = Current Data Table();
ToolID = {"G01", "G02", "G03", "G04"};
IP_add = {"10.87.123.456", "10.87.123.789", "10.87.456.789", "10.87.789.123"};
current_pref = Char( Arg( Parse( Char( Get Preferences( Export settings ) ) ), 1 ) );
Pref( Export Settings( End Of Field( Comma ), Export Table Headers( 1 ) ) );
captxt = "";
For( i = 1, i <= N Items( IP_add ), i++,
	dt << save( "\\" || IP_add[i] || "\c$\filedir\file.csv" );
	Caption( "Updated " || Tester[i] || " \\" || IP_add[i] );
	captxt = captxt || "
	Updated " || ToolID[i] || " \\" || IP_add[i];
	Wait( 0.2 );
);
Caption( captxt );
Eval( Parse( "pref(" || current_pref || ")" ) );

Now, we noticed that if we forget to put .csv in the save command, the script will still execute to the next lines, and show caption that the saving is successful even if it is not.

 

The only way we can know that it is not successful is to check the file in the network PC and JMP log file which has this error:

Unable to save file. Unknown data target.

My question is this. If this error happens, how do we stop the script and show an error message? This kind of "silent" error can be misleading to the user.

 

I tried using Try and Throw, but it did not work.

Try(dt << save( "\\" || IP_add[i] || "\c$\filedir\file.csv" );, Caption("Error! Check log."); Throw());

Thanks,

Shaira

5 REPLIES 5
msharp
Super User (Alumni)

Re: How to abort script and show error message if save file fails?

Just save it and then check if the file exists.  If it doesn't it didn't save:

 

dt = Current Data Table();
ToolID = {"G01", "G02", "G03", "G04"};
IP_add = {"10.87.123.456", "10.87.123.789", "10.87.456.789", "10.87.789.123"};
current_pref = Char( Arg( Parse( Char( Get Preferences( Export settings ) ) ), 1 ) );
Pref( Export Settings( End Of Field( Comma ), Export Table Headers( 1 ) ) );
captxt = "";
For( i = 1, i <= N Items( IP_add ), i++,
	location = "\\" || IP_add[i] || "\c$\filedir\file.csv";
	dt << save( location );
	
	//Should work, may not depending on your network settings
	If( !File Exists( location ), Caption("Error! Failed to save to location: " || location); Throw());
	//Could also try opening it, which will throw an error if it doesn't exist
	Try(dttemp = Open(location); Close(dttemp, nosave), Caption("Error: " || exception_msg); Throw());
	
	Caption( "Updated " || Tester[i] || " \\" || IP_add[i] );
	captxt = captxt || "
	Updated " || ToolID[i] || " \\" || IP_add[i];
	Wait( 0.2 );
);
Caption( captxt );
Eval( Parse( "pref(" || current_pref || ")" ) );

If the issue only happens when "if we forget to put .csv in the save command", it'd make more sense to just check if the user forgot this and correct it for them.  That said, you may want this error if there is network or connection issues.

shaira
Level IV

Re: How to abort script and show error message if save file fails?

Hi @msharp,

Thanks for the input. Unfortunately, I cannot do the file checking method since I have many IP address input.

File is already there, and what I need to know is if the file was updated (can be done by checking the timestamp). Best to check the ".csv" as you mentioned, but I really want a poke-yoke since I don't know what else can trigger this "silent errors".

 

I think I will add this to current script:

1. Run the codes already written but in the for loop, add code lines to get the file time stampbefore and after saving.

2. Flag an error if before and after timestamp are equal.

 

I don't know how to get time stamp yet but I'll start from this: https://community.jmp.com/t5/Discussions/How-to-make-JSL-get-file-timestamp/td-p/3764

 

Thanks again.

 

 

 

ian_jmp
Level X

Re: How to abort script and show error message if save file fails?

Regarding timestamps, take a look here at 'LastModificationDate()'

msharp
Super User (Alumni)

Re: How to abort script and show error message if save file fails?

While using the time stamp method is a good idea, a better idea might be rethinking your data architecture.  Your current method is less than optimal saving the same csv to multiple locations and overwriting them is just throwing away data.  I would recommend looking into setting up a database.

shaira
Level IV

Re: How to abort script and show error message if save file fails?

@ian_jmp, thanks! I'll look into this.

@msharp, yes, in the future I think that is the direction, though it needs some logistics, paperworks, etc. So for now, I am eyeing the script solution as short term fix .