LoginSignup
6
8

More than 3 years have passed since last update.

karabiner-elements でアプリケーションを切り替えるホットキーを実現する

Last updated at Posted at 2019-10-10

macではcmd + tabでアプリを切り替える人が多いと思うが、特定のアプリを開きたい時にはホットキーを設定しておくと便利。
cmd + tabでは「アプリの並びをみる」「tabを複数回押して開きたいアプリ」を選ぶという2段階の作業が必要になる。
しかも、アプリを開いている順番によって何回tabを押せばいいかが変わってくる。結構めんどい。

Screen Shot 2019-10-10 at 13.28.13.png

ホットキーを設定しておくと、別のアプリに切り替えたいときに特定のホットキーを押すだけでそのアプリに切り替えることができる。
現在の状態に依存しないので、早く切り替えられるし脳の負荷も減る。

Karabiner-Elementsでhotkeyを設定するにはcomplex modificationsを定義する。

以下のようなjsonファイルを作り ~/.config/karabiner/assets/complex_modifications 以下に配置する。
私の例では、

  • ctrl + 3 : Slack
  • ctrl + 4 : Dictionary
  • ctrl + 5 : MacVim
  • ctrl + 6 : iTerm
  • ctrl + 7 : Preview
  • ctrl + 8 : Mail
  • ctrl + 9 : Google Chrome
  • ctrl + 0 : Inkdrop

というように割り当てている。

hotkeys_switch_app.json
{
  "title": "hotkeys to switch app",
  "rules":
  [
    {
      "description": "ctrl-3 to Slack",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "3",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "shell_command": "open -a 'Slack'"
            }
          ]
        }
      ]
    },
    {
      "description": "ctrl-4 to Dictionary",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "4",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "shell_command": "open -a 'Dictionary'"
            }
          ]
        }
      ]
    },
    {
      "description": "ctrl-5 to MacVim",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "5",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "shell_command": "open -a 'MacVim'"
            }
          ]
        }
      ]
    },
    {
      "description": "ctrl-6 to iTerm2",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "6",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "shell_command": "open -a 'iTerm'"
            }
          ]
        }
      ]
    },
    {
      "description": "ctrl-7 to Preview",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "7",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "shell_command": "open -a 'Preview'"
            }
          ]
        }
      ]
    },
    {
      "description": "ctrl-8 to mail",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "8",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "shell_command": "open -a Mail"
            }
          ]
        }
      ]
    },
    {
      "description": "ctrl-9 to chrome",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "9",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "shell_command": "open -a 'Google Chrome'"
            }
          ]
        }
      ]
    },
    {
      "description": "ctrl-0 to inkrop",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "0",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "shell_command": "open -a 'Inkdrop'"
            }
          ]
        }
      ]
    }
  ]
}

このようなjsonファイルを配置した後で、Karabiner-Elementsのcomplex modificationsで設定を有効にすればOK。

image.png

以後、どのアプリにいても"ctrl+数字"のホットキーを押せばアプリを切り替えられる。

(追記)ホットキーを押した時に表に表示するアプリを複数の候補から選ぶ

Ctrl-5を押した時に、

  • MacVimが起動していたらMacVimを表示
  • Clionが起動していたらClionを表示
  • RubyMineが起動していたらRubymineを表示

というように挙動を切り替える。psコマンドでプロセス一覧を取ってきて判定している。

   {
      "description": "ctrl-5 to MacVim or clion or rubymine",
      "manipulators": [
        {
          "type": "basic",
          "from": {
            "key_code": "5",
            "modifiers": {
              "mandatory": [
                "control"
              ]
            }
          },
          "to": [
            {
              "shell_command": "ps ux | grep -o -e '[M]acVim' -e '[c]lion' -e '[r]ubymine' | head -1 | xargs open -a"
            }
          ]
        }
      ]
    },

参考

6
8
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8