actiona icon indicating copy to clipboard operation
actiona copied to clipboard

How can i find a window by pid ?

Open Majunko opened this issue 4 years ago • 5 comments

Hello, i'm trying to find a window by pid but it seems the program doesn't find the window.

I'm executing a detached command, in advanced -> output parameters i wrote the variable p1.

Then i use the action Window and in the input parameters -> Window title. On this field i'm writing this code:

var processHDL= new ProcessHandle(p1);

var windows = Window.find({
   process: processHDL
});

Any idea ? Example attached.

test_pid.zip

Majunko avatar Apr 25 '20 19:04 Majunko

Hi, there is one issue with your script: the Window title field of the Window action was set to Text and not Code, but it contains Javascript.

Jmgr avatar Apr 27 '20 11:04 Jmgr

You're right, i switch Text to Code but it seems to be the same. It throw an exception because cannot find the window, this exception is configured like this: goto line 3

The line 3 in the script is a code with this content: Console.print("pid: " + p1);

So, the main problem is that the window cannot be find by pid. Although the pid in the variable p1 is ok.

I tried the next code but it does not works too.

var windows = Window.find({
  processId: p1
});

Is there another alternative to do this? Thanks.

Majunko avatar Apr 27 '20 17:04 Majunko

I think the other issue is that your script needs to return a window title. You could write something like that:

var processHDL= new ProcessHandle(p1);

var windows = Window.find({
   process: processHDL
});

if(windows.length > 0) {
	return windows[0];
} else {
	return "Invalid window";
}

Jmgr avatar Apr 29 '20 21:04 Jmgr

I found out the way to do this with your script. I had to remove the return due to it throw an invalid statement at execution and i added the method title() in the end of windows[0]. The result is this:

var processHDL= new ProcessHandle(p1);

var windows = Window.find({
   process: processHDL
});

if(windows.length > 0) {
	windows[0].title();
} else {
	"Invalid window";
}

Thank you so much.

additionally, is there a possibility to implement into a window action the option to find the program by pid or process name instead of only window title?

Majunko avatar Apr 30 '20 00:04 Majunko

I found out the way to do this with your script. I had to remove the return due to it throw an invalid statement at execution and i added the method title() in the end of windows[0].

Ah yes, sorry. I had to rewrite this code after having tested it.

additionally, is there a possibility to implement into a window action the option to find the program by pid or process name instead of only window title?

Yes, that would make sense. Thanks for the idea.

Jmgr avatar May 01 '20 19:05 Jmgr