VBScript to Display Countdown using Vbscript Graphical Elements - vbscript

I wanted to display a MessageBox which displays countdown from 10 to 1 and autocloses after 10 seconds. As Msgbox in vbscript passes code execution untill the user acts on it i tried it using Popup in Wscript Shell Object
Dim counter
Dim oShell
counter = 10
Set oShell= CreateObject("Wscript.Shell")
While counter > 0
oShell.Popup " Left " & counter & " Seconds",1,"Remind"
counter = counter-1
Wend
But it auto-closes for every second and opens a new popup is there any way i can display the countdown and autoclose using the available GUI elements in vb script

Afraid not, the popup is modal & can't be interacted with while its displayed so there is no way to update its existing content.
If you want a more flexible UI you will need to use something different, the console or HTML in an HTA.

You can use Internet Explorer to create a non-modal display.
Set oIE = CreateObject("InternetExplorer.Application")
With oIE
.navigate("about:blank")
.Document.Title = "Countdown" & string(100, chrb(160))
.resizable=0
.height=200
.width=100
.menubar=0
.toolbar=0
.statusBar=0
.visible=1
End With
' wait for page to load
Do while oIE.Busy
wscript.sleep 500
Loop
' prepare document body
oIE.document.body.innerHTML = "<div id=""countdown"" style=""font: 36pt sans-serif;text-align:center;""></div>"
' display the countdown
for i=10 to 0 step -1
oIE.document.all.countdown.innerText= i
wscript.sleep 1000
next
oIE.quit

I have a code, but it might not help
dTimer=InputBox("Enter timer interval in minutes","Set Timer") 'minutes
do until IsNumeric(dTimer)=True
dTimer=InputBox("Invalid Entry" & vbnewline & vbnewline & _
"Enter timer interval in minutes","Set Timer") 'minutes
loop
flop=InputBox("What do you want to set the timer for?")
if dTimer<>"" then
do
WScript.Sleep dTimer*60*1000 'convert from minutes to milliseconds
Set Sapi = Wscript.CreateObject("SAPI.spVoice")
Sapi.speak "Time's up."
t=MsgBox(flop & vbnewline & vbnewline & "Restart Timer?", _
vbYesNo, "It's been " & dTimer &" minute(s)")
if t=6 then 'if yes
'continue loop
else 'exit loop
exit do
end if
loop
end if
please excuse my weird variable names.
This code is from https://wellsr.com/vba/2015/vbscript/vbscript-timer-with-wscript-sleep/

It actually is possible to edit the countdown, very easily for VBScript, so I don't understand what the problem is.
Here's the code:
Dim counter
Dim oShell
counter =InputBox("")
#if you add =InputBox you can have the choice to type in the amount of times the popup repeats#
Set oShell= CreateObject("Wscript.Shell")
While counter > 0
oShell.Popup " Time until shutdown " & counter & " Seconds",(1),"[3]Critical Viruses Detected
#and if you change the (1) to whatever number you want then you can change how fast the popup repeats and if you join this with the "open command" then you can make a gnarly nondangerous virus#
counter = counter-1
Wend

Related

Windows shell script not able to make existing IE window visible?

I have been trying to create a really simple script to maintain visibility on an IE page but am currently unable to force the visibility or foreground position of an existing IE window.
I am able to successfully activate the window using WshShell.AppActivate() but it does not make the page foreground to a user. A sample from my code is below.
Basically the code loops until the user ends the notepad window. my confusion is specifically with why IE is not visible no matter what commands I send
Dim pressX
Dim FindProc
pressX = 1
' Create WScript Shell Object to access filesystem.
Set WshShell = WScript.CreateObject("WScript.Shell")
'WshShell.Run "NoScreenSaver.txt", 2, 0
' Define Which process to seek for to terminate script
strComputer = "."
FindProc = "NoScreenSaver"
Dim objApp : Set objApp = CreateObject("Shell.Application")
Dim IE : Set IE = Nothing
Dim objWindow
' Loop start
Do
' Wait for 5 minutes
WScript.Sleep 3000 '000
For Each objWindow In objApp.Windows
If (InStr(objWindow.Name, "Internet Explorer")) Then
Set IE = objWindow
Exit For
End If
Next
' check if specific notepad closed
If WshShell.AppActivate("NoScreenSaver.txt") = True Then
'WshShell.AppActivate("iexplore.exe")
'wscript.echo FindProc & " is running"
Else
wscript.echo FindProc & " is not running" & vbCrLf & "Script will now end"
pressX = 0
End If
IE.visible = True
wshshell.appactivate ie.name
IE.navigate "google.ca"
' Send dummy key
WshShell.SendKeys "{F13}"
' repeat
Loop While pressX
I never wanted to use AppActivate to check for the specific notepad window either since it would take foreground. There is some information regarding using a COM wrapper but the COM wrapper approach is not possible within my constraints.

