Sunday, October 3, 2010

I've become quite fond of VBScript over the years despite it's many quirks and limitations, most of which are counter balanced by the fact that all you need to create something on the fly is Notepad and a good memory, but there are still a few "features" I've always missed.

I personally started my path to programming with VB6, so one of those oft-missed features is the Immediate Window; a wonderful little tool that lets you keep track of the current values of variables and expressions while your program is running. Kind of a behind-the-curtain view of what's going on. The best part about the Immediate Window was that it let the program execute uninterrupted but could give you a kind of history of what transpired, if you took the time to set it up correctly.

With VBScript, on the other hand, the most commonly used method is to throw values into a MsgBox for debugging and then either delete the line, or comment it out when you're done. While this quick and dirty approach is fine and functional for simple or short scripts, when you start to work on scripts with hundreds or thousands of lines it leaves quite a bit to be desired. Not only can the large number of Msgbox pops annoy you to death with having to click "ok" dozens of times, the effort involved to turn your debugging "on" or "off" can be downright enervating. If you've ever found yourself clicking "ok" for ten minutes straight you know where I'm coming from.

So, after dealing with this for a couple of years and just accepting it as simply par for the course, I found myself with a need to write a script for an asp site that pulled a DB query and then presented the results in a table on a web page, and the lights came on. Why not use the same principle to make a reasonable facsimile of an Immediate Window? To my chagrin I found that others had come to the same conclusion long ago (hey, I'm a dabbler) but what I found wasn't exactly what I was looking for, so I wrote my own variant.

The main benefits to using this process are:
1) all of your debug information is pushed to a single screen for easy tracking
2) Your script is not interrupted by the process so test runs complete more quickly
3) You can build your debugging routine and leave it all in place within the script, turning it on or off with only a single change.

The example below includes the function, the necessary variable declarations and some usage samples. Key points: you have to declare the objIE, objDoc and popDebug variables in the main part of the script (globally) for this to work properly. Set the value of popDebug to true to turn debugging on, and false to turn it off.

'-----------------Sample Script-----------------------------------
Dim objIE, objDoc, popDebug
popDebug = True

pop("This")
pop("That")
pop("other things")

function pop(strText)
if popDebug = True then
if Not IsObject(objIE) then
Set objIE = wscript.CreateObject("InternetExplorer.Application")
objIE.Navigate "about:blank"
objIE.Visible = 1
objIE.ToolBar = False
objIE.Width = 400
objIE.Height = 500
Set objDoc = objIE.Document
objDoc.Open
'objDoc.Writeln "<HEAD><TITLE>Immediate Window - Debugger</TITLE></HEAD>"
end if
objDoc.Writeln strText & "<br/>"
end if
end function
'-----------------Sample Script-----------------------------------


Enjoy,

No comments: