I am trying to create a very simple vbscript that opens a browser to a specific webpage then refreshes. The problem I am having is trying to make the browser the active window, I have searched various sites and tried various forms of appactivate but with no success. I normally don't work with VBS so I might be missing something super obvious, Please see below for what I have so far
set wshShell = WScript.CreateObject("wscript.shell")
wshShell.Run("http://www.example.com")
wshell.senkeys "{F5}"
I would be very grateful for any assistance.
You can use "AppActivate" and you need to wait for the response:
set wshShell = WScript.CreateObject("wscript.shell")
wshShell.Run("http://www.example.com")
Wscript.Sleep 1000
wshShell.AppActivate "Firefox" 'here put the name of the window
Wscript.Sleep 1000
wshell.senkeys "{F5}"
Related
I recently picked up VBScript and I was messing with the sendkey commands when I noticed that the {PRTSC} isn't working on my computer.
My Program: I made it so that it'll take a screenshot and paste it.
set objShell = CreateObject("WScript.Shell")
WScript.Sleep 1000
objShell.sendKeys "{prtsc}"
objShell.SendKeys "^V"
Is there something wrong with the code or am I not understanding something?
Thanks.
Note
You cannot send the PRINT SCREEN key {PRTSC} to an application.
From Help https://msdn.microsoft.com/fr-fr/library/8c6yea83(v=vs.84).aspx
Also Sendkeys is part of Windows Scripting Host not VBScript.
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.
I am new to VBScript, I need to automate click events to some buttons using its class or id ctrl attribute and select specific option from combo box list. I already have the control attributes like class name or id using which I have to automate different events to the windows applications using vb script.
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "calc"
now I have to do addition of 5 and 6, I have respective ids as num5Button(5), plusButton(+), num6Button(6) and equalButton(=). I need perform click operation for this button. How should I do this?
Since you are automating Calculator, you can use SendKeys to automate the math functions. Sample code is
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "calc"
WScript.Sleep 500
WshShell.AppActivate "Calculator"
WshShell.SendKeys "5{+}"
WScript.Sleep 500
WshShell.SendKeys "6"
WScript.Sleep 500
WshShell.SendKeys "{ENTER}"
I would recommend using something like AutoIt for automating basic Window GUI operations.
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")
I am attempting to refresh Firefox whenever a bat file is called. I Dont want an extension. I need to know if there is a command that can be used from within a .bat file to reload the current browser or tab.
I know you can use
start firefox as a command but this simply opens a new firefox instance. I need to refresh the current instance.
I was just messing around and came up with this half-working solution that might help a little. Create a file called something like refresh.vbs and paste this in:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate("Google Chrome")
WScript.Sleep 500
WshShell.SendKeys "{TAB}"
WScript.Sleep 100
WshShell.SendKeys "{F5}"
I could only test it with Chrome. The script activates Chrome, sends a tab, and then sends F5 to refresh. It works when I have Chrome up showing one web page, but not when there are multiple web tabs open (because AppActivate activates the Window, but does not give focus to anything).
Maybe it works better with Firefox. There's probably some way to enumerate the tabs in a browser and activate it in vbs, but I don't know it.
If you spawn the browser within the vbs (see WshShell.Run, and the example in the SendKeys documentation), you can get the process number and send messages directly to that Window instead of relying on the app title.
You can invoke the .vbs from within a batch file if you need to:
#echo off
refresh.vbs
On Error Resume Next
Set objExplorer = CreateObject("InternetExplorer.Application")
objExplorer.Navigate "http://www.microsoft.com/technet/scriptcenter"
objExplorer.Visible = 1
Wscript.Sleep 5000
Set objDoc = objExplorer.Document
Do While True
Wscript.Sleep 30000
objDoc.Location.Reload(True)
If Err <> 0 Then
Wscript.Quit
End If
Loop