Karabiner-Elements cannot open new Terminal window unless Terminal already in focus

karabinerkeybindingsSecurityterminal

I am on Catalina 10.15.7 and use Karabiner-Elements 13.3.

When I use the rule shown below for opening Terminal nothing happens unless Terminal is in focus (you can see Terminal on the menu bar).

Seems like something to do with security permissions. When I run the script itself from within iTerm (not Terminal) I got a pop up asking for allowing "access to control". Once accepted the shortcut worked when either Terminal or iTerm was in focus.

Obviously I want the shortcut to work regardless of what is in focus. How to achieve that?

{
  "title": "Open Terminal",
  "rules": [
    {
      "description": "Open Terminal",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "modifiers": {
              "mandatory": [
                "left_control",
                "left_alt"
              ]
            },
            "key_code": "t"
          },
          "to": [
            {
              "shell_command": "osascript -e 'tell app \"Terminal\"' -e 'do script \"tmux\"' -e 'end tell'"
            }
          ]
        }
      ]
    }
  ]
}

Best Answer

I played with your key def for Karabiner. (Note that I'm on Big Sur, Karabiner 13.3). Here's what worked for me:

After the first time that I installed and activated your shortcut and tried to use it a dialog came up asking to allow some accessibility or privacy permission. I accepted it without noting exactly what it said but looking through the Privacy permissions in System Preferences I saw that a permission for Automation to allow "karabiner_console_user_server" to control "Terminal" had been added and its checkbox was checked. At first I didn't think the shortcut had done anything but then I found that Terminal had been opened and the tmux script had been run. (I don't have the tmux script so it failed for me but was displayed in Terminal as a command.) Terminal was the bottom most application (shown last in the application chooser). So when testing the shortcut be sure that Terminal isn't hiding behind everything.

At one point I found that if Terminal was running but hidden behind other apps, using the shortcut did work but left it hidden. When it was visible the shortcut did appear to work as desired.

In continued testing I checked and unchecked the Automation preference checkbox at various times and when it was checked the shortcut did attempt to run the tmux script. When it was not checked Terminal did open (behind other apps) but the script appeared not to run. I was not asked to accept any privacy permissions after the first time it had asked after installation (probably because Apple prefers confusing someone by not displaying repeated permission requests rather than annoying someone with multiple requests and confusion tends to result in fewer complaints than annoyances).

So be sure that the Automation privacy permission in System Preferences that I mentioned above exists and that it is checked. It's probably a good idea to quit and restart Karabiner-Elements as well; I did that at various times just in case. If the permission isn't shown (checked or unchecked) then something else isn't working. Adding permissions for an application to automate another something that, for some reason, can't be added manually and is only asked for when one actually does something that requires the permission (and possibly only once), don't ask me why. (Note that I didn't ever shut down and restart to see if I'd be asked to allow the permission again though I think it wouldn't. I'm not sure.)

I hope getting the shortcut to work is as simple as this but I imagine there are plenty of other possibilities that might be the issue.