Toggle between two applications on MacOS

2024-10-25

placeholder
MacOS
Karabiner Elements

My development workflow often requires switching between my terminal where I use my code editor and my web browser were I look at the results or the docs. While you could use alt-tab, if I open any other application, i.e discord, figma - it interrupts my workflow as I need to alt-tab more than once to get back to my terminal / browser.

Find the App Bundle Identifiers

osascript -e 'id of app "kitty"' # for Terminal
osascript -e 'id of app "Arc"' # replace with your web browser name

The first step is to figure out the app bundle identifiers for the two applications. This is essentially the full name of your application, which can be used to tell the system which application needs to be targeted and opened.

Here are my results

terminal -> net.kovidgoyal.kitty
browser -> company.thebrowser.Browser

Install Karabiner Elements

I use karabiner to customise my keyboard. I love it for the flexible custom rules that I can implement. I’ll show you how !

You can get Karabiner Elements from their website

Add a Complex Modification

  1. Create your own rule by going to Complex Modification → Add your own rule
  2. Paste this script into the dialog
{
    "description": "Toggle between Kitty terminal and Browser using Right shift",
    "manipulators": [
        {
            "conditions": [
                {
                    "bundle_identifiers": [
                        "^net\\.kovidgoyal\\.kitty$"
                    ],
                    "type": "frontmost_application_unless"
                }
            ],
            "from": {
                "key_code": "right_shift",
                "modifiers": { "optional": ["any"] }
            },
            "to": [{ "shell_command": "open -a 'kitty'" }],
            "type": "basic"
        },
        {
            "conditions": [
                {
                    "bundle_identifiers": [
                        "^net\\.kovidgoyal\\.kitty$"
                    ],
                    "type": "frontmost_application_if"
                }
            ],
            "from": {
                "key_code": "right_shift",
                "modifiers": { "optional": ["any"] }
            },
            "to": [{ "shell_command": "open -b 'company.thebrowser.Browser'" }],
            "type": "basic"
        }
    ]
}

In this script, I use my right shift to toggle between kitty and Arc. I opted for this over a function key, since it’s an unused key in a easily reachable location. If you want to remap it, modify the value for "key_code" in both objects within "manipulators"

Test it out

Try pressing right shift and your applications should toggle between each other !