Tweak Maximize/Minimize hotkey

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Tweak Maximize/Minimize hotkey

Post by AHKJeff » 07 Aug 2022, 14:39

I have many hotkeys assigned to open/max/min my apps.

All of them work as expected except for one.
I hope someone here can help me tweak it so it behaves like the others.

The hotkey that needs tweaking will Open the app if it hasn't been launched.
But it won't maximize it, if it has been minimized, nor the reverse.

I will post the code for the one that needs tweaking first, then code for a couple that work
perfectly.

Code: Select all

Launch_Media::
open := False
For each, file in ["Political", "Guelph3a-Phone-Gen"] 
{
 winTitle = Ecco - [D:\Computer\Software\Ecco\%file%.eco]
 WinGet, state, MinMax, %winTitle%
 If (state = "")
  Continue
 open := True
 If (state = MINIMIZED := -1)
  ; WinRestore, %winTitle%
    WinActivate, %winTitle%   
   Else WinMinimize, %winTitle%
}
If !open
 Run, C:\Program Files\Utils\Ecco\EccoPro\eccoext.exe
Return

Code: Select all

Browser_Favorites::    ;show/minimize Total Commander (x64) 10.00 - Mark
IfWinNotExist, Total Commander (x64) 10.00 - Mark
{
Run, C:\Program Files\Utils\Totalcmd10\TOTALCMD64.EXE
return
}
else
{
WinGet,CurrentState,MinMax, Total Commander (x64) 10.00 - Mark
if (CurrentState = -1) ; The window is minimized
   WinRestore, Total Commander (x64) 10.00 - Mark
else
   WinMinimize, Total Commander (x64) 10.00 - Mark
}
Return

Code: Select all

Launch_Mail::    ;show/minimize The Bat!

IfWinNotExist, The Bat!
{
SoundSet 8
Run, C:\Program Files\The Bat!\thebat64.exe
return
}
else
{
SoundSet 8
WinGet,CurrentState,MinMax, The Bat!
if (CurrentState = -1) ; The window is minimized
   ;WinRestore, The Bat!
   WinActivate, The Bat!
else
   WinMinimize, The Bat!
}
Return

User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Tweak Maximize/Minimize hotkey

Post by Xtra » 07 Aug 2022, 16:42

Use: WinRestore before min/max.

AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Re: Tweak Maximize/Minimize hotkey

Post by AHKJeff » 07 Aug 2022, 17:52

Xtra wrote:
07 Aug 2022, 16:42
Use: WinRestore before min/max.
Thanks Xtra for helping.
If you look at the top code above you will see I had commented out the Restore code.
I must have done it because it was giving me problems before.

As soon as I uncommented it, I got this error:

Error: ELSE with no matching IF

It's the ELSE eight lines before the end of the code.

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Tweak Maximize/Minimize hotkey

Post by boiler » 07 Aug 2022, 17:58

To have more than one line following an if statement or similar to be conditional to it, you have to define a code block using { }. Otherwise, only the line immediately following the if is associated with it, so then the else is just floating on its own after something no longer part of it. See examples at the linked page. You have done so after your use of IfWinNotExist. It would be the same thing.

AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Re: Tweak Maximize/Minimize hotkey

Post by AHKJeff » 07 Aug 2022, 18:25

OK, sorry but this is confusing.
I'm not sure which version of my AHK.ahk file
AHK is running!

When I try to make a change in the one that is running (bottom of screen) and re-load it,
I get the error about ELSE.

But when I open the AHK.ahk file I can't find the offending line!!!

Will keep searching and get back to you.

How do you locate the AHK.ahk file that
AutoHotkey.exe runs?

AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Re: Tweak Maximize/Minimize hotkey

Post by AHKJeff » 07 Aug 2022, 18:51

OK I hope I am using the right AHK.ahk file now.

I have changed the code to match another key that works.

Here it is:

Code: Select all

Launch_Media::

IfWinNotExist, Ecco - [D:\Computer\Software\Ecco\%file%.eco]
{
Run, C:\Program Files\Utils\Ecco\EccoPro\eccoext.exe
return
}
else
{
winTitle = Ecco - [D:\Computer\Software\Ecco\%file%.eco]
WinGet,CurrentState,MinMax, %winTitle%
if (CurrentState = -1) ; The window is minimized
   WinRestore, %winTitle%
else
   WinMinimize, %winTitle%
}
return
But I have the same problem with it.
It will launch the app, but it won't min or max the window.

Here's one that works:

Code: Select all

Launch_Mail::    ;show/minimize The Bat!

