WinActivate "ahk_pid" not working?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Bull123
Posts: 15
Joined: 30 Jan 2022, 18:47

WinActivate "ahk_pid" not working?

23 Aug 2023, 16:06

The code:

Code: Select all

Run "notepad",,, &pid
WinActivate "ahk_pid " pid
msgbox("finished")
gives the error in the image. Anyone know why?? Is there an error in v2?

image.png
image.png (21.71 KiB) Viewed 838 times
Last edited by gregster on 23 Aug 2023, 16:10, edited 1 time in total.
Reason: Fixed image: Don't put img-tags around attachment tags - it's not necessary and will break the inline picture.
niCode
Posts: 318
Joined: 17 Oct 2022, 22:09

Re: WinActivate "ahk_pid" not working?

23 Aug 2023, 16:21

The program takes time to open. Trying to activate a window that isn't fully open yet will cause a problem. Try:

Code: Select all

Run "notepad",,, &pid
WinWaitActive "ahk_pid " pid
WinActivate "ahk_pid " pid
msgbox("finished")
Bull123
Posts: 15
Joined: 30 Jan 2022, 18:47

Re: WinActivate "ahk_pid" not working?

24 Aug 2023, 02:03

Does that code work for you? On my pc it never reaches the message box.

And the code

Code: Select all

Run "notepad",,, &pid
sleep 1000
WinActivate "ahk_pid " pid
msgbox("finished")
gives the same error as before, ie "Error: Target window not found." :(
User avatar
mikeyww
Posts: 27339
Joined: 09 Sep 2014, 18:38

Re: WinActivate "ahk_pid" not working?

24 Aug 2023, 09:52

Worked here. If you have somehow remapped Notepad to run a different program-- a Notepad replacement-- this might not work.

Tested on Win 10 Pro.
Bull123
Posts: 15
Joined: 30 Jan 2022, 18:47

Re: WinActivate "ahk_pid" not working?

24 Aug 2023, 11:04

Hm.. How strange

I am getting 95% certain there is an bug with the statement

Code: Select all

WinActivate "ahk_pid " pid
for my windows version. I am running autohotkey 2.0.3 and Windows 11 pro version 10.0.22621.

The reason why I think that is that

Code: Select all

Run "notepad",,, &pid

works fine when it runs alone. It opens notepad and populates pid with a number, which I can see when debugging or print in a message box when the script runs normally. This number is the same as can be found in task manager -> defails on my windows. I have also tried to run the script as admin, and tried some versions of SetTitleMatchMode without luck.

I made a small screen recording demoing the error i could email some devs if they want to look into it.
User avatar
MrDodel
Posts: 96
Joined: 28 Apr 2021, 09:03
Location: Event Horizon

Re: WinActivate "ahk_pid" not working?

24 Aug 2023, 11:34

@Bull123, are you running Win11 by anychance ?

I'm running 10 on my laptop it works fine, On Win 11 (22H2 22621.2134) I get the same error.
So much universe, and so little time. GNU Sir Terry.
gregster
Posts: 9104
Joined: 30 Sep 2013, 06:48

Re: WinActivate "ahk_pid" not working?

24 Aug 2023, 11:55

Perhaps there is some kind of launcher process on Win11 and the actual notepad window gets a different PID.
You could check with WinGetPID.

Afaik, Microsoft has changed a couple of things about Notepad on Win11.

I would also check if

Code: Select all

Run "notepad.exe",,, &pid
changes anything.
niCode
Posts: 318
Joined: 17 Oct 2022, 22:09

Re: WinActivate "ahk_pid" not working?

24 Aug 2023, 12:01

MrDodel wrote:
24 Aug 2023, 11:34
On Win 11 (22H2 22621.2134) I get the same error.
I'm on that exact version and it works fine for me. Strange :think:
Bull123
Posts: 15
Joined: 30 Jan 2022, 18:47

Re: WinActivate "ahk_pid" not working?

25 Aug 2023, 06:02

@niCode/MrDodel: How strange!
@gregster: Great tip about checking WinGetID. That seems to have spesified the problem somewhat.

Running the code

Code: Select all

Run "notepad.exe",,, &pid_from_run_command
sleep 1500
pid_from_wingetid := WinGetPID("Notepad")
gives to different values for pid_from_run_command and pid_from_wingetid

In task manager, I see that there are two processes for notepad.
One with pid_from_run_command and the other with pid_from_wingetid.

And pid_from_run_command seems to refere to the wrong process :wtf:
230825125510.jpg
230825125510.jpg (23.11 KiB) Viewed 651 times
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: WinActivate "ahk_pid" not working?

25 Aug 2023, 09:24

Bull123 wrote:
25 Aug 2023, 06:02
Running the code

Code: Select all

Run "notepad.exe",,, &pid_from_run_command
sleep 1500
pid_from_wingetid := WinGetPID("Notepad")
gives to different values for pid_from_run_command and pid_from_wingetid

In task manager, I see that there are two processes for notepad.
One with pid_from_run_command and the other with pid_from_wingetid.

And pid_from_run_command seems to refere to the wrong process :wtf:
 
Same for me in Win 11 (21H2 22000.2295).
Additional:
Killing pid_from_run_command (from task manager) doesn't seem to affect the open notepad window
I get 2 processes if I launch it in cmd prompt or ahk Run() function, but only a single process when launched with Run dialog (Win+R) :o
User avatar
mikeyww
Posts: 27339
Joined: 09 Sep 2014, 18:38

Re: WinActivate "ahk_pid" not working?

25 Aug 2023, 09:35

Code: Select all

; This script shows how to wait for a new instance of a program's window
#Requires AutoHotkey v2.0
app      := A_WinDir '\System32\notepad.exe'
winTitle := 'ahk_exe' app
win1     := WinGetList(winTitle)
Run app
Loop {
 Sleep 250
 win2 := WinGetList(winTitle)
} Until win2.Length > win1.Length
WinActivate win2[1]
MsgBox 'Done!', 'Status', 64
Bull123
Posts: 15
Joined: 30 Jan 2022, 18:47

Re: WinActivate "ahk_pid" not working?

04 Sep 2023, 04:18

Thanks, @mikeyww!

I ended up with a script greatly inspired by yours, but I made some slight changes.
I made a function find_elem_in_list1_not_in_list2 that simply found which element of list1 that is not a part of list2. It turned out for me that it was not nesessarily the first element of the list.

Code: Select all

winlist_pre_appstart:= WinGetList(ahk_exe)
Run commandstring  ;for instance "firefox.exe https://somewebsite.com"
sleep_accumulated:=0
Loop {
	Sleep 150
	sleep_accumulated+=150
	if sleep_accumulated > MAX_SLEEP_TIME_IN_RUN_COMMANDSTRING_AND_ACTIVATE_WINDOW   ; MAX_SLEEP... is just a numeric value
		{
		break
		}
	winlist_current := WinGetList(ahk_exe)
} Until winlist_current.Length > winlist_pre_appstart.Length
elem_in_list1_no_in_list2:=find_elem_in_list1_not_in_list2(winlist_current,winlist_pre_appstart)

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada, Smile_ and 22 guests