My solution is to use the following function. You need to specify the title of the app (usually in forms of ahk_class) is the best option, and also the name / part of the name of the shortcut of the app in your start menu. You can supply multiple wildcards to make a better match with the :
i.e :
"Auto:Help" to open Autohotkey help file instead of anything else with the word "auto" in it.
Use like:
^!1::WinMinLaunch("ahk_class Vim", "Vim72.lnk")
What this will do when run Vim72.lnk if the window is not found, restore if minimized, and minimize if the active screen.
Hope this helps.
Code:
WinMinLaunch(WindowTitle, AppTitle)
{
SetTitleMatchMode,2
AutoTrim, Off
DetectHiddenWindows, Off
IfWinActive, %WindowTitle%
{
WinMinimize, %WindowTitle%
}
Else
{
IfWinExist, %WindowTitle%
{
WinGet, winid, ID, %WindowTitle%
DllCall("SwitchToThisWindow", "UInt", winid, "UInt", 1)
}
Else
{
if InStr(AppTitle, ":")
NewApp := AppTitle
else
NewApp := FindAppPath(AppTitle)
Sleep, 10
Run % NewApp
}
}
}
; Find application path from start menus, use | to parse description for more detailed search
FindAppPath(AppTitle)
{
scanPath = %A_StartMenuCommon%|%A_StartMenu%
Count = 0
Loop, parse, scanPath,|
{
Loop, %A_LoopField%\*, %ScanMode%, 1
{
FileGetAttrib, fileAttrib, %A_LoopFileFullPath%
IfNotInString, fileAttrib, H ; EXCLUDE HIDDEN FILE
{
Count += 1
Listing%Count% := A_LoopFileFullPath
}
}
}
Loop %Count%
{
element := Listing%A_Index%
try = 0
success = 0
Loop, parse, AppTitle, |
{
try +=1
IfInString, element, %A_LoopField%
success +=1
}
If (try = success)
Return %element%
}
}