Script to make a program/browser preferably run if not opened yet, if its opened then it brings it forward/maximizes it.
-
- Posts: 2
- Joined: 14 Feb 2019, 22:22
Script to make a program/browser preferably run if not opened yet, if its opened then it brings it forward/maximizes it.
Is it possible for a script to open a site in a browser or if the site is already opened it just maximizes it/brings it forward if it is hidden behind other programs.
-
- Posts: 2
- Joined: 14 Feb 2019, 22:22
Re: Script to make a program/browser preferably run if not opened yet, if its opened then it brings it forward/maximizes
So the question is if this is possible using VB sciprt and I don't know the answer. I'm not that experienced with Microsoft scripting technologies.
I would say that opening site should be possible and that "already opened check" is not, but just guessing.
I would say that opening site should be possible and that "already opened check" is not, but just guessing.
Petr Medek
LUAmacros author
LUAmacros author
Re: Script to make a program/browser preferably run if not opened yet, if its opened then it brings it forward/maximizes
Determining if a program is running is relatively simple in VBScript. Getting that application focused can be a bit finicky at times depending on the OS, but in Windows 10 it seems to work at least somewhat reliably.
Here's some vbscript I wrote up. There's no error handling in it, but it should serve as an example of how you'd accomplish this.
As for what site is open, I'm not familiar with any way of doing that, but I can look into it some tomorrow and see what I can find, though that would likely be limited to Internet Explorer if anything.
Edit: So you can navigate IE to a certain page, but only if it's an instance you created with the script as far as I can tell. This page has the simplest script I have found for it, but I'd figure this is probably limiting beyond what you would want to do though since if you opened IE manually I don't believe it would work.
Here's some vbscript I wrote up. There's no error handling in it, but it should serve as an example of how you'd accomplish this.
Code: Select all
Dim objWMIService, objShell, application
'This is the only variable you should need to set.
application = "chrome.exe"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objShell = WScript.CreateObject("WScript.Shell")
'Search the list of processes (Win32_Process) to see if the application we're looking for is running.
Set colProcesses = objWMIService.ExecQuery("Select * from Win32_Process Where Name = '" & application & "'")
'The count will be 1 or higher if the application is running (one for each process of it running), or 0 if it isn't running.
If colProcesses.Count = 0 Then
'Not Running
'Launch the application.
'Note that I have no error handling here, so if the application isn't in the path or otherwise
'can't be found, this will produce a dirty error.
objShell.Run application, 1, true
Else
'Already Running, activate the window.
'When calling AppActivate, you have to use the name of the application without the extension.
'The next few lines remove the last .* from the executable name since that's the name we want
'to use in most cases.
Dim tempApp, i
tempApp = Split(application,".")
application = ""
For i=0 To UBound(tempApp)-1
application = application & tempApp(i) & "."
Next
application = Left(application,Len(application)-1)
'Bring the window to the front.
objShell.AppActivate(application)
End If
Edit: So you can navigate IE to a certain page, but only if it's an instance you created with the script as far as I can tell. This page has the simplest script I have found for it, but I'd figure this is probably limiting beyond what you would want to do though since if you opened IE manually I don't believe it would work.