Task Scheduler, scheduling vbs script - vbscript

I 've created an application with Visual Basic, containing a menu bar. Using a menu bar in the application, I can start VB scripts, which works fine.
The next step is to schedule the tasks daily, so I don't have to manually start the scripts.
I would like to use the Task Scheduler to perform the tasks daily and wrote an .vbs script which I scheduled in the Task Scheduler. Testing the script by manually starting the task from Task Scheduler works. But when scheduled (eg for the next morning) it fails: the program is started, but the next step is not performed.. I'am using a laptop, powered at all times, sleeping mode is off, Windows10
My vbs script:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.SendKeys("{F13}")
WScript.Sleep 10*1000 '10 sec
WshShell.Run "C:\360_Stuurprogramma\bin\Debug\360_Stuurprogramma.exe"
WScript.Sleep 5*1000 '5 sec
WshShell.AppActivate """360 Applicatie"" -p1 -c"
WScript.Sleep 20*1000
wshShell.SendKeys "^S"
WScript.Sleep 20*60*1000 '20 min, maximum time needed to perform the job
WshShell.Run "taskkill /f /im 360_Stuurprogramma.exe", , True

Only programs with a foreground GUI can set the active window. A program started by the active window has two seconds to show a GUI to become the foreground window. The full rules are here - https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow
NB Task Scheduler will never be the foreground window.
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.
An application cannot force a window to the foreground while the user
is working with another window. Instead, Windows flashes the taskbar
button of the window to notify the user.

Related

Window activation by Title using vbs

I am using a script that:
Launches Thunderbird
Creates a new message
Minimizes the message box
Copies the path to the selected folder
How to activate a minimized Thunderbird message box, knowing its Title?
The code below does not work:
Set WshShell = CreateObject("WScript.Shell")
Set WshExec = WshShell.Exec("""C:\Program Files (x86)\Mozilla Thunderbird\thunderbird.exe""")
WScript.Sleep 2000
WshShell.SendKeys("^n")
WScript.Sleep 500
PID = WshShell.AppActivate(WshExec.ProcessID)
WshShell.Run("notepad")
If PID Then
WshShell.SendKeys("^k")
Else
MsgBox "Nothing!"
End If
VBS's AppActivate wraps the API's SetForegroundWindow. Your program MUST comply with one of the following rules to set the active window. Note the standard lockout time is 2 seconds - which is where most people get caught. Note VBScript has no user interface so cannot be the foreground window - you have 2 seconds to set windows after your program starts.
From https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setforegroundwindow
SetForegroundWindow
Remarks
The foreground window is the window at the top of the Z order. It is
the window that the user is working with. In a preemptive multitasking
environment, you should generally let the user control which window is
the foreground window.
Windows 98/Me, Windows 2000/XP: 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 foreground process is being debugged.
The foreground is not locked (see LockSetForegroundWindow).
The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
Windows 2000/XP: No menus are active.
With this change, an application cannot force a window to the
foreground while the user is working with another window. Instead,
SetForegroundWindow will activate the window (see SetActiveWindow) and
call the FlashWindowEx function to notify the user.

Application window to be activated every 3 minutes

I have multiple windows on the screen and I want one of these windows to be activated every 3 minutes with a VBScript.
Here's what I've tried:
objShell.AppActivate "Program Name"
but I was unsuccessful.
You cannot do this. Only a newly started script can change the foreground window (within two seconds) or the script is the foreground window (ie it has a UI).
Programs cannot mess with users' windows unless the user is working in that program.

Send keystrokes from vbscript to a CMD window

I run a batch file as part of Windows XP startup which executes continuously until Windows closes. It calls a VBScript file, which executes SendKeys "+{ESC}" (Shift+Esc). This gets picked up by 4t Tray Minimiser to send the CMD window to the system tray. Most times it works, but occasionally the CMD window stays visible.
Upon searching, various posts seem to suggest the .VBS file is losing focus or it's a timing problem. Some suggest AppActivate but I can't work out how to achieve what I want. Even the examples given for AppActivate seem like a lot of work.
Did you check the documentation? It's actually rather straightforward. You call AppActivate with the title (or part of the title) of the window you want to bring to the foreground, then run SendKeys to send keystrokes to the foreground window.
Set sh = CreateObject("WScript.Shell")
sh.AppActivate "window title"
sh.SendKeys "+{Esc}"

VBscript run via shortcut always in Background Why?

When I run the following script via CMD.EXE the display of Word is normal i.e. Maximized.
Set oWord = CreateObject("Word.Application")
oWord.Visible = True
oWord.Documents.open(sTempWordFile)
oWord.Run WScript.Arguments.item(0)
oWord.Activate
When I call the same script as Target in a shortcut .LNK file the display is always minimized!
The .LNK file target is: "%OWNERS_CORP_ROOT%\cmd\RunWord.vbs" memos
Using Windows 8.1, Office 2013. Shortcut .LNK file is set to Run 'Maximized'.
How do I make a 'Maximized' display when using the shortcut?
SetForegroundWindow Function
The SetForegroundWindow function puts the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window, and various visual cues are changed for the user. The system assigns a slightly higher priority to the thread that created the foreground window than it does to other threads.
Syntax
BOOL SetForegroundWindow( HWND hWnd
);
Parameters
hWnd
[in] Handle to the window that should be activated and brought to the foreground.
Return Value
If the window was brought to the foreground, the return value is nonzero.
If the window was not brought to the foreground, the return value is zero.
Remarks
Windows 98/Me: 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 foreground process is being debugged.
The foreground is not locked (see LockSetForegroundWindow).
The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
Windows 2000/XP: No menus are active.
With this change, an application cannot force a window to the foreground while the user is working with another window. Instead, Foreground and Background Windows will activate the window (see SetActiveWindow) and call the function to notify the user. However, on Microsoft Windows 98 and Windows Millennium Edition (Windows Me), if a nonforeground thread calls SetForegroundWindow and passes the handle of a window that was not created by the calling thread, the window is not flashed on the taskbar. To have SetForegroundWindow behave the same as it did on Windows 95 and Microsoft Windows NT 4.0, change the foreground lock timeout value when the application is installed. This can be done from the setup or installation application with the following function call:
SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (LPVOID)0, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
This method allows SetForegroundWindow on Windows 98/Windows Me and Windows 2000/Windows XP to behave the same as Windows 95 and Windows NT 4.0, respectively, for all applications. The setup application should warn the user that this is being done so that the user isn't surprised by the changed behavior. On Windows Windows 2000 and Windows XP, the call fails unless the calling thread can change the foreground window, so this must be called from a setup or patch application. For more information, see Foreground and Background Windows.
A process that can set the foreground window can enable another process to set the foreground window by calling the AllowSetForegroundWindow function. The process specified by dwProcessId loses the ability to set the foreground window the next time the user generates input, unless the input is directed at that process, or the next time a process calls AllowSetForegroundWindow, unless that process is specified.
The foreground process can disable calls to SetForegroundWindow by calling the LockSetForegroundWindow function.
Function Information
Minimum DLL Version user32.dll
Header Declared in Winuser.h, include Windows.h
Import library User32.lib
Minimum operating systems Windows 95, Windows NT 3.1
Unicode Implemented as Unicode version.
See Also
Windows Overview, AllowSetForegroundWindow, FlashWindowEx, GetForegroundWindow, LockSetForegroundWindow, SetActiveWindow

Are the documented conditions accurate for SetForegroundWindow?

Refer to the MSDN documentation for SetForegroundWindow
Windows intentionally refuses many requests to set a particular window to foreground, with the stated goal that:
An application cannot force a window to the foreground while the user is working with another window.
The conditions under which SetForegroundWindow will succeed are documented as:
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.
This documentation does not sound correct. I believe that it started as a shorter list, in which only one of the conditions needed to be met. But the actual behavior is more complex.
Can anyone confirm the actual behavior? Is it:
A process can set the foreground window only if all the following hold:
One or more of the following conditions is true:
The process is the foreground process.
The process was started by the foreground process.
The process was identified in an AllowSetForegroundWindow call made 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.
One of the following two conditions is true:
The foreground is not locked (see LockSetForegroundWindow).
The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
No menus are active.
If other experts concur that the documentation is erroneous here, I will file a bug report on Connect referencing this question.

Resources