[Previous] [Table of Contents] [Next]

Editing Scripts

You can use Notepad to create and edit script files. To load an existing script file into Notepad, right-click on the file and choose Edit from the shortcut menu. (The Edit command is added automatically when you install WSH.)

Unfortunately, Notepad provides only rudimentary features for editing text files. The real problem occurs during script debugging: WSH parses the source code, and if it finds a faulty statement or if a run-time error occurs, an error dialog box appears, indicating the error code and a line number, as shown in Figure 2-4.

Figure 2-4 An error dialog box that appears during script execution (in WSH 2)

In this situation, you must edit the script file. Unfortunately, it can be difficult to locate the faulty statement using Notepad because you have to count the source code lines manually. This isn't feasible with lengthy scripts. You can use any of the script editors described a little later in this chapter, all of which support line numbering, to handle this chore for you.

NOTE
Only the version of Notepad in Windows 2000 includes the Go To command, which allows you to jump to a specific line within a text file.

Invoking Script Editors

If you want to use your own script editor instead of the default (Notepad), you must launch it and load the script file. It's handy to have a custom command on the file's shortcut menu to invoke your script editor. Figure 2-5 shows the EditPlus custom shortcut command, which opens a .vbs or .js file in the EditPlus program.

Figure 2-5 Shortcut menu with a custom command that invokes a script editor

To add a custom shortcut menu command, take the following steps:

  1. Open a folder window. Choose Folder Options from the View menu (in Windows 95 or Windows 98) or from the Tools menu (in Windows 2000) to open the Folder Options dialog box.
  2. On the File Types tab (shown in Figure 2-6), select your script file in the Registered File Types list, and then click Edit (in Windows 95, Windows 98, or Windows NT 4) or Advanced (in Windows 2000).
  3. Figure 2-6 The Folder Options dialog box

  4. In the Edit File Type dialog box that appears (Figure 2-7), click New to open the New Action dialog box (Figure 2-8).
  5. Figure 2-7 The Edit File Type dialog box

    Figure 2-8 The New Action dialog box

  6. Type the name of the new shortcut command in the Action text box, and type the command (the path and the EXE filename to invoke the application) in the Application Used To Perform Action text box. Characters such as %1 are placeholders for the current file (which will be expanded during command execution). You can use the Browse button to select the program file in a folder window.
  7. Close all the dialog boxes to register the new command.

NOTE
Be sure to register file types (.vbs, .js, and perhaps .wsf) for scripts. For more information on registering file types and commands, see my book Inside the Microsoft Windows 98 Registry (Microsoft Press, 1998).

Some of the available script editors are described next. A number of them are available free.

PrimalSCRIPT

PrimalSCRIPT is a powerful script editor for Windows developed by SAPIEN Technologies, Inc. If you're developing WSH scripts or scripts for different languages, PrimalSCRIPT might be your first choice. It provides a consistent user interface and development environment for several scripting languages. Version 1 was designed to support simple script editing. It allows you to insert code snippets (ForTo, IfThenElse, and so on) into the source code for all supported languages.

PrimalSCRIPT version 2, shown in Figure 2-9, is perfectly suited to WSH script development. It supports line numbering (to identify a line in an error dialog box), a type library viewer, and much more. You can edit .wsf files that contain several jobs or script elements. When you select a script element, the editor will show only the element's code—the element definition and attributes are treated as properties. PrimalSCRIPT 2 also handles well-formed Extensible Markup Language (XML) documents. Debugging is also simplified in PrimalSCRIPT 2. The program supports in-place debugging for WSH script files, so you can execute .js, .vbs, and .wsf files from the editor window or pass control to the script debugger.

Click to view at full size.

Figure 2-9 PrimalSCRIPT 2

NOTE
To learn more about advanced XML topics such as well-formed documents, see my book Advanced Development with Microsoft Windows Script Host 2.0.

Because PrimalSCRIPT 2 supports type libraries, it can incorporate sophisticated editing features. For example, when you enter an object keyword, a ToolTip window shows you the definitions of the object's methods or properties (similar to Microsoft development environments such as Visual Basic for Applications [VBA]).

You can download a 30-day evaluation copy of PrimalSCRIPT 2 from http://www.sapien.com. This site also contains a "Script Exchange" section from which you can get new sample scripts.

NOTE
PrimalSCRIPT is my favorite script editor, and not just because it's simple to use and has features for creating and debugging scripts. When you create .wsf files, PrimalSCRIPT splits the content of <script> elements into separate "units" and hides all XML definitions surrounding the pure code. When you save the file, PrimalSCRIPT automatically adds the XML structures.

Microsoft Script Editor

