Navigation: For Developers > Extend Functionality with HEScript > Sample Scripts >

How to open an external file





Welcome to a comprehensive guide . This guide on how to open document files located outside your HTML Executable ebook will walk you through the process in a clear and concise manner. Let's get started!


The Objective 🎯


You can open document files located outside your ebook, but you need to make some changes to your HTML code. The best approach is to use an HEScript script function. This function would be invoked from your HTML code and will open any file you specify with a parameter.


👉 Please note that the file to be opened must be in the same folder as the output EXE file or in a subfolder.


Step-by-Step Guide 📝


Step 1: Create the HEScript Function 🖋️


Open your HTML Executable project and navigate to the Script Manager. Double-click the "UserMain" script to open the script editor. Add the following function code:


procedure OpenAFile(Filepath: String);
 EbookPath, MyFile: String;
 res: integer;
 EbookPath := GetGlobalVar("HEPublicationPath", "");
 MyFile := IncludeTrailingPathDelimiter(EbookPath) + FilePath;
 res := OpenFile(MyFile, "", SW_SHOWNORMAL);
 if res < 32 then
  MessageBox("Error while opening: " + MyFile + ". Error code: " + inttostr(res), "Launch Error", MB_OK+MB_ICONERROR);


Don't forget to save your script!


Step 2: Edit the HTML Page 📚


Edit the HTML page with the links that should open the file. Replace the links with the following HTML code:


<a href="hescript://UserMain.OpenAFile|PATH TO YOUR DOCUMENT">Open my document</a>


Replace `PATH TO YOUR DOCUMENT` with the relative path to the file you want to open (do not remove the `|` character).


Please note that non-ASCII characters of the path for the URL should be encoded. URL encoding replaces unsafe ASCII characters with a `%` followed by two hexadecimal digits. For instance, spaces in filenames should be replaced by `%20`. You can find an online application for character conversion at: W3Schools


Examples 🖥️


ØIf you want a link that opens `mydocument.txt`, use this HTML code:


<a href="hescript://UserMain.OpenAFile|mydocument.txt">Open my document</a>


ØIf your file is inside a subfolder, use a path relative to the folder that contains the publication .EXE file. For instance, to launch the file `my folder\my document.mp3`:


<a href="hescript://UserMain.OpenAFile|my%20folder\my%20document.mp3">Open the MP3</a>


Compile your project and that's it!