More process and select this one
hello,
I need advice how to write a script that would go through all processes named "test" and according to the name of the window (the process can be the same but the window names are different... ) to find out if the application is open or not. In case it is not, it will print a message. If it is, it closes it.
so far I have this, but when I open the same process (application) but there is a different window name, the script crashes on an error that it does not see the requested window name. How do I write this and it will go through all the TEST processes and just see if the window is open with the desired title?
Thank so much!
var nameProcess = "TEST"
var windowName = "NAMEWND"
var p = Sys.WaitProcess(nameProcess).WaitWindow("#32770", windowName, 1)
if(p.Exists && p.WndCaption == windowName)
{
Log.Checkpoint("Exists");
}
else if(!p.Exists && p.WndCaption != windowName)
{
Log.Message("Non Exists");
}
Hi,
You've trapped into the problem that a lot of us happen to be in more or less often. 🙂
The problem is in this line of code:
var p = Sys.WaitProcess(nameProcess).WaitWindow("#32770", windowName, 1)
The case is that .WaitWindow() is a method of the Process object that is returned if the call to .WaitProcess() succeeds.
As it is documented, an empty stub that contains only the .Exists property is returned if .WaitProcess() fails to find the sought for process. This stub, as I just said, does not contain .WaitWindow() method and thus runtime error occurs if test code makes a call to .WaitWindow() for the stub object.
I can suggest two options that you may choose from: pure code-based and NameMapping/Aliases-based.
Code-based code sample:
var oProcess;
var arProcesses = Sys.FindAllChildren("ProcessName", nameProcess);
for (var iIdx in arProcesses) {
oProcess = arProcesses(iIdx);
if (oProcess.WaitWindow("#32770", windowName, -1, 500).Exists) {
// process with the sought for window exists..close it
oProcess.Close();
}
}
NameMapping-based option:
-- Open application and make it to display the window that you need;
-- NameMap process and the window (using the same recognition parameters that you use in code: i.e. ProcessName="TEST" for the process and WndClass="#32770" and WndCaption="NAMEWND" for the window);
-- Rename process in NameMapping and Aliases and give it some unique name (e.g. TestedProcessToBeClosed);
-- Now, -the key point-, select process entry in the NameMapping editor, on the right pane with identification parameters switch to the Required Children tab and select the entry for the window.
The NameMapping snippet create above in the human language will effectively mean this: "The process identified by provided identification parameters that has the window with identification parameters provided for this window which (window) is a child of the process object".
And test code for this NameMapping snippet may be like this:
var oProcess = Aliases.WaitAliasChild("TestedProcessToBeClosed", 500);
if (oProcess.Exists) {
// process with the sought for window exists..close it
oProcess.Close();
}