I am trying to read input interactively from a command prompt using VBScript, But I can't seem to read the data using readLine() in order to later send a reply.
Here's the code I have tried:
set OBJECT=WScript.CreateObject("WScript.Shell")
For i=1 To 500
If i=450 Then Exit For
OBJECT.SendKeys "00LI002LE99{ENTER}"
WScript.sleep 2000
dim input=ReadLine()
If input="LI002LE99" Then OBJECT.SendKeys "OK{ENTER}"
OBJECT.SendKeys "00LI002LE00{ENTER}"
WScript.sleep 2000
Next
The idea with this code is that if I were to send the command 00LI002LE99 on the command line, I want to send a console reply of LI002LE99. How can I get my script to read the data using the readline() command and then send the appropriate response?
I'm not 100% sure why you would want to do this, but there are a few problems with your code:
I'd recommend avoiding using the name OBJECT for variables.
You can't use dim like that in vbscript. You have to split it out into multiple instructions. What language do you normally use?
The way you are trying to use Readline() just won't work.
Here's a working example of your script, but be aware that if you move focus away from the the command window, strange things may happen! (eg: the send keys instruction will output the results to whichever window has the current focus - which I would imagine is not the desired outcome, if you are tabbing between windows or something):
Option Explicit
Dim shell, i, input
set shell=WScript.CreateObject("WScript.Shell")
For i=1 To 500
If i=450 Then Exit For
shell.SendKeys "00LI002LE99{ENTER}"
WScript.sleep 2000
input = WScript.StdIn.ReadLine()
If input="00LI002LE99" Then shell.SendKeys "OK{ENTER}"
shell.SendKeys "00LI002LE00{ENTER}"
WScript.sleep 2000
Next
Best of luck. Again, not entirely sure what you're doing here :)
FYI: The Wscript.StdIn.ReadLine() is going to be a little hit and miss.
Related
Long story short, I print shipping labels at my job through a proprietary program that runs through IE. A common shipper is Caterpillar Global Mining, so I made a script to enter that field for me.
Set wshShell = WScript.CreateObject("WScript.Shell")
WScript.Sleep 100
wshShell.AppActivate ("PRINT LABEL")
WScript.Sleep 100
wshShell.SendKeys "caterpillar global mining"
I put the cursor in the correct field, hit the Ctrl+Alt+A to run the script, and wham! I don't have to type the company's name 100 times. The problem is, it only works when I enter the PID instead of the title (for example, 1111 instead of "PRINT LABEL"). If I use the title in the script, it will bring focus to IE, but lose the cursor position. Using the PID is ok by me, I'm just curious as to why this happens.
UPDATE: after playing around some more I've noticed that there are 2 processes for IE. If I enter the other process PID the script reacts the same way as when I use the title. I am now even more confused. I feel like there is a simple answer, I just don't see it.
For using the title the string must match the beginning of the window title. Also, you will get the first match, so you need to make the string specific enough. The PID is by design and definition unique, so you don't run into these problems when using a PID in AppActivate.
If system locks every 5 minutes, what VB Scripting method is used to avoid system locking every 5 minutes?
Save this file in the format of .vbs
Change the Sleep Time 400 based on your requirement.
Set wshShell =wscript.CreateObject("WScript.Shell")
do
wscript.sleep 400
wshshell.sendkeys "{SCROLLLOCK}"
wscript.sleep 400
loop
Perhaps, WScript.SendKeys could help:
msdn
Something like double-ScrollLock, I guess?
PS. You'll need to put that in a loop with a sleep, of course.
The simplest and easiest way is to open windows media player and run the supplied Wildlife in HD video in repeat mode and then minimize it.
Problem solved, it won't allow the system to get lock automatically. No need for VBS.
Another option is to use small utility called Caffeine. Just download, extract and double click the .exe to run. It will run in background and will do the trick for you.
in one of my QTP process, i need to read the text in the message and decide whether to press OK or Cancel. Please help me to achieve the same.
I have used vbscript as below (oShell.Appactivate)
Dim ClosingValue
WScript.Sleep 2000
Set oShell = WScript.CreateObject("WScript.Shell")
If oShell.AppActivate("Warning Message")=True Then
While Not oShell.AppActivate ("Warning Message"):Wend
oShell.SendKeys "{ENTER}"
ClosingValue = "Warning Message"
ElseIf oShell.AppActivate ("Error Message")=True Then
While Not oShell.AppActivate ("Error Message"):Wend
oShell.SendKeys "{ENTER}"
ClosingValue = "Error Message"
End If
Thanks in Advance
Prashanth
I don't think it is possible like this
when you do AppActivate, your VB script goes on halt until the other application terminates.
Also, i don't know any methods to read the content of the message (but that could be only because i don't know everything :) )
Perhaps you could describe what you need to accomplish here. i would try to pass some values by another mean, and there's plenty.
What is the Application that open this message box?
also, i don't see the need for While Not oShell.AppActivate ("Error Message"):Wend, since the windows is already activated by the previous line, if any windows is named this way
I am trying to read input interactively from a command prompt using VBScript, But I can't seem to read the data using readLine() in order to later send a reply.
Here's the code I have tried:
set OBJECT=WScript.CreateObject("WScript.Shell")
For i=1 To 500
If i=450 Then Exit For
OBJECT.SendKeys "00LI002LE99{ENTER}"
WScript.sleep 2000
dim input=ReadLine()
If input="LI002LE99" Then OBJECT.SendKeys "OK{ENTER}"
OBJECT.SendKeys "00LI002LE00{ENTER}"
WScript.sleep 2000
Next
The idea with this code is that if I were to send the command 00LI002LE99 on the command line, I want to send a console reply of LI002LE99. How can I get my script to read the data using the readline() command and then send the appropriate response?
I'm not 100% sure why you would want to do this, but there are a few problems with your code:
I'd recommend avoiding using the name OBJECT for variables.
You can't use dim like that in vbscript. You have to split it out into multiple instructions. What language do you normally use?
The way you are trying to use Readline() just won't work.
Here's a working example of your script, but be aware that if you move focus away from the the command window, strange things may happen! (eg: the send keys instruction will output the results to whichever window has the current focus - which I would imagine is not the desired outcome, if you are tabbing between windows or something):
Option Explicit
Dim shell, i, input
set shell=WScript.CreateObject("WScript.Shell")
For i=1 To 500
If i=450 Then Exit For
shell.SendKeys "00LI002LE99{ENTER}"
WScript.sleep 2000
input = WScript.StdIn.ReadLine()
If input="00LI002LE99" Then shell.SendKeys "OK{ENTER}"
shell.SendKeys "00LI002LE00{ENTER}"
WScript.sleep 2000
Next
Best of luck. Again, not entirely sure what you're doing here :)
FYI: The Wscript.StdIn.ReadLine() is going to be a little hit and miss.
I have a VBScript I am working on that uses another object.
Sometimes that Object will get stuck. My VBScript code will hang on that line until it's "done". When it times out, I want to send the .Close command to the Object before the VBScript closes.
How can I tell when my VBScript times out?
I know that I can put WScript.Timeout = 60
Maybe something like..
WScript.Timeout = 5
do while true
loop
sub WScript_timeout()
msgbox("OK")
end sub
By setting the Timeout property you instruct the interpreter to automatically terminate the script when the timer expires. This is the same as running the interpreter with the option //T:xx and can't be caught/handled from within the script. What you want requires the ability to run code asynchronously, and VBScript doesn't really support that.
The real answer (to the question "How can I tell when my VBScript times out?") is that you can't. In common with almost all scripts, if VBScript stops running (because it's timed-out) the running thread ceases to run, so it can't report its status.
But there is a solution. However, it requires some cunning.
If you run a batch script instead, wherever you use that script to launch a new batch script (e.g. batch_1.bat includes this line: CALL batch_2.bat), the 2nd script will run, but the 1st script will wait.
Processing of the 1st script sits and waits (at the CALL) until script 2 stops running: at that point, control is returned to script 1, which continues with any code following the CALL, code which might be used to report the fact that script 2 has ended -
CALL batch_2.bat
ECHO The batch_2.bat script has stopped running && cmd /k
There are ways of launching batch_2.bat without causing batch_1.bat to pause until the 2nd script has finished, but they are not relevent here.
Theoretically, a batch script doesn't support parallel processing. VBScript certainly doesn't either. But the foregoing technique shows one method whereby parallel processing can be achieved, after a fashion, in a batch script -- which makes it one-up on vbScript!
.
One way to be certain that vbScript will time out, if the script hangs, so that the script must either complete successfully or fail (so you are never left with a frozen script due to it "hanging"), is to use a WScript function in your .vbs file and set the Windows Script Host settings to time out after (say) 30 seconds -
A. Open the "Windows Script Host Settings" dialog box:
Go to: Start > Run
In the "Open" box, type: WSCRIPT
Click "OK".
B. Set a timeout, to occur whenever WSH runs:
Select the option: "Stop script after specified
number of seconds".
In the "seconds" box, type the time limit to be
applied to all scripts (default is 10 seconds).
.
Here's a function to find and show what the current WSH/WScript timeout setting is (and if it shows that this setting hasn't been set yet, set it) -
WScript.Echo("WSH timeout: " + WScript.Timeout);
.
The option //T:xx can't be used, because it's a CScript function, which doesn't work in WScript, so can't be used in a .vbs vbScript file.
.