Can I create a shortcut to open a specific application on OS X?

I want to create a keyboard shortcut to i.e. open Firefox while I am working in another application. I know I can use ⌘ CMD+T when I am in Firefox to open a new tab.

I haven't found a way to create that kind of shortcuts in System Preferences.

Any ideas?


Solution 1:

Karabiner-Elements (10.12 and later)

Karabiner stopped working in 10.12 due to changes in the kernel architecture, and Karabiner was rewritten as Karabiner-Elements which uses a new JSON format for the configuration files.

You can now add a rule like this to karabiner.json (https://pqrs.org/osx/karabiner/json.html):

{
  "from": {
    "key_code": "t",
    "modifiers": {
      "mandatory": [
        "right_option"
      ]
    }
  },
  "to": [
    {
      "shell_command": "open -aterminal"
    }
  ],
  "type": "basic"
}

This makes a short press of the right command key open Emacs:

{
  "from": {
    "key_code": "right_command",
    "modifiers": {
      "optional": [
        "any"
      ]
    }
  },
  "to": [
    {
      "key_code": "right_command",
      "lazy": true
    }
  ],
  "to_if_alone": [
    {
      "shell_command": "open -aemacs"
    }
  ],
  "type": "basic"
}

Karabiner (10.11 and earlier)

Save a file like this as private.xml:

<?xml version="1.0"?>
<root>
  <vkopenurldef>
    <name>KeyCode::VK_OPEN_URL_Safari</name>
    <url>file:///Applications/Safari.app</url>
  </vkopenurldef>
  <vkopenurldef>
    <name>KeyCode::VK_OPEN_URL_iTunes</name>
    <url>file:///Applications/iTunes.app</url>
  </vkopenurldef>
  <vkopenurldef>
    <name>KeyCode::VK_OPEN_URL_EMACS</name>
    <url>file:///Applications/Emacs.app</url>
  </vkopenurldef>
  <item>
    <name>custom</name>
    <identifier>custom</identifier>
    <autogen>__KeyToKey__ KeyCode::F11, KeyCode::VK_OPEN_URL_Safari</autogen>

    <!-- change the right option key into an extra modifier key -->
    <autogen>__KeyToKey__ KeyCode::OPTION_R, KeyCode::VK_MODIFIER_EXTRA1</autogen>

    <autogen>__KeyToKey__ KeyCode::I, ModifierFlag::EXTRA1, KeyCode::VK_OPEN_URL_iTunes</autogen>

    <!-- make a short press of the right command open Emacs -->
    <autogen>__KeyOverlaidModifier__ KeyCode::COMMAND_R, KeyCode::COMMAND_R, KeyCode::VK_OPEN_URL_EMACS</autogen>
  </item>
</root>

Alfred 2

If you have bought the Powerpack for Alfred 2, you can create a workflow like this:

Hotkeys have a short delay by default, but you can reduce the delay by changing the "Trigger behaviour" option from "Simulate modifier keys released" to "Pass through modifier keys (Fastest)":

Automator services

Open Automator, select "Service" as the type of the document, change "Service receives" from "text" to "no input", add a "Launch Application" action, and save the service. Then assign a keyboard shortcut for the service from "System Preferences > Keyboard > Shortcuts > Services".

I don't recommend this option, because at least on my system, there is at best a delay of around half a second before an application is focused by a keyboard shortcut created using this method, and the delay is often even longer. There has also been a bug since 10.7 where the keyboard shortcuts for Automator services don't always work until you hover over the services menu from the menu bar. (Edit: it's now 8 years after I wrote this answer and the bug still hasn't been fixed.) Also the keyboard shortcuts for services are not available in some applications like Emacs.

Solution 2:

I found Apptivate the easiest to use. You just select an application and you assign a global shortcut. That's it.

Solution 3:

Alfred

Alfred, in its Power Pack version, can map shortcuts to applications, among many other features.

Solution 4:

If you do not want to use third party apps.
Then you can use Automator (Services ), and the Built in keyboard shortcuts.

Here is how in an answer to a similar question.

Very simple.

In Step 3, of creating the service.
Set it to 'any application' .

i.e,
set Service receives to: 'no input' in 'any application' (Do this by using the drop downs at the top.)