Switching apps slows down my productivity (and how I fixed it)
Tags: productivity, autohotkey, macos
Published: 2022-04-04 04:58:17

Switching apps is something I do hundreds of times a day on both MacOS and Windows. I found a way to make this faster, which has made my workflow a lot more efficient.

![Image: alt-tab-tab-tab-tab...](https://i.imgur.com/hsfyFU8.png)

<figcaption>☝ Has this been as frustrating for me as anyone else?</figcaption>

---

![Section 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/998qgqm1q2llkv661l0h.png)

## Is switching apps really difficult?
Well, just a bit. Not much, but small frustrations easily add up over time. Switching with a keyboard always required too much effort for me. I often try one of these, but each one requires some thinking (or typing):

- `win-4` to launch the 4th taskbar app *(Windows)*
- `ctrl-alt-4` using uBar to emulate Windows behaviour *(MacOS)*
- `alt-tab-tab-tab` to switch to a window *(Windows and MacOS)*
- `cmd-space firefox` to use Spotlight *(MacOS)*

---

![Section 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njny1r5hen7rbppx9hso.png)

## My solution: fixed key bindings
I settled a way that’s easier for me: shortcuts that always do one—and only one—thing. For example, `ctrl-win-w` will always focus my web browser, where `alt-tab` requires some thinking to figure out how many Tabs to press.

The result: <mark>I save a second of thinking for every app switch I do.</mark> For something I do hundreds of times a day, it all adds up! 🎉

![Image: ctrl-win-W for Web, ctrl-win-V for Visual Studio Code, and more](https://i.imgur.com/xCUMMtB.png)

<figcaption>☝ Just a small selection of the shortcuts I use.</figcaption>

---

![Section 3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jp9p27qxbz9lw3nafoa0.png)

## MacOS via Raycast
There are many ways this can be achieved in MacOS. The simplest one I think is with [Raycast](https://www.raycast.com/).

![Raycast shortcuts screen](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vjl451sq4aqswe7gtfg.png)

<figcaption>☝ Raycast allows defining shortcuts for many actions, including launching apps.</figcaption>

---

![Section 4](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4uv5lk77615zhfwxeay1.png)

## MacOS via Hammerspoon
For my MacBook, I used [Hammerspoon](https://www.hammerspoon.org/), a hotkey automation suite for MacOS. Writing a Lua script to launch apps with hotkeys isn’t too difficult:

```lua
-- ~/.hammerspoon/init.lua
hs.hotkey.bind({"ctrl", "cmd"}, "v", "Visual Studio Code", function()
  hs.application.launchOrFocus("Visual Studio Code")
end)

hs.hotkey.bind({"ctrl", "cmd"}, "w", "Firefox", function()
  hs.application.launchOrFocus("Firefox")
end)

hs.hotkey.bind({"ctrl", "cmd"}, "s", "Slack", function()
  hs.application.launchOrFocus("Slack")
end)
```

<figcaption>☝A simple Hammerspoon script to launch apps from hotkeys. (Docs: <a href="https://www.hammerspoon.org/docs/hs.application.html#launchOrFocus">launchOrFocus</a>, <a href="https://www.hammerspoon.org/docs/hs.hotkey.html#bind">bind</a>)</figcaption>

---

![Section 5](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v73gusel2p8hj9yrj9u3.png)

## Windows via AutoHotkey
For my Windows laptop, I used a powerful automation scripting language called [AutoHotkey](https://www.autohotkey.com/). Here’s an example that launches apps based on start menu items (`w` and `o`), URL (`c`) and file path (`v`).

```autohotkey
UserStartMenu := A_AppData . "\Microsoft\Windows\Start Menu\"
SysStartMenu := "C:\ProgramData\Microsoft\Windows\Start Menu\Programs"

; [w] Firefox (web)
^#w::LaunchOrFocus({ run: SysStartMenu . "Firefox", select: "ahk_exe firefox.exe" })

; [o] Obsidian
^#o::LaunchOrFocus({ run: UserStartMenu . "Programs\Obsidian", select: "ahk_exe Obsidian.exe" })

; [c] Calendar
^#c::LaunchOrFocus({ run: "outlookcal:" })

; [t] Terminal
^#v::LaunchOrFocus({ run: "wt.exe", select: "ahk_exe WindowsTerminal.exe" })

LaunchOrFocus(app) {
  selector := app["select"]
  If (selector and WinExist(selector))
    WinActivate,
  else
    Run % app["run"]
}
```

<figcaption>☝A simple AutoHotkey script to launch apps from hotkeys using <code>WinActivate</code> and <code>Run</code>. Save it as <em>example.ahk</em> and double-click on it to open in AutoHotkey.</figcaption>

---

![Section 6](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9waw39ra4mfg5lu7xvci.png)

## My shortcuts
I set up shortcuts for the apps I use 90% of the time. For anything not here, I would use Spotlight or Windows Search.

![Image: Shortcuts diagram](https://i.imgur.com/0pi5j0i.png)

<figcaption>☝I use the Colemak mod-DH layout, but this can work on any keyboard layout. (The <code>*</code> keys are reserved for moving windows to the side of my screen, but that’s for another article!)</figcaption>

---

![Section 7](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2ibk6i7lxfbo0cg4rad.png)

## Thanks for reading!

Thanks for checking out my post! I'm a web developer who loves open source and supercharging productivity. If you liked this post, consider [following me on Dev.to (@rstacruz)](https://dev.to/rstacruz) or [Twitter (@rstacruz)](https://twitter.com/rstacruz)!

- [Autohotkey](https://www.autohotkey.com/) - automation for Windows
- [Raycast](https://www.raycast.com/) - app launcher for MacOS
- [Hammerspoon](https://www.hammerspoon.org/) - automation for MacOS