AutoHotkey: Switch or Launch App

By Xah Lee. Date: . Last updated: .

Use Windows Builtin Launch/Switch Mechanism

This is the fastest and most reliable method.

Pin the apps you want to the taskbar, then, ❖ Window+1 will launch or switch the left most one. ❖ Window+2 for the second one, etc. For detail, see Windows: Keyboard Shortcuts to Launch or Switch App

Once done, you can use AutoHotkey to setup easy keys. Especially, ❖ Window+1 is hard to press because it involves pinky.

; make F1 send Win+1
$F1::Send #1

; make F2 send Win+2
$F2::Send #2

Pure AutoHotkey to Launch/Switch App

This is pure AutoHotkey solution. Suppose you want a hotkey that launches a browser, but if it is already running, just switch to it.

; launch or switch to Firefox
; http://xahlee.info/mswin/autohotkey_switch_launch_app.html
; Version 2021-02-21 2022-01-19
launchOrSwitchFirefox()
{
if WinExist("ahk_class MozillaWindowClass")
{
WinActivateBottom, ahk_class MozillaWindowClass
}
Else
{
 Run "C:\Program Files\Mozilla Firefox\firefox.exe"
}
Return
}

; shift+f8
$+F8::launchOrSwitchFirefox()

How to find window name or app name?

To find out what is the window's ahk_class , right click on the AHK icon and chose Window Spy , then click on a window you want.

win 10 AutoHotkey window spy 2021-02-14 QftPt
AutoHotkey Window Spy 2021-02-14
win 10 AutoHotkey window spy 2021-02-14
AutoHotkey Window Spy window