timond
timond

Reputation: 53

How to start a program if not already started, put on focus if already started?

I would like to make a ahk script to start apps if they are not currently started and maximize them if they are. Is it possible using AHK ?

CapsLock & w::

    Run firefox.exe

Return

Something like this but make it so that if I press CapsLock & w and then minimize firefox, pressing CapsLock & w would bring it back maximized / in focus. Any ideas? Thanks!

Upvotes: 5

Views: 1736

Answers (2)

Spyre
Spyre

Reputation: 2344

The code that you are looking for is somewhat similar to the example they give in the docs for WinActivate

So modifying that example for your purpose, and adding conditionals would give you:

CapsLock & w::

if WinExist("ahk_exe firefox.exe")
    if WinActive("ahk_exe firefox.exe")
     WinMinimize
    else
     WinActivate
else
    Run firefox.exe
Return


Take note that this script will currently only minimize a Firefox window if it is the currently active window. If you need it to minimize a Firefox in the background, the script would potentially be a bit more complex since you could possibly have multiple Firefox windows open, and you would need to provide conditions and logic to handle cases like those. However, if you need this functionality, describe what behavior you would like to occur if this condition occurs, and I can work on it.

Upvotes: 1

0x464e
0x464e

Reputation: 6499

Sure, this is very easy and doable with AHK.
Here's a very easy and straight forward example

CapsLock & w::
    if (WinExist("ahk_exe notepad.exe"))
    {
        WinActivate, ahk_exe notepad.exe
        WinMaximize, ahk_exe notepad.exe
    }
    else
        Run, notepad.exe
return

ahk_exe (docs) is used to refer to windows by their process. It's very convenient.

Upvotes: 3

Related Questions