Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Key to toggle minimize/restore



  • Please log in to reply
16 replies to this topic
AHKJeff
  • Members
  • 94 posts
  • Last active: May 01 2016 05:13 PM
  • Joined: 09 Feb 2014

I've been struggling with this for quite a few hours now and could certainly use some help. :)

 

I would like to use a key as a toggle to do the following:
First check to see if a program is running, if not launch it. (PID = 256)

If the program was running, but minimized, restore it to its previous window size.
When the key is toggled again; minimize the program.

So basically the key would toggle the app from minimized state to restored state.
And in addition, if the app was not running to begin with, the key would also launch the app (to put it in its restored state).

 

TIA



bobbysoon
  • Members
  • 81 posts
  • Last active: May 03 2014 09:50 PM
  • Joined: 15 Mar 2011
#space::	appFlapper("C:\Windows\System32\notepad.exe")

appFlapper(exe) {
	IfWinExist, ahk_exe %exe%
		IfWinActive
			WinMinimize
		else
			WinActivate
	else
		Run, %exe%
}



AHKJeff
  • Members
  • 94 posts
  • Last active: May 01 2016 05:13 PM
  • Joined: 09 Feb 2014

Thanks Bobby.

 

OK, so I tried to apply your code to my situation.

Here's what I came up with:

 

#b::    appwLite("C:\Program Files\Utils\webcamXP 5\wLite.exe")

appwLite(exe) {
    IfWinExist, ahk_exe %exe%
        IfWinActive
            WinMinimize
        else
            WinActivate
    else
        Run, %exe%
              }
return

 

I probably didn't implement it correctly because this is what happens.

I press the #b keys and the app opens.

I press #b again and nothing happens.

I see the cpu working, but nothing else.

 

Here is the script report:

 

381: appWLite("C:\Program Files\Utils\webcamXP 5\wLite.exe")  
384: IfWinExist,ahk_exe %exe%
390: Run,%exe%
391: }
381: Return (1.80)
381: appWLite("C:\Program Files\Utils\webcamXP 5\wLite.exe")  
384: IfWinExist,ahk_exe %exe%
390: Run,%exe% (0.03)
391: }
381: Return (685.23)



RHCP
  • Members
  • 1228 posts
  • Last active: Apr 08 2017 06:17 PM
  • Joined: 29 May 2006
#space::    appFlapper("C:\Windows\System32\notepad.exe")

appFlapper(exePath) {
    IfWinExist, % "ahk_exe " substr(exePath, instr(exePath, "\",, 0)+1)
        IfWinActive
            WinMinimize
        else
            WinActivate
    else
        Run, %exePath%
}
  

The program you were running probably only allows one copy of itself to be running at a time. 

The code had a small bug, the IfWinExist AH_EXE should only use the exe file name and not it's path i.e. notepad.exe, rather than C:\Windows\System32\notepad.exe.

Hence on each run it simply tried to run the program again which wasn't allowed.



Oldman
  • Members
  • 2475 posts
  • Last active: Feb 18 2015 04:57 PM
  • Joined: 01 Dec 2013

When the program is running, can you see wLite.exe in the task manager ?

Because, the script does not see it.

 

I don't think like RHCP, because the doc says :

 

ahk_exe considers all processes with name or full path matching a given string

 

The process command has that limitation.

The example from bobbysoon works perfectly.


Si ton labeur est dur et que tes résultats sont minces, souviens toi du grand chêne qui avant n'était qu'un gland....comme toi ! (anonyme) ;)

L'art de lire, c'est l'art de penser avec un peu d'aide. (É. Faguet)

Windows 3.1. Collector's Edition.     (www.avaaz.org)


RHCP
  • Members
  • 1228 posts
  • Last active: Apr 08 2017 06:17 PM
  • Joined: 29 May 2006

 

The process command has that limitation.

The example from bobbysoon works perfectly.

 

AHK_EXE/IfWinExist should accept it, but it doesn't seem to work consistently. Try bobbysoon's example. Each time you press space a  new instance of notepad is opened, as IfWinExist fails to find the window.

 

Im curious why it doesn't work for me, I noticed this once before and assumed it happened to everyone. Interesting though.



GEV
  • Members
  • 1364 posts
  • Last active:
  • Joined: 23 Oct 2013

Here's another approach:

	 #space:: 
Process, Exist, notepad.exe
If !ErrorLevel ; is not running
{
 Run, notepad.exe
  return
}
 pid := ErrorLevel
IfWinNotActive, % "ahk_pid " pid
 WinActivate, % "ahk_pid " pid
else
 WinMinimize, % "ahk_pid " pid
  return


Oldman
  • Members
  • 2475 posts
  • Last active: Feb 18 2015 04:57 PM
  • Joined: 01 Dec 2013

I have no Win key on this keyboard, it is like me, it's an oldkeyboard.

Then, I do the test with the Ctrl key and it works fine, no new instance of notepad.

 

Is there a problem with the Win key ?

 

If you choose a modifier key or another that should not make any difference.

 

AHKJeff uses the #b and not the #space.


Si ton labeur est dur et que tes résultats sont minces, souviens toi du grand chêne qui avant n'était qu'un gland....comme toi ! (anonyme) ;)

