Scripting an administrative Web interface for the printer mapping utility

In "A Printer Mapping Utility" April 2002, InstantDoc ID 24368, I described how to display printer locations within your organization by placing printer icons on floor-plan maps. Map positions result from x and y coordinates (pixel offsets) for the Location attribute of each networked printer. Manually calculating pixel offsets for more than a few printers is time-consuming; maintaining pixel offsets in a large network in which printers might move weekly is challenging. To address these obstacles, you can use a script that runs in a Web browser to administer printer locations. In the second article in my series, "The Code Behind the Printer Mapping Utility," May 2002, InstantDoc ID 24598, I discussed the details of the PrintersMap.asp script. In this article, the third and last in the series, I discuss how to configure an administrative Web interface for the printer mapping utility, install and configure the utility, and prepare and run the script. Finally, I detail how the script works.

Configuring Printers
First, click a floor map to specify pixel offsets, as Figure 1 shows. The x and y coordinates, separated by "x" and in parentheses (e.g., 120 x 300), will appear in the Map position text box. Next, select a printer that you want to reposition from the Select a printer share list. The printer's location information, if any, will appear in the Printer location text box. With both the Printer location and Map position text boxes populated, you can click either Append or Replace to insert a value in the New location text box: Click Append to concatenate the value in the Map position text box to the value in the Printer location text box, and click Replace to insert the value in the Map position text box into the New location text box. If necessary, you can edit the value in the New location text box. If you click Append and the data in the Printer location text box contains the previous coordinate value along with the new coordinate value, you'll want to edit the value in the New location text box.

When the value in the New location text box is correct, click Update to apply the location change. When a Microsoft Internet Explorer (IE) message box appears, click Retry to see the updated printer map.

To save space, Figure 1 shows only a small portion of the floor map. However, make sure that you view the entire floor map when you select printer coordinates. If you don't view the entire page, the pixel offset values will be incorrect.

This script relies on both client-side JScript and server-side VBScript code. HTML and Active Server Pages (ASP) render the pages, and Dynamic HTML (DHTML) provides advanced form features. Windows Management Instrumentation (WMI) and Active Directory Service Interfaces (ADSI) provide read and write access to printer information.