You can also use Microsoft Script Editor, which ships with Microsoft Office 2000, to edit script files. To install Script Editor, you need to use the Office 2000 installer. Just follow these steps:

  1. From Control Panel, double-click Add/Remove Programs.
  2. Click the Change Or Remove Programs button in the Add/Remove Programs dialog box.
  3. Search for Microsoft Office, and then click the Change button. In the Microsoft Office 2000 Maintenance Mode dialog box, click Add Or Remove Feature.
  4. Select Microsoft Office/Office Tools/HTML Source Editing/Web Scripting, and set the option to Run From My Computer.
  5. Click the Update Now button.

Windows will install Script Editor from the Office CD.

After installing Script Editor, but before launching it, you need to enable script file support (for example, syntax color highlighting). To enable file support for script files (.vbs, .js, and .wsf), you need to update the Registry.

  1. Launch Registry Editor (Regedit.exe), and search for the Registry key HKEY_LOCAL_MACHINE\Software\Microsoft\MSE\9.0\Editors\{C76D83F8 -A489-11D0-8195-00A0C91BBEE3}\Extensions. This key contains the settings for the files already supported.
  2. Add the DWORD value vbs, and set its value to 28 hexadecimal. (See Figure 2-10.)
  3. Click to view at full size.

    Figure 2-10 Registry entries for Script Editor

  4. Repeat step 2, and add DWORD values for js and wsh.

After closing Registry Editor, you can try to use Script Editor. There are two ways to launch Script Editor (also called the Microsoft Development Environment):

After launching Script Editor, you're ready to load and edit script files.

You can use the Define Window Layout command on the View menu to choose one of the predefined editor layouts. Figure 2-11 shows the Design mode with the Project Explorer window in the left pane and the code windows in the right pane.

Click to view at full size.

Figure 2-11 Script Editor with script code

To load and view a script file, simply drag it to the Project Explorer window (or use the Open File command on the File menu). The names of files already loaded are listed in the Project Explorer window (Figure 2-11, left pane).

You can reload a file by double-clicking its entry in the Project Explorer. To use Script Editor effectively for script development, you need to know a few tricks:

You can identify the line number you're on by clicking on the line. The line number is shown in the status bar. To create a new file, you can use the New File command on the File menu or press Ctrl+Shift+N to invoke the New File dialog box. (This dialog box also allows you to load existing and recent files.) In Chapter 1, I mentioned that you can create your own template files for .js, .vbs, and .wsf files. Just copy these template files into the Program Files\Microsoft Visual Studio\Common\IDE\IDE98\NewFileItems folder. Then, on the New tab of the New File dialog box, you can select one of those templates to create a new VBScript, JScript, or Windows Script file.

Other editors

EditPlus is a 32-bit text editor for Windows 95, Windows 98, Windows NT 4, and Windows 2000 that is distributed as shareware. EditPlus is simple to use and provides a syntax-highlighting feature for HTML, C/C++, Perl, and Java that you extend to support other languages. The best feature of EditPlus is a toolbar button that enables line numbering in the loaded text file. You can download a 30-day evaluation copy of EditPlus from http://www.editplus.com.

EditPad is a small replacement for Notepad that you can use to edit text files in different formats. EditPad doesn't support line numbering, but it has a Go To Line command that allows you to enter the line number and can be a useful addition to Windows 95, Windows 98, and Windows NT 4. (The Windows 2000 version of Notepad already has a Go To command, so EditPad doesn't offer any advantages to Windows 2000 users.) You can download EditPad at no charge from http://www.jgsoft.com.

UltraEdit-32 is a powerful editor for Windows 95, Windows 98, Windows NT, and Windows 2000 that is available as shareware. The program supports the editing of text files with different filename extensions (.bat, .ini, .html, and more) and offers several features for script editing, such as color highlighting of keywords. In the code window, you can activate line numbering. You can download a 45-day evaluation copy of UltraEdit-32 from http://www.ultraedit.com. The site provides links for downloading several versions of UltraEdit.

Other editors that you can use for script editing (but which I haven't tried) include CodeMagic, TextPad, and NoteTab. CodeMagic is a scripting integrated development environment (IDE) that is free (in its first version) and fairly customizable. You can download it from http://www.petes-place.com/codemagic.html. TextPad and NoteTab are shareware editors. NoteTab also comes in a Light version that's freeware. Neither editor supports text color highlighting. You can download TextPad from http://www.textpad.com and NoteTab from http://www.notetab.com.

TIP
You can use the Print shortcut command to print the source code of your script files. If you use one of the script editors described in this chapter, you can also print the source code with line numbers, which can be helpful during the debugging process.