cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
%3CLINGO-SUB%20id%3D%22lingo-sub-246050%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EOpen%20a%20file%20in%20the%20operating%20system%20default%20program%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-246050%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CDIV%20class%3D%22lia-message-template-problem-zone%22%3E%0A%3CH2%20id%3D%22toc-hId-1648454135%22%20id%3D%22toc-hId-1733359508%22%20id%3D%22toc-hId-1733359508%22%3EProblem%3C%2FH2%3E%0A%3CP%3EIf%20you%20need%20to%20open%20a%20file%20in%20an%20external%20program%2C%20typically%20the%20JSL%20Open()%20function%20will%20work%20for%20you.%20However%2C%20if%20you%20want%20to%20open%20a%20file%20type%20that%20JMP%20knows%20how%20to%20import%2C%20you%20will%20probably%20end%20up%20with%20the%20file%20imported%20instead%20of%20opened%20in%20the%20external%20program.%20Opening%20an%20Excel%20file%20is%20a%20common%20use%20case%20for%20this.%3C%2FP%3E%0A%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-message-template-diagnosis-zone%22%3E%0A%3CH2%20id%3D%22toc-hId--903702826%22%20id%3D%22toc-hId--818797453%22%20id%3D%22toc-hId--818797453%22%3ESolution%3C%2FH2%3E%0A%3CP%3ETo%20do%20this%2C%20we%20can%20use%20the%20JSL%20RunProgram()%20function.%20Windows%20requires%20some%20special%20handling%20so%20that%20JMP%20is%20not%20frozen%20until%20the%20program%20closes%2C%20nor%20does%20the%20process%20need%20to%20close%20before%20JMP%20closes.%26nbsp%3B%3C%2FP%3E%3CBR%20%2F%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E%2F*%20%20Function%3A%20openFileInOSDefault%0A%20%20Open%20file%20in%20the%20operating%20system%20default%20program%0A%20Arguments%3A%20%0A%20%20filepath%20-%20local%20path%20file%0A*%2F%0AopenFileInOSDefault%20%3D%20Function(%7Bfilepath%7D%2C%0A%20If(%20Host%20is(%20%22Windows%22%20)%2C%0A%20%20Run%20Program(%0A%20%20%20Executable(%20%22PowerShell.exe%22%20)%2C%0A%20%20%20Options(%0A%20%20%20%20Eval%20Insert(%20%22%5C%5B-Command%20%22%26amp%3B%20%7BStart-Process%20'%5Efilepath%5E'%7D%22%5D%5C%22%20)%0A%20%20%20)%2C%0A%20%20%20Read%20function(%20%22text%22%20)%0A%20%20)%3B%0A%20%2C%0A%20%20RunProgram(%0A%20%20%20Executable(%20%22%2Fusr%2Fbin%2Fopen%22%20)%2C%0A%20%20%20Options(%20filepath%20)%2C%0A%20%20%20ReadFunction(%20%22text%22%20)%0A%20%20)%0A%20)%3B%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3CDIV%20class%3D%22lia-message-template-solution-zone%22%3E%0A%3CH2%20id%3D%22toc-hId-839107509%22%20id%3D%22toc-hId-924012882%22%20id%3D%22toc-hId-924012882%22%3EDiscussion%3C%2FH2%3E%0A%3CP%3EGenerally%2C%20I%20don't%20think%20you%20need%20to%20understand%20how%20exactly%20this%20works%20and%20you%20can%20plug%20this%20into%20your%20scripts%20and%20call%20it%20with%20a%20path%20to%20the%20file%20you%20want%20to%20open.%3C%2FP%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E
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.

JSL Cookbook

If you’re looking for a code snippet or design pattern that performs a common task for your JSL project, the JSL Cookbook is for you.

This knowledge base contains building blocks of JSL code that you can use to reduce the amount of coding you have to do yourself.

It's also a great place to learn from the experts how to use JSL in new ways, with best practices.