Storing PID of a window

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
blad4
Posts: 315
Joined: 07 Oct 2015, 11:06

Storing PID of a window

13 Apr 2024, 13:21

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
User avatar
JoeWinograd
Posts: 2241
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Storing PID of a window

13 Apr 2024, 13:33

Use the WinGet command with the PID subcommand.
garry
Posts: 3852
Joined: 22 Dec 2013, 12:50

Re: Storing PID of a window

13 Apr 2024, 13:36

maybe also a script from user @SKAN > PIDfromAnyID()
viewtopic.php?style=1&t=3533
blad4
Posts: 315
Joined: 07 Oct 2015, 11:06

Re: Storing PID of a window

13 Apr 2024, 14:25

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]
User avatar
JoeWinograd
Posts: 2241
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Storing PID of a window

13 Apr 2024, 15:15

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?
blad4
Posts: 315
Joined: 07 Oct 2015, 11:06

Re: Storing PID of a window

13 Apr 2024, 16:11

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
User avatar
JoeWinograd
Posts: 2241
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Storing PID of a window

13 Apr 2024, 17:31

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. :)
blad4
Posts: 315
Joined: 07 Oct 2015, 11:06

Re: Storing PID of a window

14 Apr 2024, 09:09

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'
User avatar
JoeWinograd
Posts: 2241
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Storing PID of a window

14 Apr 2024, 11:43

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
Attachments
TaskbarList-jw.ahk
(3.33 KiB) Downloaded 22 times
blad4
Posts: 315
Joined: 07 Oct 2015, 11:06

Re: Storing PID of a window

14 Apr 2024, 13:06

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

Code: Select all

If WinActive("ahk_id 0x131096")
And then just

Code: Select all

WinActivate
the desired PID, so it has been quite manual up until now.
Descolada
Posts: 1431
Joined: 23 Dec 2021, 02:30

Re: Storing PID of a window

14 Apr 2024, 14:07

@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
User avatar
JoeWinograd
Posts: 2241
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Storing PID of a window

14 Apr 2024, 14:38

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).
garry
Posts: 3852
Joined: 22 Dec 2013, 12:50

Re: Storing PID of a window

14 Apr 2024, 15:14

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
;============== 

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo, Google [Bot] and 152 guests