A One-liner, cross-platform solution for opening files with their default applications

To open a file with its relevant application, it has always been a recurring question on the OmnisDev List.

The only answer till now was to use the Microsoft Windows API for the Windows Platforms and the Apple Scripts for the Mac Platforms. These work flawlessly!
Recently there has been a newer way of achieving this task.
Pretext: Sometime ago I posted a note on the list regarding an undiscovered function of the Form File component. One of its ability is to do OS specific operations using the method $osoperation (I have posed the message on the list titled “Re: FormFile Component. (Not advertised feature)”). The possible operations that can be performed on a file are “Open”, “Edit” and “Print”, similar to the options you get when you right-click a file in Microsoft Windows.
The need for a Studio 4.2 Web Edition SDK is so that we can design and launch remote forms in the “Fat client” mode. This solution is cross-platform.
Solution: (proof of concept)
  1. Create a dummy remote task and then create a remote form.
  2. Open the remote form, and change the remote form’s property, $openinwindow to kTrue.
  3. Drag and place the “FormFile” component on that window from your component store. Also drag and drop the Ok button.
  4. Assign the form file’s $name property a name, I used “FormFile” (for ease of reference).
  5. Add the following One-Liner code to the click event of the Ok button. Do $cinst.$objs.FromFile.$osoperation(“c:\MyTextFile.txt”,’open’,’FALSE’)
  6. Make the $event method of the Ok button “execute on the webclient”. And… Test it…
VOILA!!!!
On the Mac Omnis manages to launch the TextEdit and on windows the Notepad. (NOTE: On the Mac you have to give the “:” separated path, not the posix or unix path and make sure that the “cAsInG” is right)
Catches:
  1. This one liner gets triggered by an event, i.e. it must be behind a button or an event driver. You cannot call/trigger this by a normal Method Call.
  2. To open the remote form in the fat client you cannot use the “Open window” command, you must rather use, Do $clib.$classes.test.$open() or something of similar sorts that walks the navigation tree.
  3. Make sure that your path to the file including the file name are cased and spelt correctly. This will always throw a Notional error, no exceptions! Hence debugging could be hard.
Need to know: Since studio 4.2 (may be before that) we are able to launch remote forms on a Fat Client or the desktop environment. They look ‘n feel like normal windows, so your client is none the wiser. Also they don’t need any special runtime numbers to make things work. They work as they are meant to in the normal runtime engines on each platform (not in a browser).
This solution, just like the old one, works based on the fact that the right file extensions are associated with the relevant application. And, what happens behind the scenes is that Omnis invokes the Microsoft Window’s ShellExecuteA API and passes the relevant parameters.
The “old” way for opening a file with its relevant application on a Mac…
Calculate lv_script as con(‘Tell application “Finder”‘,chr(13),’Open “‘,lv_path,'”‘,chr(13),’End tell’) Calculate #F as $runapplescript(lv_script,lv_result)
The “old” way for opening a file with its relevant application on Microsoft Windows…
Register DLL (“shell32.dll”,”ShellExecuteA”,”JJCCCCJ”) Returns lv_result
Call DLL (“shell32.dll”,”ShellExecuteA”,0,”open”,lv_path,” “,” “,1) Returns lv_result