IfWinNotExist, The Bat!
{
SoundSet 8
Run, C:\Program Files\The Bat!\thebat64.exe
return
}
else
{
SoundSet 8
WinGet,CurrentState,MinMax, The Bat!
if (CurrentState = -1) ; The window is minimized
   ;WinRestore, The Bat!
   WinActivate, The Bat!
else
   WinMinimize, The Bat!
}
return

User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Tweak Maximize/Minimize hotkey

Post by Xtra » 07 Aug 2022, 18:59

Try:

Code: Select all

Launch_Media::

IfWinNotExist, Ecco - [D:\Computer\Software\Ecco\%file%.eco]
{
    Run, C:\Program Files\Utils\Ecco\EccoPro\eccoext.exe
    return
}
else
{
    winTitle = Ecco - [D:\Computer\Software\Ecco\%file%.eco]
    WinGet,CurrentState,MinMax, %winTitle%
    if (CurrentState = -1) ; The window is minimized
    {
        WinRestore, %winTitle%
        WinActivate, %winTitle%    ; or WinMaximize
    }
    else
    {
        WinRestore, %winTitle%
        WinMinimize, %winTitle%        
    }
}
return

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Tweak Maximize/Minimize hotkey

Post by boiler » 07 Aug 2022, 19:57

AHKJeff wrote: OK, sorry but this is confusing.
I'm not sure which version of my AHK.ahk file
AHK is running!

How do you locate the AHK.ahk file that
AutoHotkey.exe runs?
Don’t have multiple versions of files named AHK.ahk. Name each file with descriptive names and you can tell which one is running by its name.

AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Re: Tweak Maximize/Minimize hotkey

Post by AHKJeff » 07 Aug 2022, 20:13

boiler wrote:
07 Aug 2022, 19:57
AHKJeff wrote: OK, sorry but this is confusing.
I'm not sure which version of my AHK.ahk file
AHK is running!

How do you locate the AHK.ahk file that
AutoHotkey.exe runs?
Don’t have multiple versions of files named AHK.ahk. Name each file with descriptive names and you can tell which one is running by its name.
Instinctively I would do just that.
However years ago I think I learned that it was only AHK.ahk that is loaded automatically when the program starts, or something like that?
I thought there was something special about the AHK.ahk file?

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Tweak Maximize/Minimize hotkey

Post by boiler » 07 Aug 2022, 22:37

I’m guessing someone once had you put a shortcut to a script you had named AHK.ahk into the Windows Startup folder, so you have associated that with being a special file. You can put a shortcut to any .ahk script file in the Windows Startup folder and have it automatically start when windows starts.

Or perhaps you renamed AutoHotkey.exe to AHK.exe to have the script named AHK.ahk be the script it runs as described here.

In either case, you should break from having a single script that runs and use AHK like most users do, which is to run any file that has a .ahk extension as an AutoHotkey script.

AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Re: Tweak Maximize/Minimize hotkey

Post by AHKJeff » 07 Aug 2022, 23:07

boiler wrote:
07 Aug 2022, 22:37
...
In either case, you should break from having a single script that runs and use AHK like most users do, which is to run any file that has a .ahk extension as an AutoHotkey script.
Cool. Thanks for the tip. I just renamed my AHK.ahk file and put it in the startup folder.
Re-started and it's working. :)

Now I just need to fix up a few of these pesky min/max keys.

AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Re: Tweak Maximize/Minimize hotkey

Post by AHKJeff » 12 Sep 2022, 13:24

Well I'm back again and would like to nail this one down.
There are actually a few scripts that don't work properly.

I will start by posting a couple that do work fine, and then post some that don't
work very well (in the next post).

These load/restore/min the app:

Code: Select all

