Working with HTML Applications

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Although you can control Internet Explorer from a separate script, you will have to include code to repeatedly ensure that the browser is still running. If you do not, you will encounter problems such as:

  • The browser is closed, and an error occurs when the script attempts to update a browser instance that no longer exists.
  • The browser is closed, but the script continues to run. (Without additional coding, closing the browser will not stop the script.) This can be confusing, especially if you restart the script and now have two copies of the same script running in parallel.

Embedding the script code inside a Web page enables you to overcome these problems. When you embed the code, the Web page and the script are linked together; closing the Web page will also terminate the script. However, this introduces a new set of problems. Because Internet Explorer is designed for use over the Internet, it has a number of built-in security precautions that prevent Web sites from harming the local computer in any way. For example, if you include Windows Management Instrumentation (WMI) or another ActiveX® control in a Web page, a message box similar to the one shown in Figure 17.3 will appear when the script is run.

Figure 17.3   Unsafe for Scripting Message Box

Unsafe for Scripting Message Box

In turn, the script will suspend itself until you click Yes. This means you cannot create a Web page that can refresh itself (for example, a Web page that periodically updates the status of all the printers on a print server). Each time the page queries the WMI service, the "unsafe for scripting" message box will appear and the script will pause until you click Yes.

One way to work around the security issues inherent in Internet Explorer is to embed your script within a hypertext application (HTA) rather than an HTML file. In its simplest form, an HTA is nothing but a Web page with the .hta file name extension rather than the .htm file name extension. In fact, you can convert any Web page to an HTA just by changing the file name extension.

However, HTAs run in a different process (Mshta.exe) than do HTML files. This allows HTAs to bypass Internet Explorer security. (Of course, HTAs still respect such things as operating system security and the NTFS file system security.) HTAs are fully trusted applications, meaning that you will not receive any security warnings when using objects such as WMI or the FileSystemObject.

Although HTAs respond to the same commands that can be issued to an instance of Internet Explorer, a number of additional attributes are available through the HTA object model. Several of these attributes are listed in Table 17.4.

Table 17.4   HTA Object Model

AttributeDescription
ApplicationNameSets the name of the HTA.
BorderSets the type of border used for the HTA window. Values include:

Thick. Creates a resizeable window.

Thin. Creates a window that cannot be resized.

BorderStyleSets the style of the content border in the HTA window. Values are:

Normal. Standard Windows border style. This is the default value.

Raised. Raised three-dimensional border.

Sunken. Sunken three-dimensional border.

Complex. Combines sunken and raised styles.

Static. Three-dimensional border typically used for windows that do not allow user input.

CaptionYes/No value specifying whether the HTA displays a title bar. The default value is Yes.
IconSets the path name of the icon that appears in the upper-left corner of the HTA window. The icon can be either a .ico or a .bmp file. If not specified, a generic application icon is used.
IDSets the identifier for the <HTA:Application> tag. This property is required if you need to write a script that returns the attributes of the HTA.
MaximizeButtonYes/No value specifying whether the HTA displays a Maximize button in the title bar. The default value is Yes.
MinimizeButtonYes/No value specifying whether the HTA displays a Minimize button in the title bar. The default value is Yes.
ShowInTaskbarYes/No value specifying whether the HTA is shown in the Windows taskbar. Regardless of the value set for this property, the HTA will always appear in the list of applications that are accessible when you press ALT+TAB.

The default value is Yes.

SingleInstanceYes/No value specifying whether more than one instance of this HTA can be active at any given time. For this property to take effect, you must also specify the ApplicationName attribute.

The default value is Yes.

SysMenuYes/No value specifying whether the HTA displays the System menu in the title bar. The System menu is displayed in the upper-left corner of the HTA window and provides access to menu items such as Minimize, Maximize, Restore, and Close.

The default value is Yes.

WindowsStateSets the initial size of the HTA window. Values are:

Normal

Minimize

Maximize

HTML applications require no special coding; you can create an HTA simply by changing the .htm file extension of a Web page to .hta. However, by adding the <HTA:Application> tag to the Web page code, you can gain additional control over how the HTA will be displayed on the screen and which elements of the user interface will be available. To configure any of these elements, include the <HTA:Application> tag and the appropriate elements within the Web page <HEAD> tag.

For example, the following code snippet prevents the HTA from being displayed in the taskbar:

<HTA:Application     ShowInTaskbar = No > 

Scripting Steps

Listing 17.9 contains HTML tags for creating a sample HTA file. Type this code into a text editor, and then save it with the .hta file name extension.

To create an HTA file, you must:

  1. Insert the beginning <HTML> and <HEAD> tags.
  2. Insert the <HTA:Application> tag. Within this tag, set the configuration options for the HTA file. In this example, these options include setting Border to Thick and BorderStyle to Complex.
  3. Insert the ending </HEAD> tag.
  4. Insert the <BODY> tag, and then insert the body of the Web page, using standard HTML tags.
  5. Insert the </BODY> and </HEAD> tags.

Listing 17.9   Creating an HTA File

1 2 3 4 5 6 7 8 9 10 11 12 13 14 
<HTML> <HEAD> <HTA:Application     Border = Thick     BorderStyle = Complex     ShowInTaskBar = No     MaximizeButton = No     MinimizeButton = No > </HEAD> <BODY> This is a sample HTA. </BODY> </HTML>

A sample HTA that displays service information is shown in Listing 17.10. In this script, the code to retrieve service information and to write that data to the browser window is included in a window_onLoad procedure. This procedure automatically runs anytime the Web page is loaded. As a result, service information will be displayed anytime you start (or refresh) the HTA.

Listing 17.10   Using an HTA to Display Service Information

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 
<HTML> <HEAD> <HTA:Application     Border = Thick     BorderStyle = Complex     ShowInTaskBar = No     MaximizeButton = No     MinimizeButton = No > <SCRIPT LANGUAGE="VBScript"> Sub window_onLoad     Set objDocument = self.Document     objDocument.open       strComputer = "."     Set objWMIService = GetObject("winmgmts:" _         & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")     Set colServices =  objWMIService.ExecQuery _         ("SELECT * FROM Win32_Service")       For Each objService in colServices         objdocument.WriteLn objService.DisplayName & "<br>"     Next End Sub    </SCRIPT> </HEAD> <BODY> </BODY> </HTML>

send us your feedback Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net