If WinExist(), WinClose not working in a loop? Zero-width space at fault? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
abiostudent3
Posts: 26
Joined: 07 Jul 2023, 17:52

If WinExist(), WinClose not working in a loop? Zero-width space at fault?

Post by abiostudent3 » 07 Dec 2023, 17:36

Hello! Hopefully I'm missing something simple here. I have a list of scripts that I want to make mutually exclusive. Here's the example - please note that they have varying numbers of zero-width spaces before the filename so that Windows will sort them in the "correct" order. EDIT: I just tested with some example scripts that don't have the zero-width spaces, and that's replicating the exact same issues.

Code: Select all

Character1 := A_ScriptDir "\​​​​​​​​​​​​Example 1.ahk"
Character2 := A_ScriptDir "\​​​​​​​​​​​Example 2.ahk"
Character3 := A_ScriptDir "\​​​​​​​​​​Example 99.ahk"
Character4 := A_ScriptDir "\​​​​​​​​​​Example 10.ahk"
Character5 := A_ScriptDir "\​​​​​​​​​Example.ahk"
Previously, the way I was making them mutually exclusive was by having a list of 'if' statements that I would copy into each script before deleting the one that corresponded with the script it was in:

Code: Select all

if WinExist(Character1)	; Delete the two lines that correspond with *this* script.
	WinClose
if WinExist(Character2)
	WinClose
if WinExist(Character3)
	WinClose
if WinExist(Character5)		; Skipping Character 4 as though this were in the script for Example 10.
	WinClose
This method works perfectly, even with the zero-width spaces in script names. However, as the list of scripts has grown, this has become untenable, and I wanted to use a loop to clean up the code. Here's the loop that I have, which seems to be working, just not closing the scripts.

Code: Select all

ScriptNumber := 1
loop 15		; Increase this number if you add more scripts.
	{
	if ScriptNumber = 0		; Change this for the number that corresponds with *this* script.
		{
		++ScriptNumber
		}
	else
		{
		Current := ("Character" Scriptnumber)
		if WinExist(Current)
			WinClose
		++ScriptNumber
		}
	}
When I used a MsgBox to check the value of 'Current,' it was updating and counting correctly. So, can WinExist not be used with a concatenated variable like that, or is my syntax wrong, or...?

Thanks for any input you might have!

Edit: Also, here's the header that I've got:

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance Force
DetectHiddenWindows(true)
And this comes after the whole mutual exclusivity bit, which I think shouldn't be interfering, but hey:

Code: Select all

^!+x::ExitApp()

#HotIf WinActive("Game")

User avatar
Seven0528
Posts: 455
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: If WinExist(), WinClose not working in a loop? Zero-width space at fault?

Post by Seven0528 » 08 Dec 2023, 00:14

Code: Select all

#Requires AutoHotkey v2.0
#SingleInstance

Loop 15    {
    currProcessPath:=A_ScriptDir "\Example " format("{:02}",A_Index) ".ahk"
    ;  Msgbox currProcessPath
}

 WinGetList
 WinGetProcessPath
By the way, does inserting the path directly into the parameters of WinExist work?
I might need to use a different approach though...
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

Rohwedder
Posts: 7824
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: If WinExist(), WinClose not working in a loop? Zero-width space at fault?  Topic is solved

Post by Rohwedder » 08 Dec 2023, 05:54

Hallo,
try:

Code: Select all

#Requires AutoHotkey v2.0
Character1 := A_ScriptDir "\​​​​​​​​​​​​Example 1.ahk"
Character2 := A_ScriptDir "\​​​​​​​​​​​Example 2.ahk"
Character3 := A_ScriptDir "\​​​​​​​​​​Example 99.ahk"
Character4 := A_ScriptDir "\​​​​​​​​​​Example 10.ahk"
Character5 := A_ScriptDir "\​​​​​​​​​Example.ahk"
q:: ; close Character 1-5
{
	DetectHiddenWindows True
	SetTitleMatchMode 2
	Loop 5
		Try WinClose Character%A_Index% " -" 
}

User avatar
Seven0528
Posts: 455
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: If WinExist(), WinClose not working in a loop? Zero-width space at fault?

Post by Seven0528 » 08 Dec 2023, 06:14

 Main Window Title
Oh, hearing that reminded me. Each AHK has its own Main Window Title.
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.

abiostudent3
Posts: 26
Joined: 07 Jul 2023, 17:52

Re: If WinExist(), WinClose not working in a loop? Zero-width space at fault?

Post by abiostudent3 » 10 Dec 2023, 19:40

Hi there, sorry it's taken me a couple days to reply. I've had a nasty cold.

Code: Select all

Loop 15    {
    currProcessPath:=A_ScriptDir "\Example " format("{:02}",A_Index) ".ahk"
    ;  Msgbox currProcessPath
}
@Seven0528

I... don't understand what that's doing. I get that it's going through the local script directory, looking for any .ahk running with that location, and somehow counting up to 15 due to A_Index... but what is " format("{:02}",A_Index)" actually doing? (Oh, and inserting the path directly into the parameters of WinExist absolutely works, at least in the simpler case that I was using previously. I couldn't get it to work, though - the message box just popped up with, "Example00" and that was all I got.


@Rohwedder

That almost works brilliantly, but I don't understand why. SetTitleMatchMode 2 makes sense, but doesn't work on its own, so clearly it's something in the Try WinClose that I don't understand. The way of counting up Character%A_Index% is brilliant, but what's the dash in quotes after it for? And why use Try?

This will still close the script that's trying to run, but I can just add in an if based off the index. Thank you!

Thanks so much to both of you for the help!

Rohwedder
Posts: 7824
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: If WinExist(), WinClose not working in a loop? Zero-width space at fault?

Post by Rohwedder » 11 Dec 2023, 02:37

Try WinClose Character%A_Index% " -"
Regarding Try WinClose
https://www.autohotkey.com/docs/v2/lib/WinClose.htm#Error_Handling
A TargetError is thrown if the window could not be found
The attempt to eliminate a non-existent game's character does not lead to this error thanks to Try .
Regarding " -"
Only the character script's main windows should be closed.
During my programming, the script attacked the character's test script subwindow in my Notepad++ editor. That's why I made the title more unambiguous.

abiostudent3
Posts: 26
Joined: 07 Jul 2023, 17:52

Re: If WinExist(), WinClose not working in a loop? Zero-width space at fault?

Post by abiostudent3 » 11 Dec 2023, 14:16

Rohwedder wrote:
11 Dec 2023, 02:37
Try WinClose Character%A_Index% " -"
Regarding Try WinClose
https://www.autohotkey.com/docs/v2/lib/WinClose.htm#Error_Handling
A TargetError is thrown if the window could not be found
The attempt to eliminate a non-existent game's character does not lead to this error thanks to Try .
Regarding " -"
Only the character script's main windows should be closed.
During my programming, the script attacked the character's test script subwindow in my Notepad++ editor. That's why I made the title more unambiguous.
Gotcha, thank you so much for the explanation and links!

For anybody who comes across this in the future, here's the final code that I ended up implementing:

Code: Select all

loop 15
	{
	if A_Index = 1			; Change this to reflect this particular script. A_Index = 3 corresponds to Character3.
		continue
	else
		Try WinClose Character%A_Index% " -" 
	}

Post Reply

Return to “Ask for Help (v2)”