Recently, I got tired of using...
WScript.Echo( variable_name );
...as my main debugging tool. Visual Studio provides a full-featured script debugger, and after working with it on a few tasks, I've been kicking myself for not looking into this sooner.
I'll be talking about JScript (.JS files), but most of this article also applies to VB Script (.VBS files) debugging.
Setting Up for Quick Debugging
For me, the whole point of using JScript is that it's all contained in a simple text file. I don't want to set up a "Solution" or have piles of reference DLLs or anything like that. I just want to work with a text file that I can tweak until it works and modify at a whim using Notepad. So, here is how I set up to debug .JS files:
In Windows Explorer, use the Tools / Folder Options
command to make a new "Action" for .JS files:
Locate the JS - JScript Script Files
file entry under the File Types
tab. Select it and click the Advanced
button. Click New...
Action: Debug This Script
Application: C:\WINDOWS\System32\WScript.exe //d //x "%1"
The //d //x
tell WScript to debug the program and to automatically break at the first statement.
OK the changes and close the Folder Options window. Viola!
Now you can right-click on any .JS file in Windows Explorer and you have a new Debug This Script
option in the menu.
So What's Next?
When you start the debugging process, you are prompted with "Do you want to debug using the selected debugger?" Click Yes. Up comes Visual Studio, showing your script stopped at the first executable statement.
Now you have all of the features of Visual Studio at your command: Press F10
to single step. Hover the mouse over a variable
to get the variable's current value, and to see an Intellisense list of object members.
You can even modify variable values using the tiny popup hover box.
And you can use QuickWatch and the Watch window to do real
This is where I get the most value: Visual Studio shows methods and attributes (member functions and member variables) of ActiveX controls, as well as local program variables and objects that you create yourself. For native values -- primitive variables declared in your code, you can use the QuickWatch and Watch windows to modify values -- set true to false, or modify a numeric value, etc.)
Scroll down and put a break point in the script, and press F5
to continue execution to that point. You also have the Immediate Window available -- If it's not visible as a tab at the bottom, use the menu command: Debugging / Windows / Immediate
to get to it.
The Immediate Window lets you type in JScript commands such as:
...so it provides a way to modify any program variable and even create new variables while you step through the code.
The "Restart Program" command is not available. I just right-click on the .JS file and select "Debug This Script" again... this time selecting the existing instance of Visual Studio when prompted.
VS displays a "Dynamic Read-Only" version of your script file while debugging, so how can you make changes to your code? What I do is drag the JS file into VS so I have a read/write copy of it available. Just remember to save the changes before restarting the debugging process.
Scripts are real programs, too! Using the steps described in this article, you should be able to develop your scripts faster and and be more productive sooner. I particularly like the Intellisense quick lists of ActiveX member functions -- it's way
easier than than digging through the documentation.
If you liked this article
and want to see more from this author,
please click the Yes
button near the:
Was this article helpful?
label that is just below and to the right of this text. Thanks!