Ahk isn't getting the PID i need for chrome

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Araphen
Posts: 5
Joined: 20 Apr 2024, 05:22

Ahk isn't getting the PID i need for chrome

26 Apr 2024, 16:29

We have an in-house app that I can send commands to by accessing certain URLs. (it's super convoluted but it's the best we can do right now). So I want to be able to use my AHK script to launch the URL in chrome (in incognito mode so when I close it, it doesn't re-open the next time chrome opens and send a bunch of commands) then wait like 2 seconds and close it. And we can't use a different browser, it has to be chrome. We tried IE, firefox, and edge and all of them fail to send the commands.

Code: Select all

	Run, "chrome.exe" %targetURL% "--incognito",,, VarPID
	Process, Exist, %VarPID%
	msgbox, VarPID = %VarPID%
	sleep, 2000
	process, close, %VarPID%
But when I run the above code, it opens the webpage in incognito mode fine and it gets a PID. So I'll look in task manager to see what that PID is associated with, and it just doesn't exist. The window is still there, just with a different PID so "process, close, %VarPID%" doesn't close the window.

My goal is to add "--headless" to the run command so the window doesn't exist and my script isn't popping open random chrome windows but I don't know how to do that without getting the PID problem sorted

I'm guessing that chrome runs a process which runs another process to make the incognito window. So the original process that I get the PID of exists for less than a second. Then the incognito window has a new PID.

If that is what's happening, I have no idea how to work with that. I'd probably just end up not making it headless and using "winclose, URL - Google Chrome" instead.

Anyone have any ideas?
ShatterCoder
Posts: 84
Joined: 06 Oct 2016, 15:57

Re: Ahk isn't getting the PID i need for chrome

26 Apr 2024, 17:22

Yeah I've run into this issue before, I forget the exact reason, but the run command sometimes is fed a PID from an intermediary process that dies as soon as it passes off the commands. The good news is that there is an easy solution:

Code: Select all

Run, "chrome.exe" %targetURL% "--incognito"
while !VarPID
   WinGet, VarPID, PID, New Incognito Tab
This works well as long as you are not expecting to have multiple incognito windows open, and your script quickly points the browser to some website. It will not work for headless mode though as it requires a window to target.

With a little luck, some wiser head will come along and give you a better answer that will allow headless mode.
Araphen
Posts: 5
Joined: 20 Apr 2024, 05:22

Re: Ahk isn't getting the PID i need for chrome

26 Apr 2024, 17:44

I really appreciate the response. Sucks that it doesn't work with headless processes though. If no big-brain person comes along with a better solution, that's probably what i'll go with

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], mikeyww, Renee88 and 138 guests