Gui show - hide when a specific program is active or not Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Gui show - hide when a specific program is active or not

31 Jan 2022, 03:42

Gui show - hide when a specific program is active or not.

This is an example with notepad, but i want to use it with Mp3tag which only has ahk_exe to work with.

What i want to do is:

1 Script stays at tray hidden when no notepad exist
2 Script shows up and stays at top of notepad (and all windows) when notepad starts and working with it
3 When i select the script window, it stays at top of notepad (and all windows) when notepad is already showing up and running
4 Script hides to tray when notepad losses focus or it is minimized
5 Script shows up and stays at top of notepad (and all windows) when notepad is refocused or maximized from taskbar
6 Script stays at tray hidden when i exit notepad

And all starts from the beginning...

The script works fine except from number 3 which makes the script and notepad flicker back and forth.

I can't find the solution myself, so if anyone can help i will by thankful.

Here is the script named as (newtest.ahk) that i have so far:

Code: Select all

#SingleInstance, Force
#NoEnv
SetWorkingDir %A_ScriptDir%

Gui +Resize +AlwaysOnTop +Owner
Gui, Show, W300 H500, newtest

Loop
{
    IfWinExist, ahk_exe Notepad.exe
        {
            ifWinActive, ahk_exe Notepad.exe
                {
                IfWinExist, ahk_exe newtest.ahk
                    {
                    Gui, show
                    }
                    Else
                    {
                    Gui, Restore
                    }
                }
            Else
                {
                ifWinNotActive, ahk_exe Notepad.exe
                Gui, Hide
                }
		}
    else
		{
		Gui, Hide
		}
}
return

GuiClose:
ExitApp
User avatar
boiler
Posts: 17227
Joined: 21 Dec 2014, 02:44

Re: Gui show - hide when a specific program is active or not

31 Jan 2022, 04:06

I can’t follow what you are trying to do, but this line doesn’t make sense:

Code: Select all

                IfWinExist, ahk_exe newtest.ahk
Process names end in .exe, they are not .ahk files. See for yourself:

Code: Select all

Gui +Resize +AlwaysOnTop +Owner
Gui, Show, x0 y0 W300 H500, newtest
IfWinExist, ahk_exe newtest.ahk
	Msgbox, Exists
else
	Msgbox, Doesn't exist
return

GuiClose:
ExitApp
User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Gui show - hide when a specific program is active or not

31 Jan 2022, 04:51

I explain what i want to do in the steps from my 1st post.

I was running the script from Adventure IDE to test it.
I changed the .ahk to .exe and converted and now run it as newtest.exe

I changed the script which is a bit better, but now i can't focus the script:

Code: Select all

#SingleInstance, Force
#NoEnv
SetWorkingDir %A_ScriptDir%

Gui +Resize +AlwaysOnTop +Owner
Gui, Show, W300 H500, newtest

Loop
{
    IfWinExist, ahk_exe Notepad.exe
        {
            ifWinActive, ahk_exe Notepad.exe
                {
                ifWinActive, ahk_exe newtest.exe
                    {
                    Gui, show
                    }
                    Else
                    {
                    Gui, show, NoActivate
                    }
                }
            Else
                {
                    Gui, Hide
                }
		}
    else
		{
		Gui, Hide
		}
}
return

GuiClose:
ExitApp
User avatar
boiler
Posts: 17227
Joined: 21 Dec 2014, 02:44

Re: Gui show - hide when a specific program is active or not

31 Jan 2022, 10:49

I believe this works as you want it to:

Code: Select all

#SingleInstance, Force
#NoEnv
SetWorkingDir %A_ScriptDir%

Gui +Resize +AlwaysOnTop +Owner
Gui, Show, W300 H500 Hide, newtest

loop {
	if !WinExist("ahk_exe notepad.exe")
		Gui, Hide
	if WinActive("newtest ahk_class AutoHotkeyGUI")
		continue
	if WinActive("ahk_exe notepad.exe")
		Gui, Show, NoActivate
	if !WinActive("ahk_exe notepad.exe") && !WinActive("newtest ahk_class AutoHotkeyGUI")
		Gui, Hide
}
return

GuiClose:
ExitApp
User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Gui show - hide when a specific program is active or not

31 Jan 2022, 11:32

Yes!!!
The beauty of simplicity.
A big thank you.

BUT. When i click back and forth from script and notepad randomly the script hides when activates.
Should i put a wait somewhere?
User avatar
boiler
Posts: 17227
Joined: 21 Dec 2014, 02:44

Re: Gui show - hide when a specific program is active or not  Topic is solved

31 Jan 2022, 11:50

Yes, you're right. Apparently, sometimes we catch it right in between the two WinActive checks separated by &&. Sort of a fluky thing, but apparently not too hard to catch it in between. Incorporating your suggestion to wait a bit then check it again before hiding it seems to eliminate the issue for me:

Code: Select all

#SingleInstance, Force
#NoEnv
SetWorkingDir %A_ScriptDir%

Gui +Resize +AlwaysOnTop +Owner
Gui, Show, W300 H500 Hide, newtest

loop {
	if !WinExist("ahk_exe notepad.exe")
		Gui, Hide
	if WinActive("newtest ahk_class AutoHotkeyGUI")
		continue
	if WinActive("ahk_exe notepad.exe")
		Gui, Show, NoActivate
	if !WinActive("ahk_exe notepad.exe") && !WinActive("newtest ahk_class AutoHotkeyGUI") {
		Sleep, 100
		if !WinActive("ahk_exe notepad.exe") && !WinActive("newtest ahk_class AutoHotkeyGUI")
			Gui, Hide
	}
}
return

GuiClose:
ExitApp
User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Gui show - hide when a specific program is active or not

31 Jan 2022, 12:03

Smooth like a butter!!!
Again a big thank you.
User avatar
boiler
Posts: 17227
Joined: 21 Dec 2014, 02:44

Re: Gui show - hide when a specific program is active or not

31 Jan 2022, 12:07

You're welcome. Glad to help. Note (as you probably have already) that you don't need to compile the script since the way the GUI window is referenced in the above script works with the uncompiled script.
User avatar
Stem75
Posts: 57
Joined: 18 Dec 2021, 02:37

Re: Gui show - hide when a specific program is active or not

31 Jan 2022, 12:22

Yes that is true. But i am what i am and i couldn't help it not checking it both ways.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], braunbaer, gero and 127 guests