I wrote this as a tutorial for Cogntive Science masters students in a programming course. I borrowed some text from a tutorial I found on the web about Visual Basic and HTA but rewrote it extensively for its lacunes and my main purpose: discussing the creation, saving, reading and writing from and to text files in general, and from within web pages specifically. It is instructive also for the use of COM objects from within html files/HTA's (including the Excel-COM that can be used to incorporate an Excel sheet in your HTA application). I hope you get excited.
If you have any comments or suggestions or other remarks, please e-mail me: rgrasman•{at}•uva•nl. Please forgive me for some of the crap (get on with it!), I will edit it when I have more time.
In the 'good' old days, when RAM was measured in bytes rather than megabytes, and processor speeds were measured in single figures, most operating systems came with a programming language. DOS had GWBASIC and QBasic, UNIX had it's shell scripting, most microcomputers used BASIC and CP/M relied on assembler. This gave users access to a programming language without the need to spend money. Magazines could publish program listings secure in the knowledge that users had access to the relevent language because it was bundled with the Operating System. Then along came the GUI (Graphical User Interface) and suddenly the facility dried up. QBasic was still shipped with Windows but it wasn't updated to take advantage of the new GUI so nobody used it (even though it is still a great way to get into programming). This has lead to a fragmentation of the number of languages that users could be using (Java, VB etc...). I believe this has lead to most users not being able to write even the most rudimentary programs. It could be argued that such skills are not required and that the GUI does it all for them. Sadly, this is just not true. There are many tasks that benefit from programming skills which the GUI does not encourage.
Then Microsoft came up with the Windows Scripting Host. It could be easily programmed in JScript (a version of JavaScript) using a text editor and it gave the user access to virtually all the facilities that could be found in full languages like C++. It is relatively slow because it is interpreted but that may be considered an advantage, as there is no need to mess around with compilers, and has much in common with the UNIX shell script. However, it lacked one vital ingredient. It's GUI features were limited to message boxes and input boxes which limited it's appeal. It is possible to write small applications with it but the graphical user interface (GUI) is very sparse.
Then along came Internet Explorer 5 and with it the HTA (HTML Application) which allowed scripts to be hosted inside IE complete with GUI widgets (like buttons, input fields, file selectors) but without the limitations imposed by the browser. HTA's look just like ordinary windows and can be run just like programs but they can also host HTML pages complete with graphics and all the active content that comes with it.
Surprisingly, for a technology so useful, there seems to be little mention of it in the computer press. Some examples of what you can achieve with HTA's can be found here (famous HTMLEditor sample at msdn.microsoft.com) and here (a little program I made).
To follow this tutorial, you will need access to a Windows PC running Internet Explorer 5.0 or above. It will be necessary to construct a basic template file to make creating new files so much easier. This file will look something like this:-
You will notice that it is an HTML file despite the fact that a HTA is run locally and not on the web. Even though a HTA acts like an ordinary Windows program, it is actually still being run in a browser window albeit a specialised one. This allows us to use all of the active content and GUI widgets you associate with the web without actually having to run it over a connection or via a local web server.
Notice the line
Cut and paste this code into a file and name it template.hta. Double click on it and check there are no errors and that a window is displayed. If you make a copy of it and name it template.htm and remove the tag:-
In any useful application, results are computed that you usually want to store more permanent than in the main memory. Maybe you want to analyze the data you have collected or created or computed later, or you want to transfer the data to another computer. In any case you want the data to persist, even if your computer is turned off. What you will want to do is write to and read from files. These will be stored on the local drive NOT on the Internet and can therefore be regarded as safe (or at least as safe as any other file created by an application). Consider the following program and cut and paste it into your template file.
The file having been created can now be written to, which is what the next program does.
Start a new template file and cut and paste this into it.
document.write("bla ... "); statements. Here however we invoke a write command on a file (referenced by a) instead of on the document:
WriteLine method adds a carriage return and linefeed to the end of its argument. When this file is opened in Notepad then it will display each character on two seperate lines:
So now to read from the file you've created. Some variables and a reference to the filesystem as before.
Basically, this is all there is to it. In what follows we will delve into what has been presented so far in the next couple of sections a little bit more deeply.
| Try changing the programs to create different files and try complete words and sentences to see how this might be useful in creating a simple database program. |
It is at this juncture let's take a breather from the coding and consider what makes HTML Applications so special. When an ordinary web page is launched, it opens up in Internet Explorer complete with with all the buttons and security features that are essential for surfing the Internet. What Microsoft have done is taken the core of Internet Explorer (the bit that handles scripting) and made it look like an ordinary program. Why? Because it enables scripts to be run that are not reliant on the presence of the Internet so the various security features can be switched off making scripts run much more seamlessly, and to all intents and purposes are Windows programs that can be launched from shortcuts and menu's just like real programs. They even look like real programs, complete with icons and menu's. But they can also make use of all of the features we associate with a web browser such as graphics and hyperlinks (hyperlinks are launched in IE proper so security is preserved).
To make HTML Applications look even more like programs, the window containing the script can be controllled in certain specific ways. Here I am concerned with making them look more like ordinary Windows programs but there are other options that can be employed but these will not be gone into in any depth here. So, below is a HTML Application that does nothing but it does contain all the controlling code so we can test it all much more easily. The code is below. Don't be put off as I will explain it all further down.
writeFile is defined:
document is an example of an object, as is an Array object; the document object has the property body which itself has the property innerText (e.g., document.body.innerText = "Hello, World!"), and also the method write (e.g., document.write("Hello, World!")), an Array object has a length property (e.g., ["man","child","horse"].length, equal to the number of elements in the array), and a sort method (e.g., ["a","c","b"].sort(), which--surprise, surprise--sorts the elements of the array). In this case the object is a File System Object. This is in fact a library, akin to the sort of library you have been creating in .js files, which is stored on your Windows computer. A File System Object gives you full access to the entire structure of the files and folders hierarchy on your harddisk, and allows you to create, delete, and overwrite files (as you have already seen) and folders.
[In the "system" folder of your Windows system--most likely the "C:\Windows\system\" folder, i.e. the folder called 'system' in the folder called 'windows' on you harddrive C--you will find a great many files with the ".dll" filename extension: these are libraries, dynamic link libraries to be a bit more precise; they are there for Windows applications to use and share. Among them are also .ocx files, which are similar libraries, called ActiveX Objects, which allows one program to incorporate the functionality of another. For instance, there exists an ActiveX Object that allows you to incorporate Excel sheets into your webpage.]
Different programs may use a given ActiveX Object (e.g. the File System Object) simultaneously, and to prevent clashes between programs, every time the ActiveX Object library is asked for by a program, Windows creates a new instance of the object. The keyword new commands the interpreter to create a new instance of an object. The variable fs is assigned the reference to the new File System Object, through which you now have access to the file system on the hard drive. To do so we use its CreateTextFile method:
CreateTextFile method actually physically creates a file by reserving part of your harddisk. It then returns a reference to a File Object, which again consists of a collection of properties (variables) and methods (functions). Because there can be only one file with this name at this portion of the disk, it is impossible to create multiple instances of t, and so we don't use the new keyword. It might take you a while to get your head around what these two lines of code is doing. We first create an object that happens to be the file system object, and stored a reference to it in fs. Using this reference, we can then get to the function that will create the file for us which in this case we have called "hello.txt" which will be written to the folder where the .hta file is stored. Next, we can write to the file, using the WriteLine method that File Objects possess:
b to get to the function that will write a line into the file object created above with the simple string "Hello". We need to close the file, so that an end-of-file marker is written to the harddrive---if we don't, Windows or other programs will not know where the end of the file is and continue reading beyond outside the part of the harddrive reserved for the file:
fs and a are local to this function, and cannot be accessed by other functions or the main program.
FileExists method of the File System Object:
One has to be even more pedantic: Before you start reading using ReadLine() (or Read(n) which reads n characters, or ReadAll(), which reads all there is in the file), you must check if you are not at the end of the file--that is, you must test if you're not at the "end of file" or "end of stream" (eof) marker on the harddrive of the file, this was done in the first program with a.AtEndOfStream which is false if there are more unread characters in the file, and true when none is left.
To repeat: More on the File System Object and File Object can be found in the JScript reference that can be downloaded from the msdn microsoft site
| Combine the files from this Reading Data section with the files from the previous section (Saving Data to file) to create a program that will only create a file if it doesn't exist, and only read from it if it exists. You'll have three buttons: Quit, Read and Create with the relevent routines in the code. |
Extend the program from exercise HTA3, to include a textarea that the user can type into, and that saves the text typed into this textarea. Also, let the user provide a filename to which the data should be saved in an input type="text" field. If the file exists, the user should be prompted if he would like to overwrite the existing file, or if he wants to provide a new name. To this end, you can use the confirm---for instance call confirm("The file " + filename +" exists allready. Do you want to overwrite?"). The return value of confirm is a Boolean value: true if the user clicked the 'OK' button, false if the user clicked the 'Cancel' button. When pressing the Read button, it should read the entire content of the file, and display the result in the textarea.
|