Installing and Configuring the Interface
After you configure the printer mapping environment, which I outlined in detail in "A Printer Mapping Utility" and "The Code Behind the Printer Mapping Utility," copy the following files from the Windows Scripting Solutions Web site ( to the Main directory, which is a subdirectory of the PrintersMap virtual directory:

  • defaultadmin.htm*the initial print-server selection page
  • printersmapadmin.asp*the printer mapping administration script
  • stylesheet.css (updated for the administrative interface)*the style sheet that replaces the original style sheet in the Img directory
  • bigmask.gif*an image that the coordinate mapping feature uses to obtain x and y coordinates

You must also designate the printer mapping site as a trusted site in IE, then configure the trusted site option Initialize and script ActiveX controls not marked as safe to Enabled:

  1. Open IE, click Tools, then select Internet Options.
  2. Click the Security tab and, under Select a Web content zone to specify its security settings, click Trusted sites, then click Sites.
  3. Under Add this Web site to the zone, type
  4. http://server_name/printersmap

    where server_name is the name of the Web server that hosts the printer mapping site.

  5. Verify that the Require server verification (https:) for all sites in this zone check box is cleared and click OK.
  6. Click Custom Level.
  7. On the Security Settings dialog box, click the Enable radio button under Initialize and script ActiveX controls not marked as safe.

You need to make this security adjustment so that the GetObject() function can use client-side JScript to retrieve the moniker "WinNT://" + sPrnSvrName + "/" + sPrinterName. The sPrnSvrName variable receives the name of your print server, and the sPrinterName variable receives the name of the printer that the GetObject() function retrieves.

Preparing and Running the Script
Before you run the script, you must modify DefaultAdmin.htm and PrintersMapAdmin.asp to match the values you placed in Default.htm and PrintersMap.asp. In "A Printer Mapping Utility," I explained how to modify Default.htm to include your print-server names, floor-map filenames, and floor names and how to modify PrintersMap.asp to include Case values and variables to match Default.htm.

You must make one more modification to PrintersMapAdmin.asp. Within the <HEAD> section of the page, find the code that appears in Listing 1. Modify the Case values and sPrnSvrNameForJS variables to match the Case values and sPrnSvrName variables you entered later in the script. The client-side JScript uses this code, and it must appear in the <HEAD> section of the page because of how the code is organized. Although you could reorganize the script and reuse the Select Case code block later in the script, I chose this approach to clearly separate variables that the server-side VBScript and client-side JScript use.

After you complete the modifications, run the printer mapping administration script by entering


in your browser's Address bar, where servername is the name of the Web server that hosts the printer mapping utility. From the DefaultAdmin.htm page, select the print server that hosts the printers and click Next. This step calls PrintersMapAdmin.asp to display printers on the floor map.

How It Works
PrintersMapAdmin.asp uses the same printer mapping utility code to display printers that contain pixel offsets on the floor map. The utility displays any printer with invalid location values to the right of the floor map with an explanation of why the values are invalid. The printer mapping administration utility otherwise functions quite differently from the printer mapping utility.

The form objects below the floor map are unique to the printer mapping administration utility. When the page loads, server-side VBScript obtains printer share names and locations. Client-side JScript and DHTML then perform client-side form data manipulation. For example, when the page loads, the DHTML event fires and the fnDisableButtons() client-side function disables the Append, Replace, and Update buttons on the page. Other functions on the page enable these buttons as necessary. After you enter new location data, the IADs::Put method updates the printer's location value. IADs is a core ADSI interface, and Put is one of its methods.

The Details
The Select a printer share list (Listbox1) receives all the printer share-name values, and a hidden list (HiddenListbox1) receives the corresponding location values. Selecting any of the values in Listbox1 fires a DHTML event that runs fnShowText(), as Listing 2 shows.

The fnShowText() function populates the Printer location text box (Textbox1) with the location value in HiddenListbox1. If no location value exists, fnShowText writes "none specified" below Textbox1. At the end of this function, the script calls fnEnableAppend().

A transparent image that I named bigmask.gif is overlaid on top of the floor map. Client-side JScript uses this invisible image for coordinate mapping. Clicking anywhere on bigmask.gif fires a DHTML event that runs fnShowCoordinates().

I positioned bigmask.gif above the printer icons so that when you click anywhere on the image, you get x and y coordinates. If I hadn't positioned bigmask.gif above the printer icons and you clicked a printer, you would go to the printer page that the Internet Printing Protocol (IPP) provides. Listing 3 shows how the script returns x and y coordinates.

The fnShowCoordinates() function assigns the x and y coordinates (provided in pixels) to variables. Note that the script subtracts 20 pixels from the values that the two window events provide. This subtraction centers the printer icon at the position on the floor map that the operator clicks. The script then writes the modified x and y coordinates to the Map position text box (Textbox2). Finally, the script enables the Replace button (Button2) and fnShowCoordinates() calls fnEnableAppend().

The fnEnableAppend() function determines whether Textbox1 and Textbox2 contain values. If they do, the function enables the Append button (Button1). The script doesn't enable the Append button unless both text boxes contain values because both an x and y coordinate are necessary for positioning a printer on the map. Clicking the Append button (Button1) fires a DHTML event that runs fnAppend(), which concatenates the value in Textbox1 with the value in Textbox2 and places the result in the New Location text box (Textbox3). You can then edit the value in Textbox3 as necessary.

Clicking the Replace button (Button2) fires a DHTML event that runs the fnReplace() function. This function places the value that's in Textbox2 into Textbox3. Both functions, fnReplace() and fnAppend(), enable the Update button (Button3) because after Textbox3 contains a value, you can update the printer location.

Clicking the Update button fires a DHTML event that runs the fnModifyLocation() function. This function uses ADSI to update the printer object's location value. Listing 4 shows how the function modifies the location value.

The sPrnSvrName local variable obtains the name of the print server from the VBScript Select Case block code within the <HEAD> tag of the page. Listbox1 provides a value for the sPrinterName variable. Using these two variables, the GetObject() function binds to the object (as specified by the moniker string) through the WinNT provider. The GetObject() function simply obtains the printer object from the print server. The IADs::Put method writes the value in Textbox3 to the location attribute of the printer object, and the IADs::SetInfo method saves the printer object to the registry. Finally, the page reloads so that you can see the new position on the map. When the DHTML reload method runs, IE prompts you to retry the operation. This step is necessary because information on the page has changed on the print server as a result of the ADSI update. Click Retry to reload the page.

The printer mapping administration utility gives you a powerful and easy way to use pixel offsets to update your printer location values. This tool saves time and lets you use the printer mapping utility I presented in "A Printer Mapping Utility" and "The Code Behind the Printer Mapping Utility" on large networks that have many printers. Be sure to configure the environment I described in the first article before you attempt to implement the printer mapping administration utility. In my experience, network users appreciate seeing and installing printers that are neatly mapped to company floor maps.