Toggle Program to minimize and maximize

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Dogeatdogdog
Posts: 8
Joined: 20 Jul 2022, 15:23

Toggle Program to minimize and maximize

Post by Dogeatdogdog » 06 Aug 2022, 12:17

Below is my current script to minimize and maximize however, i need help making it a bit smarter by checking the state of the window first. example: if the program is minimized, then maximize it and vice versa. i currently find myself pressing the key twice as sometimes the program is already either minimized or maximized.

Code: Select all

toggle:=!toggle
If toggle=1
{
Run, <Program>
WinMaximize, <Program>
WinActivate <Program>
}
Else
{
WinMinimize, <Program>
}
Return
i hope someone will be kind enough to help with my script please.


[Mod edit: Added [code][/code] tags]


Dogeatdogdog
Posts: 8
Joined: 20 Jul 2022, 15:23

Re: Toggle Program to minimize and maximize

Post by Dogeatdogdog » 07 Aug 2022, 20:59

Cheers Mikey!

for anyone else who needs a script to check if a program is running and if not run it. Then toggle between minimize and maximize, you can use the below.

Code: Select all

IfWinNotExist, <YOUR PROGRAM TITLE>
Run, <YOUR PROGRAM PATH>
IfWinNotActive, <YOUR PROGRAM TITLE>
	WinActivate, <YOUR PROGRAM TITLE>
else
	WinMinimize, <YOUR PROGRAM TITLE>
Return

User avatar
mikeyww
Posts: 26881
Joined: 09 Sep 2014, 18:38

Re: Toggle Program to minimize and maximize

Post by mikeyww » 08 Aug 2022, 06:12

WinMaximize never appears in your script. You might want to go with something like this.

Code: Select all

app = %A_WinDir%\System32\notepad.exe
F3::
If WinExist("ahk_exe" app)
 If !WinActive() {
  WinMaximize
  WinActivate
 } Else WinMinimize
Else Run, %app%,, Max
Return

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Toggle Program to minimize and maximize

Post by wetware05 » 08 Aug 2022, 06:25

Hi mikeyww.

I've tried your ulimoi gion and hitting F3 opens a new instance of notepad. Sy if it occurs again, without closing the previous ones, it opens another new instance, and so on ad infinitum. Same with the Dogeatdogdog script. Tested on Windows 11 X64.

User avatar
mikeyww
Posts: 26881
Joined: 09 Sep 2014, 18:38

Re: Toggle Program to minimize and maximize

Post by mikeyww » 08 Aug 2022, 06:31

Not here (Win10 Pro x64), but that could happen if you have multiple possible paths to the program, or if the process name used by the program changes (as some programs run multiple processes). The following may rectify it for you.

Code: Select all

app := A_WinDir "\System32\" proc := "notepad.exe"
F3::
If WinExist("ahk_exe" proc)
 If !WinActive() {
  WinMaximize
  WinActivate
 } Else WinMinimize
Else Run, %app%,, Max
Return
If it does not work for you, you can add lines to identify the values of the WinExist & WinActive functions.

A potential bug in the Dogeatdogdog script is that after the program is run, the remaining code executes. This is likely unintended.

Microsoft Store apps have special paths that may require adjustments.

wetware05
Posts: 750
Joined: 04 Dec 2020, 16:09

Re: Toggle Program to minimize and maximize

Post by wetware05 » 08 Aug 2022, 07:06

Now it works, mikeyww. :thumbup:

Dogeatdogdog
Posts: 8
Joined: 20 Jul 2022, 15:23

Re: Toggle Program to minimize and maximize

Post by Dogeatdogdog » 08 Aug 2022, 14:57

Hi Mikey,

I tried your script and it replaced it with my path but i couldnt get it to run. i decided to omit WinMaximize so it would remember the state it was in and just bring it to the front as i found i needed WinActivate otherwise it would be stuck behind other windows although it was technically maximized.

i think it does what i intend although if i have written it in a way that doesnt make sense please let me know, what i want is for it to check if the program is running, if not run it then bring it to the foreground. if i press the key again i want it to minimize. i then want it to come to the foreground and minimize as a toggle. one important thing is i want it to check the state first so i dont have to press the key twice or if i manually minimize or maximize, the script will know and do the opposite.

User avatar
mikeyww
Posts: 26881
Joined: 09 Sep 2014, 18:38

Re: Toggle Program to minimize and maximize

Post by mikeyww » 08 Aug 2022, 15:36

If your script works, then go with it. If it does not work, you can post it below.

Konrad
Posts: 1
Joined: 12 Apr 2024, 07:48

Re: Toggle Program to minimize and maximize

Post by Konrad » 12 Apr 2024, 08:01

Hi,
i just upgraded from Win 10 to Win 11 and now my script isn't working anymore.

My code is:

Code: Select all

app := "C:\Program Files (x86)\eM Client\" proc := "MailClient.exe"
F4::
If WinExist("ahk_exe" proc)
 If !WinActive() {
  WinMaximize
  WinActivate
 } Else WinMinimize
Else Run, %app%,, Max
Return
The script does nothing, but when i maximize eMClient, then i can minimize it with F4. But one thing i have observed: In the Win Taskmanager there are a lot of processes "MailClient.exe" that cannot removed or killed. Every time i start the script, one mor process of "MailClient" is added.

What can i do to get worked the script?

Thanks a lot

Konrad

User avatar
mikeyww
Posts: 26881
Joined: 09 Sep 2014, 18:38

Re: Toggle Program to minimize and maximize

Post by mikeyww » 12 Apr 2024, 09:16

Perhaps:

Code: Select all

#Requires AutoHotkey v1.1.33.11
EnvGet pf86, ProgramFiles(x86)
app      := pf86 "\eM Client\" proc := "MailClient.exe"
winTitle := "ahk_class WindowsForms10.Window.8.app.0.129c866_r3_ad1 ahk_exe" proc

F3::
If WinExist(winTitle)
 If !WinActive() {
  WinMaximize
  WinActivate
 } Else WinMinimize
Else {
 Run % app,, Max
 SoundBeep 1500
}
Return
If you are minimizing to tray, then this script might need adjustment to look for the process and activate it. It might be easier not to minimize to tray.

Post Reply

Return to “Ask for Help (v1)”