LoginSignup
30
27

More than 3 years have passed since last update.

WindowsでカスタムURLスキーマを設定し、引数付きで起動できるようにする

Last updated at Posted at 2019-08-24

目的

ブラウザからカスタムURLを叩いてwindows端末にインストールされているローカルアプリを引数付きで起動したい。

本稿では以下の例の実装方式を記載する。
例:ブラウザにnote:sample.txtをを入力するとメモ帳アプリが起動しsample.txtが開くようにする

方法

以下のtest.regを作成し、regeditの「ファイル」→「インポート」からインポート

test.reg(ファイル名は自由、regeditには中に記載された「note」という名前で設定が保存される)
Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\note]
@="url:note protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\note\shell]

[HKEY_CLASSES_ROOT\note\shell\open]

[HKEY_CLASSES_ROOT\note\shell\open\command]
@="C:¥¥Windows¥¥System32¥¥cmd.exe /k set myvar=%1&call set myvar=%%myvar:note:=%%&call start notepad.exe %%myvar%%"

説明

ブラウザでnote:と打ち込むと[HKEY_CLASSES_ROOT\note\shell\open\command]の欄に設定された命令が実行される。
なので単純にメモ帳を起動するだけなら

test2.reg
・・・
[HKEY_CLASSES_ROOT\note\shell\open\command]
@="C:¥¥Windows¥¥System32¥¥notepad.exe"

とかけば良い

プロトコル部分のnote:以降のsample.txtを抽出して引数に渡したい場合が厄介で、以下の設定でURLスキーマを参照する事はできるのだが

test3.reg
・・・
[HKEY_CLASSES_ROOT\note\shell\open\command]
@="C:¥¥Windows¥¥System32¥¥notepad.exe %1"

regedit備え付けの引数%1はURLスキーマ全体を渡すのでnote:sample.txtをブラウザに打ち込むと以下のような処理が実行され、note:sample.txtというファイルを開こうとしてしまう。

test3.regを設定した場合、実行される命令
C:¥¥Windows¥¥System32¥¥notepad.exe note:sample.txt

なので以下の設定ではブラウザURLから直接notepadを起動するのをやめ、代わりにコマンドプロンプト(cmd.exe)を起動し、コマンドプロンプト内でmyvarという変数に%1の引数を渡して文字変換をした上でメモ帳(notepad.exe)起動をしている。

test.reg(再掲)
・・・
[HKEY_CLASSES_ROOT\note\shell\open\command]
@="C:¥¥Windows¥¥System32¥¥cmd.exe /k set myvar=%1&call set myvar=%%myvar:note:=%%&call start notepad.exe %%myvar%%"

%1を引数にとって起動する記事はたくさんあったが、パラメータ部分のみ取得する記事はなかったので投稿、文字変換をもっと緻密に記載すればnote://sample.txt?parm1=aaa&param2=bbbみたいなURLにも対応できるが今はここまで

参考

30
27
0

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
30
27