
Wait for window to open then use WinGetTitle.
For example, there is a folder of games but the names are game1.exe, game2.exe... I have a script to read the names of the files in the folder, launch a file and then add specific controls to a gui depending on which game opens.
What I can't figure out is how to wait for let's say game3.exe to fully open to be able to use WinGetTitle. Once I get the title I can control the window but if WinGetTitle runs before the window fully opens I get the name of my launching script instead.
So, how do you wait for a window that you only know the exe name and not the window name?

Run, %someexe%, , , PID [color=red]WinWait, ahk_pid %PID% WinGetText, wTitle, ahk_pid %PID%[/color]
Keep in mind it won't wait for the window to finish loading, just for it to exist.

Thanks sinkfaze

I tried using the same method to select a bunch of misc AHK scripts that have gathered in a folder and open them from a drop down list. I discovered that this method only works for exe files.
I made this script to test with.
Gui, Add, DDL, vopenapp gopenapp, App_1.ahk|App_2.ahk|App_1.exe|App_2.exe Gui, Add, Button, x+10 gReload, R Gui, Add, Edit, xm w280 ReadOnly vtext, Gui, Show, y0 w300 Return openapp: Gui, Submit, NoHide Run, %openapp%, , , PID WinWait, ahk_pid %PID%, , 5 If ErrorLevel = 1 { Guicontrol, , text, Error retrieving window info of %openapp%. Error Level %ErrorLevel%. Return } WinGetTitle, wintitle, ahk_pid %PID% GuiControl, , text, The PID and Name of %openapp% is %PID% and %wintitle%. Return Reload: Reload Return GuiClose: Gui, Destroy ExitAppThe target scripts and exes are just scripts I made for testing that have names like "Window 1", "Window 2" ect.
The exe scripts work fine but the AHK script time out and get errors. I spent a little time trying to use "A_ScriptName" but haven't been able to get it to work either. I can only get it to return the name of this script.
I should mention that my goal here is to close an open script when I select a new one from the DDL. The problem I was having is if I had the script open in Notepad++ when I selected another from the DDL Noteapd++ closed instead of the intended script. I need to positively identify the name of the script opening but it may never get a chance to activate if I'm working in the main script.
Is there a similar method to wait for an AHK script to open then get the title?


On trick i found that works with 'WinWait' is to also add some text to look for. Sometimes WinWait will trigger before the window has fully loaded. So the best solution is to add to WinWait some text you would see on the screen. This will delay the triggering until the text is also present on the screen (i.e., the program has more fully loaded)
Thanks but I was actually using that method. Since Notepad++ also contained that text it was still closed. This also meant that I had to edit every script and and the same line of text so they would all be recognized.
I need to come up with a way so that when I launch a script from my DDL I can be absolutely certain it is what I will be controlling from my main script. I'm trying to avoid having to edit the scripts individually but what I may end up do is adding a "PostMessge" to the autoexec of each one.


Maybe the Process command will help. You can use it to get the PID for a specific program you launch and then sync that PID with the window you want
Run, %newwindow% Process, Wait, %newwindow%, 5 Process, Exist, %newwindow%,Using this I'm getting the same results. Works for exe files not for ahk files.
Thanks for showing me that though. I can see it being very handy in the future.

WinGetTitle, title, ahk_pid %id% if instr(title, ".ahk") MsgBox, 262144,, title = %title%you will get the name of every ahk script that is running. Hopefully, you can now use that list technique to do what you want since you will now have the actual script name of any and every running AHK script!

If you look at the last example in AHK help for 'Process' you will find code to loop through all running processes (make sure you set DetectHiddenWindows, On first). If you insert the following into the loop for that example (after each pid is retrieved):
WinGetTitle, title, ahk_pid %id% if instr(title, ".ahk") MsgBox, 262144,, title = %title%you will get the name of every ahk script that is running. Hopefully, you can now use that list technique to do what you want since you will now have the actual script name of any and every running AHK script!
It almost works. I put it in my script as a gosub label and it does pop up a message box with the main script's name but it does not see any other running scripts. I delayed it running for 5 seconds to give the new window time to open but it still doesn't see it.
I GIVE UP! This will probably work with some tweaking but I'm to the point I'm just sick of working on it. I spent 9pm to 3am working on it last night and 10am to now 7pm just trying to figure out this one function and I'm at the point it's not worth it anymore. Let it run with errors.
Thanks for you help.

WinGetTitle, title, ahk_pid %id% if instr(title, thePartialNameOfAHKScript) break ; match found for the ahk script you were looking for to be running!the process checking loop will be endlessly repeating to query all running processes looking for you ahk script name. It keeps looping it until you find a hit (or a timeout or other error check etc. if you don't see the ahk script running) then you can break out of the loop. For a new ahk file to look for, just restart the loop with a new ahk name to look for.
Note: there will be two loops. The outer endless loop (until you break out of it) simply keeps running the inner loop of checking each running process (from the Process example)

WinGetTitle, title, ahk_pid %id% if instr(title, ".ahk") ahkList = %ahkList%`n%title%; add the ahk script window name to the list
