Shell.openItem() to return PID or get PID of a launched application using child_process


I’m trying to open a file with an electron app and ‘watch’ it for any changes, etc.

I have tried using shell.openItem() which opens the item perfectly fine, but all it returns is true of false indicating the status of the command. However, I’d like to receive or find out the PID of the application that just got launched. Is this possible?

I also tried to use child_process.spawn() / child_process.exec() and had no success with either. If I try to get the PID using the child_process, it returns the wrong PID, im assuming because it returns the PID for the node process which isn’t what I want.

I also tried -Wait flag in the exec() command. In shell, if I add the -Wait command, it will wait until the application is closed however this does not work with electron. The process immediately ends with code 0. If I could make the node process wait instead of closing right away essentially I get what I want, if I watch the node process that is waiting for the application that it just launched to close, im technically watching that file that I opened. Once the node process ends, I can assume the launched file is now closed.

Does anyone have any ideas / tips on how I could achieve this?

Thanks :slight_smile:

I did also try execSync, it also returns with code 0 as soon as the ‘start’ application command is run, also since its execSync I cannot retrieve the PID of neither the node process or the application.

So I was able to get a bit closer after a whole day of debugging. Before I was using start to launch a file in the default app for Windows 10. I was reading through the documentation of start here and in there was a nifty little sentence which stated

To start a new program (not a batch script), you don’t have to use CALL or START, simply enter the path/file to be executed, either on the command line or within a batch script.

For some reason, if I simply have the path and use execSync with -Wait, it works. As soon as I have start or call it doesn’t.

But, this also only works for documents.

const test = child_process.execSync(fullPath + ' -Wait');

For some reason, it only waits for documents like .txt, .docx, .xlss not sure why but nothing else seems to wait and just closes with code 0 as soon as the program launches.