LoginSignup
36
37

More than 5 years have passed since last update.

Karabinerで多機能ランチャーを実現する

Last updated at Posted at 2015-02-12
  • ものすごい勘違いをしていたので全面的に書き直しました。
  • 以前はAlfred Workflowで無理やり実現していました。

動機

  • Karabinerの__SimultaneousKeyPresses__素晴らしい
    • アルファベットキーの同時押しとかを他のキーコンビネーションにリマップできる
    • いっぱい作れるし語呂合わせとかで覚えやすい
    • ホットキー枯渇問題への強力な回答!
  • これをアプリ起動のショートカットとして利用したい

ドキュメントは読もう(戒め)

全部ここに書いてあるけどいちおう解説します。

概要

Karabinerは基本的にはキーリマップアプリですが、private.xmlでvkopenurldef要素を記述することにより、追加のKeyCodeを定義することができます。

KeyCodeの実行結果としては、以下のものを定義することができます。

  • URLを開く
  • ファイルを開く
  • シェルスクリプトを実行する

つまり、ほぼなんでもできると言ってよいです。これにキーコンビネーションを割り当てることで、複雑な処理をホットキー一発で実行できます。

アプリケーションをトグルするAppleScriptを用意する

アプリケーションランチャとして利用するために、汎用的なランチャースクリプトを用意しておきます。

~/bin/osascript/toggleapp.scpt
on run argv
    set appName to (first item of argv)

    set appID to bundle identifier of (info for (path to application appName))
    tell application "System Events"
        if not (exists process appName) then
            tell application appID to activate
        else
            if frontmost of process appName then
                set visible of process appName to false
            else
                set frontmost of process appName to true
            end if
        end if
    end tell
end run

private.xmlの例

itemの子要素として、vkopenurldef要素と、それに対応するautogen要素を記述します。

private.xml
<?xml version="1.0"?>
<root>
  <item>
    <name>launcher</name>
    <identifier>karabiner_launcher</identifier>
    <vkopenurldef>
      <name>KeyCode::VK_OPEN_URL_SHELL.toggle.Finder</name>
      <url type="shell">
        <![CDATA[ osascript ~/bin/osascript/toggleapp.scpt Finder ]]>
      </url>
    </vkopenurldef>
    <vkopenurldef>
      <name>KeyCode::VK_OPEN_URL_SHELL.toggle.System_Preferences</name>
      <url type="shell">
        <![CDATA[ osascript ~/bin/osascript/toggleapp.scpt "System Preferences" ]]>
      </url>
    </vkopenurldef>
    <vkopenurldef>
      <name>KeyCode::VK_OPEN_URL_SHELL.todotxt</name>
      <url type="shell">
        <![CDATA[ osascript ~/bin/osascript/togglefile.scpt TaskPaper /Users/catfist/Dropbox/todo/todo.txt ]]>
      </url>
    </vkopenurldef>
    <autogen>
    <autogen>
    <!-- finder -->
      __SimultaneousKeyPresses__
      KeyCode::F, KeyCode::D,
      KeyCode::VK_OPEN_URL_SHELL.toggle.Finder,
      Option::SIMULTANEOUSKEYPRESSES_STRICT_KEY_ORDER
    </autogen>
    <autogen>
    <!-- System Preferences -->
      __SimultaneousKeyPresses__
      KeyCode::S, KeyCode::P,
      KeyCode::VK_OPEN_URL_SHELL.toggle.System_Preferences,
      Option::SIMULTANEOUSKEYPRESSES_STRICT_KEY_ORDER
    </autogen>
    <autogen>
  </item>
</root>

この場合、例えば、「sを押しながらpを押す」ことで、システム環境設定をトグルすることができます。

もちろん、AppleScriptの内容を全部private.xmlに書いてもよいです。

謝辞

コメントいただいたrinopoさん、ありがとうございました。

36
37
2

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
36
37