So far I have:
Process, Exist notepad.exe
Process, Close, %p_id%
How do you set ahk to kill the process if it exists? I read it's something to do with the PID, but don't know how to implement that.
So far I have:
Process, Exist notepad.exe
Process, Close, %p_id%
How do you set ahk to kill the process if it exists? I read it's something to do with the PID, but don't know how to implement that.
Have a look at the Documentation.
You can kill by simply using the name of the process:
Process, Close, notepad.exe
If the process does not exist, it will do nothing.
If you still would like to kill the process by using the pid
instead, you must use the WinGet command in order to retrieve the pid
.
This AHK script kills the active process when pressing Ctrl+Alt+K:
^!k::
{
WinGet, xPID, PID, A
Process, Close, %xPID%
}
return
there are at least two ways to get the PID from a window that I can think of immediately
1:
WinGet, My_PID, PID, WinTitle
2:
Run, ProgramFilePath "Args", Options, My_PID
The first one is to get an already running window PID and the second is to get a PID when opening the program with AHK. In both cases the variable "My_PID" now contains the window process ID
To answer your question of closing a process if it exists you could try a couple of methods.
ifWinExist ahk_pid %My_PID%
Process, Close, %My_PID%
; OR
Process, Exist, %My_PID% ; from my examples above
;Process, Exist, notepad.exe ; from your example above
If ErrorLevel ; Errorlevel is set to matching PID if found
Process, Close, %ErrorLevel%
I think that should answer your immediate question