Page 1 of 1
Storing PID of a window
Posted: 13 Apr 2024, 13:21
by blad4
Currently I use "#1", "#2" etc activate specific windows which are open
Is there a way to store the PID of each window as a variable? I cannot find any old topics on the matter, it would be pretty cool
Re: Storing PID of a window
Posted: 13 Apr 2024, 13:33
by JoeWinograd
Use the WinGet command with the PID subcommand.
Re: Storing PID of a window
Posted: 13 Apr 2024, 13:36
by garry
maybe also a script from user
@SKAN > PIDfromAnyID()
viewtopic.php?style=1&t=3533
Re: Storing PID of a window
Posted: 13 Apr 2024, 14:25
by blad4
Thanks guys, but how do I connect WinTitle, WinText, ExcludeTitle or ExclusdeTest to the order of the applications in the taskbar?
I can adapt this when I input WinTitle, for example, but all of these applications have the same WinTitle..
Code: Select all
WinGet, OutputVar, PID , [WinTitle, WinText, ExcludeTitle, ExcludeText]
Re: Storing PID of a window
Posted: 13 Apr 2024, 15:15
by JoeWinograd
If they all have the same Title, how do you want to refer to them so that you'll know which PID opens which window? If the only way to know is via their positions on the taskbar, I would think that #N is as good a method as any. It's certainly easy enough to search all the PIDs in the system and find those with the Title that you're looking for, but once you have all those PIDs (maybe in an associative array for easy lookup), how do you want to distinguish them so that you know which one to activate?
Re: Storing PID of a window
Posted: 13 Apr 2024, 16:11
by blad4
Hi thank you. Maybe I am not explaining myself adequately, and your replies are certainly helping me.
The taskbar placement is the only distinguishable factor for me, since I may open/close programs, pin/unpin and move them around on the taskbar.
This is why my desire is to find a link between #N and PID
Re: Storing PID of a window
Posted: 13 Apr 2024, 17:31
by JoeWinograd
blad4 wrote:Maybe I am not explaining myself adequately
Maybe, or maybe it's me, but I am definitely not understanding what you want.
For looking at all the buttons on the taskbar by position number, check out this script:
viewtopic.php?t=94563#p419941
The test MsgBox in there displays all the Titles on the taskbar in position order...1 to N. You may be able to modify that to do what you want...although I don't know what you want.
Re: Storing PID of a window
Posted: 14 Apr 2024, 09:09
by blad4
Thanks very much, that seems to be getting closer. This should be simpler, pseudo-code:
1::
- checks if there is a window in the taskbar, in the firstmost location
- retrieves the PID of said window
- stores the PID to a variable called 'pid1'
2::
- checks if there is a window in the taskbar, in the secondmost location
- retrieves the PID of said window
- stores the PID to a variable called 'pid2'
Re: Storing PID of a window
Posted: 14 Apr 2024, 11:43
by JoeWinograd
blad4 wrote:pseudo-code
Very helpful!
I don't know much about
Acc. There may be a way to get the PID inside the
For Each,Child loop. Let's hope an
Acc expert jumps in with that.
Attached is a minor modification of the script that shows the position number on the taskbar for each button. The MsgBox at the end now displays info like this:
1=Task Manager - 1 running window
2=Task Scheduler - 1 running window
3=Control Panel\All Control Panel Items - 1 running window
4=(3201) Storing PID of a window - AutoHotkey Community - Brave - 1 running window
5=AutoHotkey Help - 1 running window
Btw, instead of
pid1,
pid2, etc. (a pseudo-array), it's better to use a real array, imo, i.e.,
pid[n]. As you can see in the script I posted, that's what I did with the position variable, i.e.,
PosArray[n].
One thing I'm still not understanding: even if you have the PID, how are you going to know which window you're activating, since (1) "all of these applications have the same WinTitle" and (2) "I may open/close programs, pin/unpin and move them around on the taskbar".
Regards, Joe
Re: Storing PID of a window
Posted: 14 Apr 2024, 13:06
by blad4
Jesus Christ! How did you do that so quick? The script looks insane!
JoeWinograd wrote: ↑14 Apr 2024, 11:43
blad4 wrote:pseudo-code
One thing I'm still not understanding: even if you have the PID, how are you going to know which window you're activating, since (1) "all of these applications have the same WinTitle" and (2) "I may open/close programs, pin/unpin and move them around on the taskbar"
At the moment I use
And then just
the desired PID, so it has been quite manual up until now.
Re: Storing PID of a window
Posted: 14 Apr 2024, 14:07
by Descolada
@blad4 PID means Process ID, an unique identifier for a process (eg Notepad.exe) which in turn might own multiple windows (eg a window named "Untitled - Notepad" or "Cookbook - Notepad"). In your WinActive call you are using an ahk_id which is a window handle, an unique identifier of a single window.
Multiple AHK functions return window handles which you can then store in a variable and use later. Example:
Code: Select all
wId := WinExist("My window title")
; do stuff
WinActivate, ahk_id %wId% ; activates the same window that was found by WinExist previously
Re: Storing PID of a window
Posted: 14 Apr 2024, 14:38
by JoeWinograd
blad4 wrote:How did you do that so quick?
*I* did not. The bulk of the code was at the
link that I gave you earlier in this thread (which I thought you had checked out when I posted it, but obviously not). All I did was add a few lines of code to display the taskbar position number in the ending MsgBox.
blad4 wrote:WinActive("ahk_id 0x131096")
To be clear, that is
NOT a PID. You should check the
WinTitle doc. That param -
ahk_id - is the window or control (unique) handle, often referred to as
HWND (short for handle to window). The param for PID (Process ID) is
ahk_pid (all of this is at the
WinTitle doc).
Re: Storing PID of a window
Posted: 14 Apr 2024, 15:14
by garry
example , not sure it's correct (?)
Code: Select all
Detecthiddenwindows,on
settitlematchmode,2
scx :="MATHE" ;- TITLE
global scx
Gui,show,x500 y500 w300 h300,%scx%
WinGet,PID2,PID,%scx% ;- TITLE PID
WinWait, ahk_pid %pid2%
sleep,3000
WinMove, ahk_pid %pid2%, ,1,1,
return
;--------------
Guiclose:
Exitapp
;--------------
F8::
WinActivate,%scx%
return
;==============