KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Using LAUNCH EXTERNAL PROCESS to open a file with the default application
PRODUCT: 4D | VERSION: 11.4 | PLATFORM: Mac & Win
Published On: July 30, 2009

Often it is the desire of the developer to allow users to open a file on disk with the default application instead of 4D. This can be done on both Mac and Windows without knowing what the document's default application is, using the LAUNCH EXTERNAL PROCESS command.

On Mac OSX the UNIX command "open" can to be used along with the POSIX/UNIX path to the document as shown in the code below:

C_TEXT($Path_T)
$Path_T:="\"/Users/myName/Documents/Projects folder/MyNotes/myDocument.doc\""
SET ENVIRONMENT VARIABLE("_4D_OPTION_BLOCKING_EXTERNAL_PROCESS";"false")
LAUNCH EXTERNAL PROCESS("open "+$Path_T)


The above code will open the "myDocument.doc" using whatever application has been set as the default application for the file type of ".doc". That could be Microsoft's Word, Apple's Pages, OpenOffice, NeoOffice, etc.

Note that the entire path is wrapped in quotes(""). This allows for any space characters that may be contained in any of the names in the path.

As explained in KB asset 75812: Using Launch External Process to run applications asynchronously, the SET ENVIRONMENT VARIABLE command used here causes 4D to execute the command without waiting for it to complete, in an asynchronous mode.

The same can be accomplished on Windows by following example #10 that is shown in the command's documentation.

Commented by Jesse Pina on August 7, 2009 at 5:29 PM
Opening a file with the default application makes the workflow from 4D to another application seemless. This is a perfect example of just how powerful the LAUNCH EXTERNAL PROCESS command is.