Vbscript Sendkeys not working in some applications - vbscript

I recently google around and found that tools like CCleaner can be automated to Scan and Fix Regsitry errors by VBScript or AutoIt scripts.
As I prefer native tools and libraries, so I created a VBScript to automate the Registry Cleaning process(not upto the last button click, as there are too many buttons for that to be automatable), which is as below:
On Error Resume Next
Set WshShell = WScript.CreateObject("WScript.Shell")
Set UsrEnvDmn = WshShell.Environment("User")
UsrEnvDmn("CclnrInstDir") = "C:\Program Files\CCleaner"
WshShell.Run """%CclnrInstDir%\\CCleaner64.exe"" /Registry"
WScript.Sleep 1000 : WshShell.AppActivate "CCleaner"
WScript.Sleep 2000 : WshShell.SendKeys "%s"
WScript.Sleep 5000 : WshShell.SendKeys "%f"
Set WshShell = Nothing : Set UsrEnvDmn = Nothing
But the problem is once CCLeaner opens and comes into focus, the Sendkeys function isn't clicking/selecting any buttons or options in the window. If I press manually Alt + s then the Scan process indeed starts, but I badly need to automate that thing, if possible upto the very end of registry cleanup process.
Can anyone guide on this ?

See the remarks section of SetForegroundWindow. Your script has to comply with at least one of those rules when you use AppActivate (as this is what AppActivate calls). You keep sleeping and losing the right to set the foreground window. See CCleaner documentation on a better way https://www.ccleaner.com/docs/ccleaner/advanced-usage/command-line-parameters
The system restricts which processes can set the foreground window. A
process can set the foreground window only if one of the following
conditions is true:
•The process is the foreground process.
•The process was started by the foreground process.
•The process received the last input event.
•There is no foreground process.
•The process is being debugged.
•The foreground process is not a Modern Application or the Start
Screen.
•The foreground is not locked (see LockSetForegroundWindow).
•The foreground lock time-out has expired (see
SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
•No menus are active.

Related

Using VBS script SendKeys to activate hotkeys through Task Scheduler

I have Open Broadcaster Software installed and I want to use it to automatically record the desktop at scheduled times. In OBS, I've set up a "start record" hotkey (in this cast ALT 1) and a "stop record" hotkey (ALT 2).
OBS starts with Windows and opens minimized to the task bar.
Pressing the hotkeys keys successfully starts and stops screen recordings regardless of what program is open or focused on.
I want to be able to use Task Scheduler to schedule recordings at specific times throughout the week. Task Scheduler cannot schedule key strokes so I created a start.vbs file and a stop.vbs file. Here's where my problem is, I can't figure out how to get the 'ALT 1' or 'ALT 2' keys to be recognized by OBS.
Here's what I have for my start.vbs:
'Start Recording
Option Explicit
Dim WSHShell, strDesktop
Set WSHShell = WScript.CreateObject("WScript.Shell")
strDesktop = WSHShell.SpecialFolders("Desktop")
WSHShell.AppActivate strDesktop
WSHShell.SendKeys "%1"
WScript.Quit
It seems to run fine, but the keystrokes don't register. Am I doing something wrong, or is there a better way to go about this?
Thanks

WScript.Shell SendKeys not working on Windows 8.1 x64

I'm having problems with the following VBScript.
I'm trying to send keystrokes to a web browser window, but nothing happens.
The first two lines in the following script works fine, but the third line with the SendKeys command, doesn't do anything.
Set objShell = CreateObject("WScript.Shell")
objShell.Run "http://someurl.com", 1
objShell.SendKeys "Some Text"
I've tried to run the script on two different Win 8.1 machines. On Win 8.1 Pro it works, but on Win 8.1 not-Pro it won't work. Is there any setting somewhere in Windows that needs to be set or anything other that can help me?
Update
New code tried (added sleep and running browser explicit), but still no luck
Set ObjShell = WScript.CreateObject("WScript.Shell")
objShell.Run """C:\Program Files\Internet Explorer\iexplore.exe""http://someurl.com/"
WScript.Sleep 5000
objShell.SendKeys "Some Text"
While its admittedly not a great programming technique, place a Sleep command after your objShell.Run line (as shown below) to give the OS some time to "catch up" and actually run the URL, activate the browser window to give it focus (you have to take note of the title in the Browser window and use that title exactly), because I have seen the browser window lose focus in some cases and then text cannot be piped into it, and finally it sends the text. Your mileage may vary, and you might even have to increase the sleep time in order to be successful. I've had to sleep up to 30 seconds, in some cases. Especially where, for whatever reason, the web page was slow to render - due to network latency, slow OS, etc.. I haven't faced this problem on Windows 8.1 because I haven't tried it on that platform. I don't think this has anything to do with the OS version; I have faced this same problem with SendKeys on other Windows OS versions where I've had to use the Sleep function like this. The SendKeys method has known limitations, and as it runs outside of the browser you have to wait for certain actions to complete before you can have it do more actions.
Set ObjShell = WScript.CreateObject("WScript.Shell")
objShell.Run "http://someurl.com", 1
WScript.Sleep 20000
objShell.appactivate("Welcome to SOMEURL.COM! - Mozilla Firefox")
WScript.Sleep 700
objShell.SendKeys "Some Text"
Also, you might try replacing line 2 in the above to the following, to see if you have better luck. In the example below, Firefox is the default web browser. Change the path accordingly to the browser of your choice:
objShell.Run("""C:\Program Files\Mozilla Firefox\firefox.exe""http://someurl.com")

Need a startup script in windows to send ALT+F11 keys?

We are using a shared desktop on thin clients. By default it comes with windowed screen, but by pressing the Alt + F11 keys it will restore to full screen. So, we want a script to execute at Windows log-on with some delay.
Hopefully a login vbscript will work for you. Either append this to an existing login script or save it as a ".vbs" file. Microsoft has some good tutorials if you are unfamiliar with login scripts.
Set WshShell = CreateObject("Wscript.Shell") 'Create wshell object
WScript.Sleep(5000) 'Lets wait 5 seconds
WshShell.AppActivate "EXACT TITLE OF THE WINDOW YOU WANT" 'EDIT THIS LINE!
'The line above selects the window so we make sure the keystrokes are sent to it
WshShell.SendKeys "%{F11}" 'Send ALT+F11
Wscript.Quit 'Quit the script

Block screensaver

Could you provide simple approach to block screensaver and prevent locking computer.
I need this for running automation tests and I have no rights to change this on local machine due to global computer locking company policy.
here's mouseJiggler
Mouse Jiggler is a very simple piece of software whose sole function
is to "fake" mouse input to Windows, and jiggle the mouse pointer back
and forth.
Useful for avoiding screensavers or other things triggered by idle
detection
a windows system? I did on Windows 2008 R2:
use notepad.exe to create c:\temp\idle.vbs
paste code
Dim objResult
Set objShell = WScript.CreateObject("WScript.Shell")
i = 0;
Do While i = 0
objResult = objShell.sendkeys("{NUMLOCK}{NUMLOCK}")
Wscript.Sleep (60000)
Loop
every 60 seconds, this script "press" numlock twice
run the script C:\Windows\System32\cscript.exe c:\temp\idle.vbs
Additonal you can create a new Shortcut, paste the command line and set to "run minimized", camouflagethe icon to explorer or other stuff.

How to set focus on file dialog opened in Vbscript

Our Team is automating tests/test data preparation in QTP and we do the scripting in VBScript.
In several tests the tester who runs the script need to supply an MS-Excel file with the indata. We use UserAccounts.CommonDialog for this and it works great. Except for one litle problem, when we run this from QTP the file dialog does not get focus. It's opened in the background and it's easy for the tester that runs the script to miss it and waste time waiting for the dialog.
How do we give the file dialog focus?
Code Example:
Set ObjFSO = CreateObject("UserAccounts.CommonDialog")
ObjFSO.Filter = "MS-Excel Spreadsheets|*.xls|All Files|*.*"
while ObjFSO.ShowOpen = false
msgbox "Script Error: Please select a file!"
wend
msgbox "You selected the file: " & ObjFSO.FileName
My guess is that since the dialog is modal, the ShowOpen method doesn't return the execution control back to the script until the dialog is closed. So there's no way to interact with the dialog as part of your test script.
As a workaround, you could spawn a parallel script that would wait for the dialog and activate it. But I guess QTP cannot run two scripts in parallel, so you'll probably need an external shell script (written in VBScript / JScript / PowerShell / etc).
Edit: Try the following:
Create an external VBScript file (.vbs) with the following contents:
Set oShell = CreateObject("WScript.Shell")
While Not oShell.AppActivate("Open")
WScript.Sleep 500
Wend
This script calls WshShell.AppActivate to activate a window whose title contains Open (replace it with the actual dialog title). If there's no such widnow at the monent, it retries the attempt after 0.5 sec (you can increase the delay if you wish).
Launch this script from your QTP test before opening the dialog. (Use SystemUtil.Run or something like this.)
I'm not sure, but I think this should do the trick.
Did you try recording a click on the dialog - so that QTP will click on it to set focus before proceeding?

Resources