`::    ;show/minimize Sunbird


If WinExist(wTitle := "ahk_exe sunbird.exe") {
 If WinActive(wTitle)
  WinMinimize
 Else WinActivate
} Else Run, C:\SunbirdPortable\SunbirdPortable.exe
Return

Code: Select all

Browser_Favorites::    ;show/minimize Total Commander (x64) 10.00 - Mark
IfWinNotExist, Total Commander (x64) 10.00 - Mark
{
Run, C:\Program Files\Utils\Totalcmd10\TOTALCMD64.EXE
return
}
else
{
WinGet,CurrentState,MinMax, Total Commander (x64) 10.00 - Mark
if (CurrentState = -1) ; The window is minimized
   WinRestore, Total Commander (x64) 10.00 - Mark
else
   WinMinimize, Total Commander (x64) 10.00 - Mark
}
return
Last edited by AHKJeff on 12 Sep 2022, 18:00, edited 1 time in total.

AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Re: Tweak Maximize/Minimize hotkey

Post by AHKJeff » 12 Sep 2022, 13:29

These don't work very well.

This one launches the app, but does not Min/Restore it:

Code: Select all

Launch_Media::

IfWinNotExist, Ecco - [D:\Computer\Software\Ecco\%file%.eco]
{
Run, C:\Program Files\Utils\Ecco\EccoPro\eccoext.exe
return
}
else
{
winTitle = Ecco - [D:\Computer\Software\Ecco\%file%.eco]
WinGet,CurrentState,MinMax, %winTitle%
if (CurrentState = -1) ; The window is minimized
   WinRestore, %winTitle%
else
   WinMinimize, %winTitle%
}
Return

This one will open Word, but doesn’t Min/Restore it. If I press the hotkey again, it just opens another version of the app!

Code: Select all

Launch_App2::    ;show/minimize Microsoft Word
;IfWinNotExist, Microsoft Word
IfWinNotExist, Word 2013
{
;Run, c:\Program Files\Microsoft Office\Office10\Winword.exe
Run, C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Office 2013\Word 2013.lnk
return
}
else
{
;WinGet,CurrentState,MinMax, Microsoft Word
WinGet,CurrentState,MinMax, Word 2013
if (CurrentState = -1) ; The window is minimized
;   WinRestore, Microsoft Word
WinRestore, Word 2013
else
;   WinMinimize, Microsoft Word
WinMinimize, Word 2013
}
return
Last edited by AHKJeff on 12 Sep 2022, 18:01, edited 1 time in total.

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Tweak Maximize/Minimize hotkey

Post by boiler » 12 Sep 2022, 16:25


AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Re: Tweak Maximize/Minimize hotkey

Post by AHKJeff » 12 Sep 2022, 17:35

boiler wrote:
12 Sep 2022, 16:25
WinRestore is not WinMaximize.
OK thanks you are right.
What I would like to do with my hotkeys is:

1. Launch the app if it's not launched already.
2. Minimize if it is active.
3. Restore if it has been minimized.

Do you see the problems with my code that doesn't work?

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Tweak Maximize/Minimize hotkey

Post by boiler » 12 Sep 2022, 18:32

Are those the complete scripts? You don't have the variable file defined anywhere.

AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Re: Tweak Maximize/Minimize hotkey

Post by AHKJeff » 12 Sep 2022, 19:01

boiler wrote:
12 Sep 2022, 18:32
Are those the complete scripts? You don't have the variable file defined anywhere.
Yes they are the complete scripts.
I got help to create them.
The %file%.eco variable was given to me.
It was set up because the name of the .eco file
that opens (is minimized) changes frequently.

User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: Tweak Maximize/Minimize hotkey

Post by boiler » 12 Sep 2022, 19:14

It sounds like you're expecting that having a variable there causes it to act as a wildcard. That's not how it works. I suppose the person who helped you didn't tell you that you need to either define the value of that variable, and that would only get it to work for that particular value. It's not going to automatically take on whatever characters might show up there in the window title. To work no matter what would appear there, you would need to do something different. The easiest thing to do is just to remove everything from the first % to the end of the line in each place %file% appears. Try this:

Code: Select all

winTitle := "Ecco - [D:\Computer\Software\Ecco\"

Launch_Media::
	if !WinExist(winTitle)
	{
		Run, C:\Program Files\Utils\Ecco\EccoPro\eccoext.exe
		return
	}
	else
	{
		WinGet, CurrentState, MinMax, %winTitle%
		if (CurrentState = -1) ; The window is minimized
		   WinRestore, %winTitle%
		else
		   WinMinimize, %winTitle%
	}
return

AHKJeff
Posts: 91
Joined: 07 Apr 2016, 11:54

Re: Tweak Maximize/Minimize hotkey

Post by AHKJeff » 12 Sep 2022, 19:51

OK thanks for explaining how the variable is supposed to work!
I made the changes with your code.
One thing stood out before I tried to run it.
At the very top you added this code:

Code: Select all

winTitle := "Ecco - [D:\Computer\Software\Ecco\"
I copied it along with the rest of the code you gave me, but
I was confused because I understood that ahk looks for the key that was
pressed (in this case the Launch Media key) and runs the code below that.

But your code started with a line above the Launch Media key.
I was wondering how ahk would know to read that code?

Anyway, when I ran your code it behaved the same as before I made the changes.
It launches the app but doesn't minimize or restore it.

Post Reply

Return to “Ask for Help (v1)”