Turn off a VBScript program while it is running

(This is a .vbs program) I am trying to make a message bot for a program but I came up with a problem where if you set the time for too long, and set number of messages too high, you can't stop the program easily and have to deal with your message being typed over and over again until it is finished. Is there a way to add an option while it is running to stop the message from being typed?
Here is the code:
set shell = createobject ("wscript.shell")
strtext = inputbox ("Type the message you like to type")
strtimes = inputbox ("How many times would you like to type this message?")
strspeed = inputbox ("How fast do you like to message? (1000 = one per sec, 100 = 10 per sec etc)")
strtimeneed = inputbox ("How many SECONDS do you need to get to your input box?")
If not isnumeric (strtimes & strspeed & strtimeneed) then
msgbox "You entered something else then a number on Times, Speed and/or Time need. Closing program"
wscript.quit
End If
strtimeneed2 = strtimeneed * 1000
do
msgbox "You have " & strtimeneed & " seconds to get to your input area where you are going to message."
wscript.sleep strtimeneed2
shell.sendkeys ("Hello" & "{enter}")
for i=0 to strtimes
shell.sendkeys (strtext & "{enter}")
wscript.sleep strspeed
Next
shell.sendkeys ("Bye" & "{enter}")
wscript.sleep strspeed * strtimes / 10
returnvalue=MsgBox ("Want to send the messages again with the same info?",36)
If returnvalue=6 Then
Msgbox "Ok Messagebot will activate again"
End If
If returnvalue=7 Then
msgbox "Messaging is shutting down"
wscript.quit
End IF
loop
You can just set up a shortcut to terminate all running wscript.exe processes. Click right mouse button on the desktop, in context menu choose New - Shortcut:
Enter %comspec% /c taskkill /f /im wscript.exe as location and click Next:
Enter a name you like and click Finish:
Also you may assign hot keys in shortcut properties.

VBSript Shutdown Message Confusion

