Automate complex setup tasks with ScriptIt

How often have you needed to automate a Windows NT task that doesn't have a command-line interface or a scripting solution? Sound driver and modem installations are perfect examples of tasks that run only through a GUI on NT 4.0 systems. In past acts of desperation, I've copied the old macro recorder from my Windows 3.11 system to an NT computer to automate a setup routine.

Fortunately, one of my coworkers found a free tool called ScriptIt on Microsoft's Web site that automates tasks in a GUI and requires minimal configuration. In this article, I'll describe ScriptIt's capabilities and provide examples of how you can use this utility to automate tasks you probably didn't think you could automate.

ScriptIt Does the Hard Work
When you download ScriptIt, you get three files: the ScriptIt executable, a .dll, and documentation that explains how to use the software. ScriptIt takes as input a simple text-based .ini file that contains the commands you want the tool to run and the names of the windows you want the tool to run those commands in. (Because the ScriptIt file is a simple text file, its filename doesn't have to end in .ini.) The utility processes these commands in order until it reaches the end of the file. ScriptIt isn't a programming language, and it doesn't contain any of the usual programming constructs (e.g., looping, conditional statements). However, the commands and functions that ScriptIt supports allow you some creativity. For example, you can include system environment variables when you tell ScriptIt what keystrokes you want to send to a given dialog box. You can also call built-in command shell (DOS) commands and redirect the output of these commands to windows. This functionality lets you make your scripts somewhat dynamic, which is helpful if you need to automate a task that varies slightly depending on the target system's characteristics.

ScriptIt's best feature is its ability to watch and wait for specific windows to appear on your desktop. If you tell ScriptIt to perform a function in a certain window, the tool waits until the window opens to perform that function. When the window opens, ScriptIt sends the keystrokes you specified to the intended dialog box. Keystroke recorders' automation scripts usually break down when a window event occurs out of sequence or when a window that the program doesn't expect pops up. These automation scripts aren't flexible because most keystroke recorders automatically replay your exact keyboard and mouse movements. ScriptIt is intelligent enough to wait for a specific window before performing a particular action.

To install a sound driver, I click Multimedia in Control Panel, select the Devices tab, and click Add. Then I choose a driver from the resulting list or select another destination if my driver isn't in the standard NT source files. After I select the driver, I might need to confirm hardware settings (e.g., IRQ, direct memory access--DMA). These steps might vary depending on the hardware platform I install to because different hardware devices (or even newer versions of the same hardware device) use different sound chips. With ScriptIt, you can use the same .ini file to automatically install drivers for different hardware configurations. You can vary the window names the tool watches for and the keystrokes ScriptIt sends to adapt to the configuration it's installing on.

How the Scripts Work
The ScriptIt documentation describes the options you have within the ScriptIt .ini file. Each .ini file can have two sections: Script and Adlib. You must place these section names in brackets (e.g., \[Script\]), as you do in standard .ini files. The Script section contains your main ScriptIt commands. The optional Adlib section contains commands for your script to perform when unexpected events occur. For example, after you set up a particular service or appli-cation for the first time, different windows or dialog boxes might appear during subsequent setups on the same system. You add commands to the Adlib section to account for these differences within the same setup script. The Adlib section is similar to a primitive exception handler.

After you create your input file, running a script is simple. From a command line or batch file, you call ScriptIt with the name of the input file:

scriptit c:\temp\myfile.ini

Using the Commands
Within the Script section of the input file, you can use five commands: run, runwait, mkfile, REM, and title. The run command initiates an executable program, batch file, or DOS command. The runwait command instructs ScriptIt to wait until a process completes before continuing to the next command. Some setup routines return control to the calling process before the routines complete. If you're trying to automate sequential tasks, ScriptIt's return to the .ini file before a process finishes can cause timing problems; runwait solves those problems. However, use the runwait command with caution. If you tell runwait to wait for a process to complete and the command you're waiting on fails, your script won't complete.

The mkfile command creates files or scripts as the ScriptIt script runs. Mkfile tells ScriptIt to create a file, then passes a line of text to that file. The line of text the software passes to the file is the text you type on the mkfile line. For example, if a ScriptIt script contains the line

mkfile script.log=This script is running now

ScriptIt creates a file called script.log that contains the text This script is running now. If a script.log file already exists, mkfile appends the line of text to the end of the file. You can use mkfile to create a script in your favorite scripting language and call that script from within ScriptIt. For example, if you have the Windows Scripting Host (WSH), you can use mkfile to create a JScript file, then use the run command later in the ScriptIt script to call that file.

You can use REM statements to place comments within your script. ScriptIt doesn't process REM statements.

The Title Command
ScriptIt's title command does most of the tool's work. The title command sends keystrokes to a window that you specify by the window's title. Listing 1 is a sample script that uses the title command to change a workstation's membership from a workgroup to a domain. To manually make this change, you open the Network Control Panel window. (ScriptIt sees the window's title as Network.) The Identification dialog box appears. You click Change to open the Identification Changes window, then enter a domain name (and possibly a domain user ID) to create a new account for your workstation in that domain. ScriptIt lets you automate this process completely. ScriptIt runs the Network Control Panel applet (control ncpa.cpl), then sends the keystrokes you would enter to manually change the workstation's membership to each window that appears. ScriptIt enters c in the Identification property page to open the Identification Changes window. The exclamation point character in the third line represents the Alt key; Alt+D selects the domain radio button.

Listing 1's fourth line demonstrates how ScriptIt uses sendkeys to include special keystrokes. To add special keys (e.g., Tab, Back Space, function, and up and down arrow keys) to ScriptIt files, place braces around the key name. The \{TAB\} command in Listing 1 tabs over in the domain name entry box to the dialog box in which you enter a domain name. Listing 1 enters test as the do-main name.

Adding the tilde character to a ScriptIt file is equivalent to pressing Enter in the open window. I used this character to close the Identification Changes and Network windows after I created the workstation's domain account.

Finding those pesky window titles. Because you identify windows to ScriptIt by their titles, you must make sure you enter the correct window titles when you write the code. ScriptIt requires you to use the same capitalization and spacing between words that the program sees when it runs. The tool provides an option that lets you reveal what the utility sees. Type

scriptit /reveal

from the command line to open a window that shows you the text that ScriptIt sees within the active window. Screen 1 provides an example of the reveal option in action. You must bring a window into focus for ScriptIt to reveal the window's contents. You must lay out your windows on the desktop so that ScriptIt and the active window are visible simultaneously.

You don't need to enter the complete name of a window to identify it. For example, on line three through line five in Listing 1, I can enter Identification instead of Identification Changes. If no other open windows have the name Identification, ScriptIt will find the right window.

If you open two windows with the same title, you can specify which window you want to manipulate when you append to the title command text that the window of interest contains. If I want to access the Network Control Panel window and another window with the name Network is open, I can make the second line of Listing 1

Network+Change=c

This line tells ScriptIt to look for the Network window that contains text that includes the word change.

Hiding windows from users. You can use the title command to hide a window ScriptIt is using. For example, in Listing 1, suppose I don't want a user to see the other Network Control Panel options as I change the workstation's membership. I can add

Network=~winhide

to the script to hide the Network Control Panel window from users' view. Only the Identification Changes window will be visible. You can use similar options to display hidden windows or close or maximize windows.

ScriptIt's Extra Features
ScriptIt supports special characters that let you add functionality to your scripts. For example, you can send the output of a script or internal DOS command to a window if you precede it with an apostrophe character. Listing 2 (a modification of Listing 1), on page 192, changes my computer's workgroup name to a concatenation of the username and computer name, which I separated with an underscore. This line (line 5) uses the DOS echo command to pass the environment variables to the requested window. Also, to populate the dialog, I can use the DOS find command against a file that lists possible workgroup names. In addition, you can use the number sign character as a keystroke separator so you can place multiple keystrokes on one ScriptIt line. Two number sign characters tell ScriptIt to pause for 5 seconds before continuing.

You can use the at sign character to tell ScriptIt to run a WinBatch function. WinBatch is powerful batch-file enhancement and scripting software from Wilson WindowWare. For more information about WinBatch, download the documentation for Windows Interface Language (WIL--the language WinBatch uses) from Wilson WindowWare's Web site.

Sampling the Power of ScriptIt
Listing 3 is a script that automates creating a new Line Print Remote (LPR) printer port and HP LaserJet 4 Plus shared printer definition on an NT workstation. You can create an LPR port only from the GUI. Listing 3 illustrates many of ScriptIt's features. Although this script appears long and complicated, it performs simple tasks.

Listing 3 contains many REM statements. Adding comments to your scripts always helps other users interpret them. The script includes many examples of ScriptIt's keystroke separator and 5-second delay options. The keystroke separator saves me from repeatedly typing each window title. I use the 5-second delay frequently because without the delay, Listing 3's script processes too quickly for ScriptIt to keep up. I never use the keystroke separator and 5-second delay on the same line so that the window controls can keep up with script processing. Listing 3 uses the mkfile command to create a temporary batch file that echoes the name of the computer the script runs on. Listing 3 then uses the apostrophe to call the batch file to enter the computer name in a dialog box.

To execute this script, which I named addport, I type

scriptit addport

from a command line or Start menu Run command. A ScriptIt icon on the taskbar lets you keep track of the script. You can right-click this icon to terminate your script before it completes. When you terminate a script prematurely, ScriptIt leaves a temporary file in your %temp% directory. To find these files, look for numbered files without extensions. You must delete these files manually.

A Power Tool for Automating That Last 10 Percent
You can script approximately 90 percent of NT configuration tasks from the command line through a combination of built-in commands, resource kit utilities, and third-party scripting tools. The remaining tasks don't have command-line support, so you must perform them from the GUI. ScriptIt is the ideal tool for these kinds of tasks. ScriptIt is also ideal if you're trying to automate an NT installation on hundreds of systems: It lets you script tasks you can't otherwise script (e.g., sound driver installations) so your NT installation is as hands-off as possible. Using NT 4.0's unattended

installation process, you can include a ScriptIt script to install sound drivers during the initial workstation installation using the runonce option. This option prevents you from returning to each workstation and installing sound drivers manually. ScriptIt provides ease of use (no programming is necessary), flexibility, and extensibility. ScriptIt is one of the more interesting tools to come out in a while.

Contact Info
ScriptIt
Microsoft * 800-426-9400
Web: http://www.microsoft.com

WinBatch
Wilson WindowWare * 800-762-8383
Web: http://www.windowware.com