Our World Statistics Day conversations have been a great reminder of how much statistics can inform our lives. Do you have an example of how statistics has made a difference in your life? Share your story with the Community!
Choose Language Hide Translation Bar

Open a file in the operating system default program

Problem

If you need to open a file in an external program, typically the JSL Open() function will work for you. However, if you want to open a file type that JMP knows how to import, you will probably end up with the file imported instead of opened in the external program. Opening an Excel file is a common use case for this.

Solution

To do this, we can use the JSL RunProgram() function. Windows requires some special handling so that JMP is not frozen until the program closes, nor does the process need to close before JMP closes. 


/* 	Function: openFileInOSDefault
		Open file in the operating system default program
	Arguments: 
		filepath - local path file
*/
openFileInOSDefault = Function({filepath},
	If( Host is( "Windows" ),
		Run Program(
			Executable( "PowerShell.exe" ),
			Options(
				Eval Insert( "\[-Command "& {Start-Process '^filepath^'}"]\" )
			),
			Read function( "text" )
		);
	,
		RunProgram(
			Executable( "/usr/bin/open" ),
			Options( filepath ),
			ReadFunction( "text" )
		)
	);
);

Discussion

Generally, I don't think you need to understand how exactly this works and you can plug this into your scripts and call it with a path to the file you want to open.

Article Labels

    There are no labels assigned to this post.

Article Tags
Contributors