I am currently learning how to write commands in Visual Basic and decided to make a simple shutdown message. The code worked perfectly: when the user clicked 'yes' it shutdown and 'no', 'cancel' and the 'X' button closed the message.
However, I decided to try a make a prank message aswell, where the computer would shutdown whatever option was chosen. I ran the script, however when I clicked the 'X' icon (I did not fancy the idea of shutting down my computer!), my computer shutdown anyway :(
Is there a way to stop this happening, or, even better, is there a way to grey out the 'X' icon so the user cannot close the message?
Here is the code:
Option Explicit
Dim result
result = MsgBox ("Do you want to shutdown?", 3+48,"Warning")
Dim objShell
Select Case result
Case vbYes
MsgBox("shuting down ...")
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 20"
Case vbNo
MsgBox("shuting down ...")
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 20"
Case vbCancel
MsgBox("shuting down ...")
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "C:\WINDOWS\system32\shutdown.exe -r -t 20"
End Select
Cheers in advance! :D
-r : for reboot
-s : for shutdown
This Vbscript can create a shortcut on your desktop asking you if you want to shutdown the computer or not.
Option Explicit
Dim MyScriptPath
MyScriptPath = WScript.ScriptFullName
Call Shortcut(MyScriptPath,"Shutdown the computer")
Call AskQuestion()
'**********************************************************************************************
Sub Shortcut(PathApplication,Name)
Dim objShell,DesktopPath,objShortCut,MyTab
Set objShell = CreateObject("WScript.Shell")
MyTab = Split(PathApplication,"\")
If Name = "" Then
Name = MyTab(UBound(MyTab))
End if
DesktopPath = objShell.SpecialFolders("Desktop")
Set objShortCut = objShell.CreateShortcut(DesktopPath & "\" & Name & ".lnk")
objShortCut.TargetPath = Dblquote(PathApplication)
ObjShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,-28"
objShortCut.Save
End Sub
'**********************************************************************************************
Sub AskQuestion()
Dim Question,Msg,Title
Title = "Shutdown the computer"
Msg = "Are you sure to shutdown the computer now ?"& Vbcr &_
"If yes, then click [YES] button "& Vbcr &_
"If not, then click [NO] button"
Question = MsgBox (Msg,VbYesNo+VbQuestion,Title)
If Question = VbYes then
Call Run_Shutdown(30)
else
WScript.Quit()
End if
End Sub
'**********************************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
Sub Run_Shutdown(N)
Dim ws,Command,Execution
Set ws = CreateObject("wscript.Shell")
Command = "Cmd /c Shutdown -s -t "& N &" -c "& DblQuote("Save your work because your PC will shut down in "& N &" seconds")
Execution = ws.run(Command,0,True)
End sub
'**********************************************************************************************
Ok, is there a reason for trying to remove/disable the X? I ask because it will be kinda difficult though it can be done.
Not all button combinations on the msgbox enable the X. Try vbAbortRetryIgnore and also try vbYesNo.
If a Cancel button is provided (all other combinations except vbOkOnly) then X is enabled
If only one button is displayed then X is enabled
This really makes it almost pointless to disable the button once you are aware of these things. Here's what happens when the above scenarios are displayed an user hits X vs clicking on a button
N/A. X button not enabled
MsgBox returns vbCancel
MsgBox returns value of only button
By the way, it is not just clicking X. When X is enabled, ESC will trigger same results.

How to click on ok button in message box using vbscript

I have written a simple script to press ok on message box but its not working. Please help me how to do this
here is the sample code
set oWShell = createobject("WScript.Shell")
MsgBox "Hello"
WScript.Sleep 2000
oWShell.Sendkeys "{enter}"
MsgBox waits for the click. If you don't click yourself, it gets never to "Sleep" or "SendKeys".
I assume you are just trying to learn, because this code makes no sense. If you want to press a button on another programs window, this could work. But in its own process this doesn't work.
If you really want to click your own MsgBox, you have to do it with a separate script. One creates the MsgBox and another clicks the button.
If you just want to close a message box after a certain period of time, check out the Popup() method of the WshShell class. Its second parameter specifies the number of seconds to display the message box for before closing it.
With CreateObject("WScript.Shell")
' Display a message box that disappears after two seconds...
.Popup "Hello", 2
End With
chek this site
you need this:
Set objArgs = WScript.Arguments
messageText = objArgs(0)
MsgBox messageText, 51, "Warning......!"
how to click on button in message box and control of display time here the script can do that .
just copy this lines of code and paste in text file then save it as "ControlMsgBox.vbs".
''' IN THE NAME OF ALLAH
' THIS SCRIPT CONTROL OF MSGBOX DISPLAY TIME
' LET SENKEYS DEAL WITH THE MSGBOX
' SOLVE THE PROBLEM OF APPACTIVATE NOT WORKING EFFECTIVE
On Error Resume Next
Dim Sh : Set Sh=CreateObject("wscript.shell") ' declare and create the wshshell
Dim path : path =Replace(WScript.ScriptFullName,WScript.ScriptName,"") 'declare the variable of the current script path
Dim myMessage : myMessage="This is my message ." 'declare variable of the of the display text of msgbox
Sh.run "cmd.exe /c cd """&path&""" && echo msgbox """&myMessage&""",,""hello"" > mymsgbox.vbs",0,false 'create masgbox script in the same path
WScript.Sleep 1000 'wait 1 sec to let process of create msgbox script execute
Sh.run "mymsgbox.vbs" 'run the created msgbox script
WScript.Sleep 3000 ' let the msgbox display for 3 sec before we sendkeys to close
For i=0 To 600 ' loop to retry select correct msgbox window about 1 min
ret = Sh.AppActivate("In_The_Name_Of_Allah") 'select the activate msgbox window (if this loop 300 this mean loop will continue 30 sec if 600 (1 min)
If ret = True Then ' check if the msgbox windows select or not
Sh.SendKeys "%N" 'send key of Alt+N to select first button in msgbox (ok)
End If
ret = Sh.AppActivate("In_The_Name_Of_Allah") 'recheck again to be sure that we will not send key out of target windows (msgbox window)
If ret = True Then
Sh.SendKeys "{enter}" ' send key to click enter
wscript.sleep 500
ret=Sh.AppActivate("In_The_Name_Of_Allah")
If ret=False Then ' using nested IF to sure of selected windows is false because its close
Exit For ' exit for loop directly and execute what after for next
End If
End If
WScript.Sleep 100
Next
With CreateObject("Scripting.FileSystemObject")
If .FileExists(path&"mymsgbox.vbs") Then 'check if the msgbox script we create form this script exist or not
.DeleteFile(path&"mymsgbox.vbs") 'delete the msgbox script we create after message window closed
End If
End With
Set Sh=Nothing 'remove the sh object create
WScript.Quit ' terminate wscript.exe instance who run this script

Open multiple .vbs one by one

i want .vbs script, to open multiple large files .vbs [i want to Open .vbs one by one] that do not make me, lag in PC.
0001.vbs, 0002.vbs, 0003.vbs, 0004.vbs
is can be different names like:
Anna.vbs, Diana.vbs, Antony.vbs, Andy.vbs
Example:
run C:\0001.vbs
MsgBox "0001.vbs IS END"
Next Open run C:\0002.vbs
MsgBox YES NO
MsgBox "0002.vbs IS END"
Next Open run C:\0003.vbs
MsgBox YES NO
MsgBox "0003.vbs IS END"
Next Open run C:\0004.vbs
MsgBox YES NO
MsgBox "0004.vbs IS END"
Thank you for you help.
Set Shell = CreateObject("WScript.Shell")
For i = 1 To 4
strFile = Right("0000" & i, 4) & ".vbs"
If MsgBox("Would you like to run " & strFile & "?", vbYesNo Or vbQuestion) = vbYes Then
Shell.Run "c:\" & strFile, 1, True
MsgBox strFile & " IS END"
End If
Next
Just make sure you pass True as the last parameter to Shell.Run so that this script waits until the others are done before reporting that they've ended.
Edit: To answer your comment about using names, you can loop through an array created on-the-fly.
For Each strName In Array("Anna", "Diana", "Antony", "Andy")
Next
To not make you wait for each sub process/.vbs before you start the next, don't use the 3rd/wait/true parameter to the .Run method:
a.vbs
Option Explicit
Dim oWSH : Set oWSH = CreateObject("WScript.Shell")
Dim v
For v = 0 To 1
oWSH.Run "cscript.exe " & v & ".vbs", 0, False
Next
MsgBox WScript.ScriptName & " done. " & Now()
0.vbs, 1.vbs
Option Explicit
Randomize
WScript.Sleep Rnd() * 1000
MsgBox WScript.ScriptName & " done. " & Now()
Evidence:
As you can see, a.vbs is finished first and 0.vbs and 1.vbs terminate in random/not in call order.
We have
0001.vbs, 0002.vbs, 0003.vbs, 0004.vbs
Assuming that you have this script file with the after mentioned files in the same directory.
If not, just modify the full path of your vbs files you want to run.
Instead of
WshShell.Run ".\0001.vbs"
You use for example:
WshShell.Run "c:\indel\0001.vbs"
This is the script:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run ".\0001.vbs"
WshShell.Run ".\0002.vbs"
WshShell.Run ".\0003.vbs"
WshShell.Run ".\0004.vbs"
What you need to do is make this code
do
msgbox("haha you cant close this")
CreateObject ("WScript.Shell").Run(".\Duplicate.vbs")
loop

Resources