Subject: For WSH people
From: furbs (furbs@swjedi.net)
Date: Tue Apr 01 2003 - 12:18:37 AKST
I know, this shouldn't really be posted to this group but I figure some
people here actually use Windows to do some stuff. If you do much WSH
scripting, then I have 2 scripts attached that may be of interest.
Progress.vbs:
This is a typical progress bar that I found online somewhere. I have
modified it so it looks a little bit better and displays the actual Percentage
on the middle of the bar. It uses IE, and uses tables to make the bar. Pretty
easy to use with, and easy to implement with this example/skeleton script.
Alert.vbs
This is something I made starting originally with the Progressbar
structure. I have completely modified the way it works to be used as an error
handler. It is pretty much a function that is called with an error code sent to
it. You have to manually tell it what to say for each error code, and currently
has 4 modes.
0 or False) Null Error (In case it was called on accident)
1-99) "Continue" or "Cancel" case. (Standard Range)
100-199) "Yes" or "No" case. (Confirmation Range)
200-299) Fatal Error (Fatal Range, stop script)
I have found this to be an excellent way to handle custom errors, or simply
handling possible events to prevent errors from occurring in the first place.
Obviously if you wanted this script to handle Err.Number codes, that could
easily be done as well. But currently it is set to only allow codes up to 300.
Both of these scripts I have tried to comment as much as possible without the
comments making it difficult to understand the programs. Both have also been
modified so that they always have the focus. Because they are both
example/skeleton files, they can be run as they are to see them work. Remove
the '.txt' to use them.
Brian ThunderEagle
- http://www.swjedi.net
- furbs@swjedi.net
- bthundereagle@aidea.org
-- Attached file included as plaintext by Listar --
-- File: Progress.vbs.txt
' ==============================================================
' Created By: Brian ThunderEagle
' Progress Bar v.4
' Original Author: Unknown
' ==============================================================
' ======================== DECLARATIONS ========================
Dim gdocProgressBar
Dim goieProgressBar
Set gdocProgressBar = Nothing
Set goieProgressBar = Nothing
Dim Progress_Amount
Dim Objects
' ======================== DECLARATIONS =========================
' ===============================================================
' ============================= MAIN ============================
' How many objects are being processed?
' Whats 'the step' for the percentage bar.
Objects=150
Progress_Amount=Objects
the_step=1
if (Progress_Amount = 0) then
the_step=100
End If
if (Progress_Amount > 100) then
the_step=(100/Progress_Amount)
End If
if (Progress_Amount < 100 and Progress_Amount > 0) then
the_step=(100/Progress_Amount)
End If
Current_Load=0
' Show the ProgressBar and start going through
' the objects.
ProgressBar Current_Load
For processing = 1 to Objects
' Now our script is doing stuff...
Current_Load=Current_Load+the_step
ProgressBar Current_Load
Next
ProgressBar 101
wscript.quit
' ============================= MAIN ============================
' ===============================================================
' ============================= SUBS ============================
Sub ProgressBar(intPercent)
'Requires global declaration for gdocProgressBar and goieProgressBar
'Creates an html page showing a status bar.
'intPercent must be between 0 and 100.
'If intPercent is out of range, the status bar is shut down.
'Create the status bar window
If gdocProgressBar Is Nothing Then
If ((Cint(intPercent) >= 0) And (Cint(intPercent) <= 100)) Then
Set goieProgressBar = CreateObject("InternetExplorer.Application")
goieProgressBar.Offline = True
goieProgressBar.AddressBar = False
goieProgressBar.Height = 110
goieProgressBar.Width = 250
goieProgressBar.MenuBar = False
goieProgressBar.StatusBar = False
goieProgressBar.Silent = True
goieProgressBar.ToolBar = False
goieProgressBar.Navigate "about:blank"
Do While goieProgressBar.Busy
WScript.Sleep 100
Loop
'On Error Resume Next
Set gdocProgressBar = Nothing
Do Until Not gdocProgressBar Is Nothing
WScript.Sleep 100
Set gdocProgressBar = goieProgressBar.Document
Loop
gdocProgressBar.Open
gdocProgressBar.Write "<html><head><title>"
gdocProgressBar.Write "Status"
gdocProgressBar.Write "</title></head><body onBlur='self.focus()' onload='self.focus()'><center>"
gdocProgressBar.Write "<TABLE width='200' border='3' frame='box' style='z-index:1' BGCOLOR='c0c0c0'><tr><td align=center style='position:relative'>"
gdocProgressBar.Write "<font name='reportPercent' id='reportPercent' color='LightBlue' style='z-index:150; position:absolute; left:78'>0%</font><font> </font>"
gdocProgressBar.Write "<TABLE align='left' style='position:absolute; left:1px; z-index:100' width='100%' border='0' cellpadding='0' cellspacing='0' bgcolor='#FFFFFF'>"
gdocProgressBar.Write "<tr><td name='status' width='0%' id='status' bgcolor='Green'> </td><td name='status' width='100%' id='status2' bgcolor='red'></td></tr></table></td></tr></table></center></body></html>"
gdocProgressBar.Close
goieProgressBar.Visible = True
Else
Exit Sub
End If
End If
'Close the status bar window
If Not gdocProgressBar Is Nothing Then
If ((Cint(intPercent) < 0) Or (Cint(intPercent) > 100)) Then
goieProgressBar.Visible = False
Set gdocProgressBar = Nothing
goieProgressBar.Quit
Set goieProgressBar = Nothing
Exit Sub
End If
End If
'Update the status bar window
If Cint(intPercent) = 0 Then
gdocProgressBar.all.status.width = "1%"
gdocProgressBar.all.status.bgcolor = "red"
gdocProgressBar.all.status2.width = "100%"
Else
gdocProgressBar.all.status.width = Cstr(Cint(intPercent)) & "%"
gdocProgressBar.all.reportPercent.innerHTML = ""&Cstr(Cint(intPercent))&"%"
gdocProgressBar.all.status2.width = (101-Cstr(Cint(intPercent))) & "%"
gdocProgressBar.all.status.bgcolor = "Green"
End If
End Sub
' ============================= SUBS ============================
' ===============================================================
-- Attached file included as plaintext by Listar --
-- File: Alert.vbs.txt
' ==============================================================
' Created By: Brian ThunderEagle
' Custom Error Handling : Alert Script v.11
' Adaptation from ProgressBar
' ==============================================================
' ======================== DECLARATIONS ========================
Dim OieDocAlertWindow
Dim OieAlertWindow
Dim myError
Dim Getting_Error
Dim Continue
Dim sEvent
Continue = True
Getting_Error = False
Set OieDocAlertWindow = Nothing
Set OieAlertWindow = Nothing
Set OieAlertWindow = wscript.CreateObject("InternetExplorer.Application")
Err.Clear
On Error Resume Next
' ======================== DECLARATIONS =========================
' ===============================================================
' ============================= MAIN ============================
'NOTE: This will call the Alert Box, but not provide an error code
' this results in a "null" error. It will report that no error
' has really occurred, and you have the option to continue.
GiveAlert myError
if Continue=False then
wscript.quit
else
sEvent=""
myError=False
Set OieDocAlertWindow = Nothing
Set OieAlertWindow = Nothing
end if
counting=0
For counting = 1 to 100000
' If these things happen, make this error Code.
if counting=1000 then
'NOTE: This will call the Alert Box, and is in the range of 1 to 99
' which is the standard Error range. This gives the user
' "Continue" and "Cancel" options.
myError=1
GiveAlert myError
if Continue=False then
wscript.quit
else
sEvent=""
myError=False
Set OieDocAlertWindow = Nothing
Set OieAlertWindow = Nothing
end if
elseif counting=20000 then
'NOTE: This will call the Alert Box, and is in the range of 100 to 199
' which is the confirmation range. The user must answer with "Yes"
' or "No".
myError=101
GiveAlert myError
if Continue=False then
wscript.quit
else
sEvent=""
myError=False
Set OieDocAlertWindow = Nothing
Set OieAlertWindow = Nothing
end if
elseif counting=50000 then
'NOTE: This will call the Alert Box, and is in the range of 200 to 299
' which is the Fatal error range. The user can only quit the script
' at this point.
myError=201
exit for
end if
next
' If we have received an error somewhere, show it
if myError then
GiveAlert myError
if Continue=False then
wscript.quit
end if
myError=False
Set OieDocAlertWindow = Nothing
Set OieAlertWindow = Nothing
end if
'wscript.echo Continue
' ============================= MAIN ============================
' ===============================================================
' ============================= SUBS ============================
Function GiveAlert(intMyCode)
' We need to re-open a new InternetExplorer object and tell our program we are
' currently waiting for a user response ( Getting_Error=True )
Set OieAlertWindow = wscript.CreateObject("InternetExplorer.Application")
Getting_Error = True
' At the moment, we don't want the program to continue unless the event
' specified tells us specifically to continue.
Continue = False
'Create the status bar window
If OieDocAlertWindow Is Nothing Then
If ((Cint(intMyCode) >= 0) And (Cint(intMyCode) <= 300)) Then
OieAlertWindow.Offline = True
OieAlertWindow.AddressBar = False
OieAlertWindow.Height = 225
OieAlertWindow.Width = 300
OieAlertWindow.MenuBar = False
OieAlertWindow.StatusBar = False
OieAlertWindow.Silent = True
OieAlertWindow.ToolBar = False
OieAlertWindow.Navigate "about:blank"
Do While OieAlertWindow.Busy
WScript.Sleep 100
Loop
'On Error Resume Next
Set OieDocAlertWindow = Nothing
Do Until Not OieDocAlertWindow Is Nothing
WScript.Sleep 100
Set OieDocAlertWindow = OieAlertWindow.Document
Loop
OieDocAlertWindow.Open
OieDocAlertWindow.Write "<html><head><title>"
OieDocAlertWindow.Write "Error"
OieDocAlertWindow.Write "</title></head>"
OieDocAlertWindow.Write "<STYLE TYPE = 'text/css'>"
OieDocAlertWindow.Write "BODY {FONT-FAMILY: 'Arial','Verdana';}"
OieDocAlertWindow.Write "FONT.1 {FONT-SIZE: 12pt; COLOR:'red'}"
OieDocAlertWindow.Write "FONT.2 {FONT-SIZE: 10pt; COLOR:'black'}"
OieDocAlertWindow.Write "</STYLE><body onBlur='self.focus()' onload='self.focus()'><center>"
OieDocAlertWindow.Write "<TABLE width='100%' height='100%' border='1' frame='box' BGCOLOR='c0c0c0'>"
OieDocAlertWindow.Write "<tr height='20%'><td colspan='2'><font class='1' name='errorcode' id='errorcode'>Error: Null</font></td></tr>"
OieDocAlertWindow.Write "<tr height='60%'><td colspan='2'><FONT class='2' id='theMessage' name='theMessage'> </FONT></td></tr>"
If Cint(intMyCode) < 100 then
OieDocAlertWindow.Write "<tr height='20%'><td width=50% align=center><input type='submit' value='Continue' name='cmdContinue'></td>"
OieDocAlertWindow.Write "<td width=50% align=center><input type='submit' value='Cancel' name='cmdCancel' onclick='window.close(this);'>"
OieDocAlertWindow.Write "</td></tr></table></center></body></html>"
Set OieAlertWindow.Document.all.cmdCancel.OnClick = GetRef("cmdCancel_Click")
Set OieAlertWindow.Document.all.cmdContinue.OnClick = GetRef("cmdContinue_Click")
Set OieAlertWindow.Document.body.OnUnload = GetRef("cmdCheck_Click")
Elseif Cint(intMyCode) > 199 then
If Cint(intMyCode) < 300 then
OieDocAlertWindow.Write "<tr height='20%'><td colspan='2' align='center'>"
OieDocAlertWindow.Write "<input type='submit' value='Quit' name='cmdCancel' onclick='window.close(this);'>"
OieDocAlertWindow.Write "</td></tr></table></center></body></html>"
Set OieAlertWindow.Document.all.cmdCancel.OnClick = GetRef("cmdCancel_Click")
Set OieAlertWindow.Document.body.OnUnload = GetRef("cmdCancel_Click")
End If
Elseif Cint(intMyCode) > 99 then
If Cint(intMyCode) < 200 then
OieDocAlertWindow.Write "<tr height='20%'><td width=50% align=center><input type='submit' value='Yes' name='cmdYes'></td>"
OieDocAlertWindow.Write "<td width=50% align=center><input type='submit' value='No' name='cmdNo' onclick='window.close(this);'>"
OieDocAlertWindow.Write "</td></tr></table></center></body></html>"
Set OieAlertWindow.Document.all.cmdYes.OnClick = GetRef("cmdYes_Click")
Set OieAlertWindow.Document.all.cmdNo.OnClick = GetRef("cmdNo_Click")
Set OieAlertWindow.Document.body.OnUnload = GetRef("cmdCheck_Click")
End If
End If
OieDocAlertWindow.Close
OieAlertWindow.Visible = True
' This cannot be called, because it will report the process
' as cancelled when the error window is closed by default
' ie: OieAlertWindow.close
' Set OieAlertWindow.Document.body.OnUnLoad = GetRef("cmdCancel_Click")
Else
Exit Function
End If
End If
' Close the status bar window
' If an error code was given out of our range the error window is hidden
' at this point. This is a default action, and can be removed or modifed
' for expandability. For use with Standard Err.Number codes, this section
' MUST be commented out. Otherwise myError 101 hides and closes the error box.
'If Not OieDocAlertWindow Is Nothing Then
' If ((Cint(intMyCode) < 0) Or (Cint(intMyCode) > 300)) Then
' OieAlertWindow.Visible = False
' Set OieDocAlertWindow = Nothing
' OieAlertWindow.Quit
' Set OieAlertWindow = Nothing
' Exit Function
' End If
'End If
' What does the Error number we got mean?
' If we didn't get an error number or if the number has been
' undefined, then tell the user "There was no error" and
' "There was an unknown error" respectively.
If Cint(intMyCode) = False Then
OieDocAlertWindow.all.errorcode.innerHTML = "Error: "&intMyCode
OieDocAlertWindow.all.theMessage.innerHTML = "No error occurred, this has been called by mistake."
ElseIf Cint(intMyCode) = 101 Then
OieDocAlertWindow.all.errorcode.innerHTML = "Error: "&intMyCode
OieDocAlertWindow.all.theMessage.innerHTML = "Something is buggin me...<br>Are you sure you want<br>to do this?"
ElseIf Cint(intMyCode) = 201 Then
OieDocAlertWindow.all.errorcode.innerHTML = "Error: "&intMyCode
OieDocAlertWindow.all.theMessage.innerHTML = "Fatal Error has occurred. Script cannot continue."
Else
OieDocAlertWindow.all.errorcode.innerHTML = "Error: "&intMyCode
OieDocAlertWindow.all.theMessage.innerHTML = "An unknown error occurred!<br><br>Continuing may produce further<br>errors."
End If
' While Getting_Error is true, lets look for what the user
' wants to do or what the program has told us to do.
' This will loop forever until the user either closes the Alert Box
' or clicks a button on the Alert Box.
Do While Getting_Error
wscript.sleep 100
select case sEvent
case "Cancel"
OieAlertWindow.quit
Continue=False
Getting_Error=False
wscript.sleep 100
Exit function
case "Continue"
OieAlertWindow.quit
Continue=True
Getting_Error=False
wscript.sleep 100
Exit function
case "Yes"
OieAlertWindow.quit
Continue=True
Getting_Error=False
wscript.sleep 100
Exit function
case "No"
OieAlertWindow.quit
Continue=False
Getting_Error=False
wscript.sleep 100
Exit function
End Select
loop
end function
' Was the user supposed to close this window yet?
Sub cmdCheck_Click()
if sEvent = "" then
Set OieDocAlertWindow = Nothing
Set OieAlertWindow = Nothing
GiveAlert myError
End If
End sub
' What should the sEvent be for what the user clicked.
Sub cmdCancel_Click()
sEvent = "Cancel"
End sub
Sub cmdContinue_Click()
sEvent = "Continue"
End Sub
Sub cmdYes_Click()
sEvent = "Yes"
End Sub
Sub cmdNo_Click()
sEvent = "No"
End Sub
' ============================= SUBS ============================
' ===============================================================
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
This archive was generated by hypermail 2a23 : Tue Apr 01 2003 - 12:20:53 AKST