Focus in an already running app

The LyX program window is open (they write in it). You need to write a file .js for executing certain commands in this window. But I can't put this program in focus. I know this beginning:

WshShell=WScript.
CreateObject("WScript.Shell")
theCalculator = WshShell.Exec("calc");           
// Запустим калькулятор
WScript.Sleep(3000);                             
// Ждем 3 секунды
WshShell.AppActivate
(theCalculator.ProcessID);   // Активируем окно запущенного приложения

But there the focus is moved to the program that was started before from the same script. On the other hand, I understand that you need to use WScript. ConnectObject, but I don't know how. Tell me, pliz.

+++++++++++++++++++++++++++++++++++

I found this one bullshit: here is the Windows calculator running, its PID is 4156. Then the code

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.AppActivate("calc");
WScript.Sleep(100);
WshShell.SendKeys("1{+}");
WScript.Sleep(500);
WshShell.SendKeys("2");
WScript.Sleep(500);
WshShell.SendKeys("~");
WScript.Sleep(500);
WshShell.SendKeys("*3");
WScript.Sleep(500);
WshShell.SendKeys("~");
WScript.Sleep(2500);

Doesn't do anything. Code

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.AppActivate("4156");
WScript.Sleep(100);
WshShell.SendKeys("1{+}");
WScript.Sleep(500);
WshShell.SendKeys("2");
WScript.Sleep(500);
WshShell.SendKeys("~");
WScript.Sleep(500);
WshShell.SendKeys("*3");
WScript.Sleep(500);
WshShell.SendKeys("~");
WScript.Sleep(2500);

Does what was intended. And at the same time, the code

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.AppActivate("word");
WScript.Sleep(100);
WshShell.SendKeys("1{+}");
WScript.Sleep(500);
WshShell.SendKeys("2");
WScript.Sleep(500);
WshShell.SendKeys("~");
WScript.Sleep(500);
WshShell.SendKeys("*3");
WScript.Sleep(500);
WshShell.SendKeys("~");
WScript.Sleep(2500);    

It also performs the necessary task (before executing this script, the word is already running). It seems as if the system does not know the names of the programs implanted in it.

Author: mathem, 2019-07-20

1 answers

Have you tried the Run method? Here is a sample code:

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "calc.exe", 9

WScript.Sleep(3000)

WshShell.SendKeys "1"
WshShell.SendKeys "{+}"
WshShell.SendKeys "2"
WshShell.SendKeys "{ENTER}"

There is a note to the function AppActivate. Perhaps this function searches for the nearest window with the specified title (as a string parameter) and activates it. That is, it searches for the window by name, not by the process Id.

In general, managing the state of windows via WSH functions is not possible. But nothing prevents you from importing the necessary WinAPI functions into scripts and using them. As an alternative, it is the use of external utilities on the similarity to NirCmd.

P.S. In the given example, the existing process is not used, but a new process is generated - this is how the Run{[2 function works]}

 0
Author: Daemon-5, 2019-07-31 03:24:59