uri-handler.app
macOSでカスタムプロトコルからアプリを開く。ここではGUIのEmacsでファイルを開けるようにしたいが、emacsclientを使うので端末から起動したEmacsでserver-startするのでも問題ない。
emacsclientを起動しておく
urihandlerはプロセスを起動するため、emacsclientを使うのがよい。M-x server-startで待ち受けられる。
uri-handlerをダウンロード
https://github.com/fooqri/uri-handler を使う。GitHubのページからzipをダウンロードするのが簡単。
./setupを実行するとホームディレクトリに ~/.uri_handlersというディレクトリと、~/.uri_handlers/.bash_profileが作られる。 https://github.com/fooqri/uri-handler/blob/master/README.md#add-a-handler を参考に ~/.uri_handlers/emacsというファイルを作る。この例ではemacsclientのpathが直接書かれているので、下記の通りに調べたpathで置き換える。
emacsclientのpathの調べかた
Magitを使っていればwith-editorが入っているので簡単に特定できる。
M-x ielmでEmacs Lispの対話環境を起動して、以下のコードを実行する。
code:ielm
(require 'with-editor)
(with-editor-locate-emacsclient)
筆者はこのようなpathが得られた: /usr/local/Cellar/emacs-plus@27/HEAD-86d8d76/bin/emacsclient
Emacs Plusを使っている場合、このpathは再インストールの度に変わる可能性があるので注意。
ターミナルからcd ~/.uri_handlers/; ./emacs "emacs://open/?file=$PWD/.bash_profile&line=25&column=0"のように実行して通れば成功。
emacs:// プロトコルの有効化
cd ~/.uri_handlers/; ./add_handler emacsで登録できる。
uri-handler.appの初回起動
macOSのアプリケーションはダウンロードした直後はそのままでは起動できないので、open /Applicationsでファインダーでuri-handler.appを探し、右クリックして開く必要がある。この作業は初回だけでいい。
発展的な利用方法
作者のblog http://rwx.io/posts/osx-uri-protocol-handler/ ではWebブラウザからbookmarkletでorg-captureを開始する方法も紹介されている。