Using VBS script SendKeys to activate hotkeys through Task Scheduler - vbscript

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

Related

Vbscript Sendkeys not working in some applications

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.

Create a VB script to send the keys to open up a program with specific settings

I am trying to create a VB script to send the keys to open up a program/application with specific settings. So, after the application has been started, choosing those very specific settings would involve pressing 2 buttons inside the application UI (1st button to choose specific set of options, and 2nd button to save those options).
I will then create a batch file to call the mentioned VB script on boot.
So far, I got this:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys "^%1"
I've searched and searched, but couldn't figure out how to be able choose/save options once the app has been started, if it's even possible?
Thank you in advance for helping out!
Disclaimer: I am not a technical person, so forgive me if noob question :)
Here's a script to start an application (notepad), then send some keystrokes to it:
'VBScript Example
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "c:\windows\notepad.exe"
' add delay here
WshShell.AppActivate "Notepad"
WshShell.SendKeys "Hello World!"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "abc"
WshShell.SendKeys "{CAPSLOCK}"
WshShell.SendKeys "def"
Perhaps this can be adapted to your needs?
The WshShell.AppActivate command is used to bring the program (with the specified window title) to the foreground.
You might want to add a delay to allow the program time to start before sending the keystrokes. This can be done by adding a sleep() call just after Wshell.Run:
' Sleep for 5 seconds (5000 msec)
WScript.Sleep(5000)
Also, here's a list of key-codes that you can use.

How can I automatically cancel the Enter Network Password prompt in Outlook?

I have an unattended PC which does a very simple task : read emails from a POP server and run various tasks based the content of those emails... 24/7/365
The problem is that at least once a week "something" glitches and the processing stalls because Outlook is waiting for a user to confirm their password with the dreaded Enter Network Password dialog.
I have read so many articles in the past 6 months in an attempt to fix the root cause and nothing has worked. There's no WiFi involved and the email server is Google so it should be extremely reliable so I can only suspect that the broadband connection to my ISP is the culprit as I've read that "certain network conditions" can trigger the password prompt.
Yesterday, I gave up on the root cause approach and looked for an automated tool to cancel the annoying dialogue box when it appears. To my surprise, Windows can do this out-of-the-box via Windows Script Host. Here's how:
A. Open your preferred text editor.
B. Copy paste the following VB Script (you can also use JScript if you prefer but I'm not including the syntax here).
Set WshShell = WScript.CreateObject("WScript.Shell")
' Move focus to the network password dialog if it exists (if not, script continues)
If WshShell.AppActivate ("Enter Network Password") = True Then
' Suspend the script for 1/10th second to make sure the dialogue is in focus
WScript.Sleep 100
WshShell.SendKeys "{ESC}"
' Optionally save this event to a log file
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\Temp", ForAppending, tristateFalse)
file.Write vbCrLf & Date & " " & Time
file.Close
End If
Set WshShell = Nothing
C. Save the file with a .vbs extension e.g. EscapePrompt.vbs
D. Next, open the Windows Task Scheduler (Click the Windows Start button and type "Task" and you should see Task Schedule appear at the top of the search result list, or just type "Task" from the Windows 8 tile view).
Create a new task that runs the script file above, every day, repeating every 5 minutes, indefinitely:
General tab : give the task a name and then check Run with highest privileges
New Trigger tab : click New and select Daily and then in the Advanced Settings, check Repeat task every: and choose 5 minutes. set the for a duration of: to Indefinitely. Check Enabled
Actions Tab : click New and then Browse to choose the script file you created above.
Conditions tab : Set any conditions as required by your environment.
Settings tab : I don't change anything here but you may want to.
Now, when the Enter Network Password prompt appears, there is a maximum delay of 5 minutes before it is automatically cancelled.
For more information on WScript and in particular, the SendKeys method, check out MSDN :
http://msdn.microsoft.com/en-us/library/8c6yea83(v=vs.84).aspx
You may also be interested in our free VBA examples at:
http://youpresent.biz/category/blog/vba/
Microsoft outlook keeps prompting network password if there registry value and network password mismatches. If you change the password for your account in ISP or Control Panel.Then, configure and login outlook with the new password.Then ,it will not prompt for outlook password again and again.

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

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