Storing PID of a window
Storing PID of a window
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
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
- JoeWinograd
- Posts: 2241
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: Storing PID of a window
Use the WinGet command with the PID subcommand.
Re: Storing PID of a window
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..
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]
- JoeWinograd
- Posts: 2241
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: Storing PID of a window
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
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
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
- JoeWinograd
- Posts: 2241
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: Storing PID of a window
Maybe, or maybe it's me, but I am definitely not understanding what you want.blad4 wrote:Maybe I am not explaining myself adequately
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
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'
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'
- JoeWinograd
- Posts: 2241
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: Storing PID of a window
Very helpful!blad4 wrote:pseudo-code
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
- Attachments
-
- TaskbarList-jw.ahk
- (3.33 KiB) Downloaded 23 times
Re: Storing PID of a window
Jesus Christ! How did you do that so quick? The script looks insane!
And then just the desired PID, so it has been quite manual up until now.
At the moment I useJoeWinograd wrote: ↑14 Apr 2024, 11:43One 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"blad4 wrote:pseudo-code
Code: Select all
If WinActive("ahk_id 0x131096")
Code: Select all
WinActivate
Re: Storing PID of a window
@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:
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
- JoeWinograd
- Posts: 2241
- Joined: 10 Feb 2014, 20:00
- Location: U.S. Central Time Zone
Re: Storing PID of a window
*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:How did you do that so quick?
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).blad4 wrote:WinActive("ahk_id 0x131096")
Re: Storing PID of a window
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
;==============