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: Key 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

[see AutoHotkey Tutorial]

Use 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/Switch Firefox

; 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()

If you don't know what to do with this, see AutoHotkey Tutorial

Launch/Switch Emacs

For different app, you need to use different code. Here's a emacs example.

; launchOrSwitchEmacs
; http://xahlee.info/mswin/autohotkey_switch_launch_app.html
; version 2021-02-21 2022-01-19
launchOrSwitchEmacs()
{
if WinExist("ahk_class Emacs")
{
  WinActivate, ahk_class Emacs
}
Else
{
  Run "%HOMEPATH%\Downloads\emacs-27.1-x86_64\bin\runemacs"
}
Return
}

+F6::launchOrSwitchEmacs()

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

AutoHotkey Tutorial

AutoHotkey logo nz9fX