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):
- Open your HTML Executable project and select the "Behavior & Scripting" tab.
Then double-click on the "UserMain" script to open the script editor as shown on the figure.
- In the script editor, insert a new script procedure called "OpenAnExternalProgram" (you can give it another name but remember it in that case).
- Paste the script available below and click on Save. The editor is closed.
You will need to insert the following code in the editor:
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∞
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 <A> HTML tag and with a
JavaScript call. For further information, check this help topic:
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:
<a href="hescript://UserMain.OpenAnExternalProgram">Standard HTML link
</a>
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∞
Edit the HTML page in which you would like to place the button. Then use the following HTML code:
<input type="button" onclick="javascript:window.external.runHEScriptCom('UserMain.OpenAnExternalProgram');" value="Open Program" name="B1">
Compile your ebook and test it! Again the "
anotherebook.exe" file is executed as expected.
Code of this page:
<h3>Run external programs
</h3>
Run an external program.
<p><a href="hescript://UserMain.OpenAnExternalProgram">Standard HTML link
</a></p>
<p>Test with JavaScript function:
</p>
<p>
<input type="button" onclick="javascript:window.external.runHEScriptCom('UserMain.OpenAnExternalProgram');" value="Open Program" name="B1">
</p>
<p>It is working fine!
</p>
Notes
- If you would like to open a document file, then you should use the OpenFile internal function instead of RunAProgram.
- Check the source code of the Main Demonstration∞: there are several samples available about how to open files, launch programs...