L'art de lire, c'est l'art de penser avec un peu d'aide. (É. Faguet)

Windows 3.1. Collector's Edition.     (www.avaaz.org)


RHCP
  • Members
  • 1228 posts
  • Last active: Apr 08 2017 06:17 PM
  • Joined: 29 May 2006

Using ahk_pid has always worked fine for me, and Gev's example works perfectly.

 

Using F1 still didn't work. 

 

Found part of the problem. When using the full path C:\Windows\System32\notepad.exe, wintitle doesn't find the window, however when using C:\Windows\notepad.exe it does!

 

I guess it's something to do with this.

 

Otherwise, ahk_exe accepts a case-insensitive name or full path; for example, ahk_exe notepad.exe covers ahk_exe C:\Windows\Notepad.exe, ahk_exe C:\Windows\System32\Notepad.exe and other variations.

 

If i run C:\Windows\System32\notepad.exe, the exe location/directory of the process is C:\Windows\SysWOW64 and it is a 32 bit process. However if C:\Windows\notepad.exe is run, the Location is C:\Windows and it's a 64bit process.

 

So i guess using C:\Windows\System32\notepad.exe causes winExist to fail, as windows redirects/launches the notepad from another (SysWOW64) 32 bit folder, and while using the explicit ahk_exe this variation isn't found. 

 

I guess I can start using full path directories in ahk_exe now, providing they're not 32 bit system exes. Thanks, I learnt something new today.



AHKJeff
  • Members
  • 94 posts
  • Last active: May 01 2016 05:13 PM
  • Joined: 09 Feb 2014

 

Here's another approach:

	 #space:: 
Process, Exist, notepad.exe
If !ErrorLevel ; is not running
{
 Run, notepad.exe
  return
}
 pid := ErrorLevel
IfWinNotActive, % "ahk_pid " pid
 WinActivate, % "ahk_pid " pid
else
 WinMinimize, % "ahk_pid " pid
  return

 

Thanks GEV.

I tried out your method.

At first, with Process, Exist, wLite.exe, nothing happened.

Then I added the full path and the program launched.

But when I hit the hotkey again, nothing happened. The program didn't minimize.

Here's what was returned:

219: Process,Exist,C:\Program Files\Utils\webcamXP 5\wLite.exe
220: if !ErrorLevel  
222: Run,C:\Program Files\Utils\webcamXP 5\wLite.exe (0.03)
223: Return (3.84)
219: Process,Exist,C:\Program Files\Utils\webcamXP 5\wLite.exe
220: if !ErrorLevel  
222: Run,C:\Program Files\Utils\webcamXP 5\wLite.exe (0.06)
223: Return (4.94)
219: Process,Exist,C:\Program Files\Utils\webcamXP 5\wLite.exe
220: if !ErrorLevel  
222: Run,C:\Program Files\Utils\webcamXP 5\wLite.exe (0.05)
223: Return (12.70)



AHKJeff
  • Members
  • 94 posts
  • Last active: May 01 2016 05:13 PM
  • Joined: 09 Feb 2014

In the meantime I was doing some more experimenting.

I could launch the program but then kept having problems when I minimized it.

It would immediately restore itself on its own.

This was using some other code I was trying out.

 

Then it dawned on me how the program was behaving when I launched it and 'minimized' it.

