=====How to run external programs from my ebook?===== In this topic, you will learn how to to launch a program from a HTML page (for example another compiled ebook). We will use a simple HEScript function to do that. The program we want to launch is called "//anotherebook.exe//" (it can be what you want of course) and in this sample, this program file will be available in the same folder as our ebook .exe file (named //demo.exe// here): {{image class="center" alt="folder" title="folder" url="images/ep/p1.png"}} 1) Open your HTML Executable project and select the "Behavior & Scripting" tab.{{image class="center" alt="folder" title="folder" url="images/ep/p0.png"}}Then double-click on the "UserMain" script to open the script editor as shown on the figure. 1) In the script editor, insert a new script procedure called "//OpenAnExternalProgram//" (you can give it another name but remember it in that case). {{image class="center" alt="folder" title="folder" url="images/ep/p2.png"}} 1) Paste the script available below and click on **Save**. The editor is closed. You will need to insert the following code in the editor: %%(pascal) procedure OpenAnExternalProgram; var EbookPath, MyProgram: String; begin // Read the path to the folder that contains our ebook .exe EbookPath := GetGlobalVar("HEPublicationPath", ""); // Construct the path to the .exe file we want to launch. MyProgram := EbookPath + "anotherebook.exe"; // Execute the program! if not RunAProgram(MyProgram, "", EbookPath, false, SW_SHOWNORMAL) then begin MessageBox("Unable to execute the external program : " + MyProgram, "Ebook Error", MB_OK+MB_ICONERROR); exit; end; end; %% We used two built-in HEScript internal functions: GetGlobalVar and RunAProgram. Check this help topic for further information about their parameters: [[http://help.htmlexe.com/index.php?show=scriptreference.htm http://help.htmlexe.com/index.php?show=scriptreference.htm]] The full path (folder path+filename) to the program file we want to run is stored in the //MyProgram// variable, and the latter is then passed to the RunAProgram function. If the function is not successful, i.e. the program file is not executed, an error message is displayed. Now your new HEScript procedure is ready to be called. We will see two means to do that: with a standard HTML tag and with a JavaScript call. For further information, check this help topic: [[http://help.htmlexe.com/index.php?show=callscript.htm http://help.htmlexe.com/index.php?show=callscript.htm]] **Run the program by clicking on a standard hypertext link** Edit the HTML page in which you would like to place the hyperlink. Then use the following HTML code: %%(html4strict) Standard HTML link %% The **hescript:""//"" ** custom protocol tells the ebook that it should execute the //OpenAnExternalProgram// procedure of the //UserMain// script. Take care about the **period character** that separates the script's name from the function's name. Compile your ebook and test it! The "//anotherebook.exe//" file is executed. **Run the program with JavaScript ** We'll use a simple button that executes JavaScript code when it is clicked. HEScript procedures and functions can be called from JavaScript using the **window.external.runHEScriptCom** method. Learn more here: [[http://help.htmlexe.com/index.php?show=windowexternal.htm http://help.htmlexe.com/index.php?show=windowexternal.htm]] Edit the HTML page in which you would like to place the button. Then use the following HTML code: %%(html4strict) %% Compile your ebook and test it! Again the "//anotherebook.exe//" file is executed as expected. {{image class="center" alt="folder" title="folder" url="images/ep/p3.png"}} Code of this page: %%(html4strict)

Run external programs

Run an external program.

Standard HTML link

Test with JavaScript function:

It is working fine!

%% **Notes** 1) If you would like to open a document file, then you should use the **OpenFile** internal function instead of **RunAProgram**. 1) Check the [[http://www.htmlexe.com/samples.htm source code of the Main Demonstration]]: there are several samples available about how to open files, launch programs...