cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
DMR
DMR
Level V

How can I trap an attempt to write to an open file?

Hi - I have a JSL script that prompts the user to save a selection of JMP graphics that have just been generated by an analysis directly to a PowerPoint presentation.  The user is likely to want to inspect the resulting slides immediately, so I don't want to close the PowerPoint file automatically - but if the user then tries to append any more graphics to it with a second run of the script, they'll get a JMP Alert telling them they're trying to overwrite a file that might be open in another program (which of course it is).

 

I've tried trapping an attempt that could fail with the Try() function, but this doesn't seem to work: the error occurs anyway - and although the JMP Alert does correctly tell the user what the problem is, I don't really want error messages interrupting the flow of the program.  I thought using the "Is File Writable()" function to check it in advance would deal with it, but that doesn't seem to work either: the PowerPoint file is deemed to be writable, even though it's open.

 

At the moment I'm putting up a message on the screen - if the file already exists (which obviously I can check for) - warning the user in advance that if it's open, they'll need to close it before proceeding - but this is a bit inelegant and doesn't really solve the problem.  Is there a way to automatically (a) check that it isn't open already, and (b) close it if necessary?

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: How can I trap an attempt to write to an open file?

txt = logcapture(...jsl that might make log messages...)

 could also help with the same approach.

 

edit: You might need Create Database Connection Error  too, which shows how to use BatchInteractive() to suppress a dialog and just get a log message. ...and more on batch interactive .

Craige

View solution in original post

3 REPLIES 3
txnelson
Super User

Re: How can I trap an attempt to write to an open file?

You can use

mylist = get log();

and then parse through mylist to see if the warning occurred and if it did, then take the action you want to take.

Jim
Craige_Hales
Super User

Re: How can I trap an attempt to write to an open file?

txt = logcapture(...jsl that might make log messages...)

 could also help with the same approach.

 

edit: You might need Create Database Connection Error  too, which shows how to use BatchInteractive() to suppress a dialog and just get a log message. ...and more on batch interactive .

Craige
DMR
DMR
Level V

Re: How can I trap an attempt to write to an open file?

Many thanks Jim and Craige - I've now got a working solution that's an amalgam of both your recommendations.  I had to use the "Batch Interactive" command because the JMP Alert wasn't being sent to the log, but I couldn't get the logcapture() command to work, whereas << get log() did what I needed.

 

I note that the log has to be cleared each time, or the same error message gets picked up on every subsequent run.  Just for the record, I've included a stripped-down version of my final script below.  (I'm guessing that the second "Batch Interactive(bi) term turns the interactive JMP Alert back on again - is that right?)"

 

OutputFileName = "$DESKTOP\My Slides.pptx";

if(not(file exists(OutputFileName)),

	// No possible problem here;

	Text Box("New File - First Page") << save presentation(OutputFileName);
	caption("You've just started a new presentation - here it is..."); wait(2); caption(remove);
	open(OutputFileName)

	,

	// The file exists but it might be open - so be wary;

	bi = Batch Interactive( 1 );

	// If I don't clear the log first, it will tell me there's an error even when there isn't;
	
	clear log();
	Text Box("Existing File - Next Page") << save presentation(OutputFileName, append);
	Error_List = get log();

	if(contains(concat items(Error_List), "Unable to overwrite"),
	
		beep();
		caption("You can't write to the file\!rbecause it's open right now.\!rPlease close it, then try again.");
		wait(3);
		caption(remove);
		Batch Interactive(bi)
		
		,
		
		caption("Here's the updated presentation with your latest slide"); wait(2); caption(remove);
		open(OutputFileName)
		
		)
		
	);