I was using a hotkey to launch it and then always was using Alt-F4 to 'minimize' it.

I thought I was minimizing it because I could see its icon in the task bar.

 

Then I realized that I was actually closing the program.

When I realized this it then became clear that I simply had to have code to open the program and code to close it.

 

This is what I came up with. I know it could probably still be pared down, but it works.

BTW, what does Current State = 0 mean?

 

Launch_App1::    ;  toggle Webcam

SetTitleMatchMode, 2   
IfWinExist, webcamXP
{
    WinActivate  ; Automatically uses the window found above.
    WinGet, CurrentState, MinMax   
         
     If (CurrentState = 0)
     {
       send, !{F4}
       return
     }   

}

else

{
Run, C:\Program Files\Utils\webcamXP 5\wLite.exe
return

}
return

 



GEV
  • Members
  • 1364 posts
  • Last active:
  • Joined: 23 Oct 2013

Not bad!
Novices have sometimes  better ideas than pros when they are diligent.

Launch_App1::    ;  toggle Webcam

SetTitleMatchMode, 2   

IfWinNotExist, webcamXP
 {
   Run, C:\Program Files\Utils\webcamXP 5\wLite.exe
	 return
 }
else
{
  WinGet, CurrentState,, webcamXP
    If CurrentState = -1
	WinRestore webcamXP
    else
	{
	 IfWinActive, webcamXP
	    WinMinimize webcamXP
	 else
	    WinActivate, webcamXP
	}
 }
 return

Shorter:

IfWinNotExist, webcamXP
{
 Run, C:\Program Files\Utils\webcamXP 5\wLite.exe
        return
}
else
{
 IfWinNotActive, webcamXP
       WinActivate, webcamXP
 else
       WinMinimize, webcamXP
}
 return

Keep it up!



AHKJeff
  • Members
  • 94 posts
  • Last active: May 01 2016 05:13 PM
  • Joined: 09 Feb 2014

When the program is running, can you see wLite.exe in the task manager ?

Because, the script does not see it.

 

I'm not sure about before with the code above, but now, when the program is running I can see it in Task Manager/programs.

When I close the program with my new code, I no longer see the app in Task Manager/programs, but I do see it in Task Manager/processes.

This would explain why I see the icon in the task bar, even when the program is closed.



AHKJeff
  • Members
  • 94 posts
  • Last active: May 01 2016 05:13 PM
  • Joined: 09 Feb 2014
✓  Best Answer

Not bad!
Novices have sometimes  better ideas than pros when they are diligent.

Keep it up!

 

Thanks GEV for your encouragement. :)

I will replace my code with one of your suggestions.

Thanks again for your help!



AHKJeff
  • Members
  • 94 posts
  • Last active: May 01 2016 05:13 PM
  • Joined: 09 Feb 2014
Just to follow-up on your suggestions GEV.
I tried both of them with the following same results for each one:
 
When I hit the key the first time it launches the app.
Second time it minimizes window.
Third time nothing happens.
If I click on another program to change focus, and then hit the key, the app maximizes.
Hit key again it minimizes and the above cycle will continue.

 

Another key point here that I discovered yesterday when I found my CPU meter unusually high, is that when this app (USB camera) is running, it keeps my CPU load at 50% or higher. This occurs even if it is minimized.

 

So I am going to stick with my clumsy code that I threw together yesterday.

What my code did basically was replicate with one hotkey, what I had been doing before with two different combinations:

I would hit the hotkey to launch the app, and then hit Alt-F4 to close the app. The app launches super-fast because the process continues to run.

 

So this way (opening and closing with the same hotkey) my CPU load stays much lower and also I can quickly change between camera and no camera, with a quick toggle. I don't have to change focus first.  :)

 

Had I known from the beginning that I was actually closing the program each time, before 'maximizing' it, I would have worded my initial question differently. Sorry for the confusion!

 

One other thing I should add.

 

I just read that with State = 0 it means that the window is neither min. nor max.

Which is the situation with my app. The window is in the middle of the screen, not max nor min.

So this is why I ended up choosing State = 0 for my code. At the time it was simply trial by error.  :)

 

WinGet, CurrentState, MinMax 
         
     If (CurrentState = 0)

    ......