A Micro Toolbox

ソフトウェアのニッチな問題の対処記録です

新規作成ファイルのテンプレートを右クリックメニューから直接コピーする

テンプレートファイルを開いているフォルダにコピー(回りくどいけど楽に安全に) - Toolbox
Perl/Tkx版テンプレートファイル選択ダイアログ - Toolbox
の続き。

拡張子.templateを呼び水にしなくても、フォルダの右クリックメニューから直接テンプレート選択ダイアログを開けるようにした。


レジストリエディタで
\HKEY_CLASSES_ROOT\Directory\Background\shell
の下にtemplateというキーを作成。

値を「Template」などにする。この値が右クリックメニューに表示される。


作成したキーtemplateの下にcommandというキーを作成。

値を
C:\usr\bin\wperl "C:\tool\file\copy_template\copy_template.pl" "%V"
にする。
wperl.exeのパスと次のcopy_template.plのパスは置き場所にあわせて適宜変更。


copy_template.plを修正。
dirname()する必要がなくなった。

#!/usr/bin/perl --

use utf8;
use strict;
use Encode;
use Tkx;
use File::Basename;
use File::Copy;

# 雛形ファイルの置き場。
# セパレータは\\にしないと動作しない。
# 末尾の\\がないと2回目以降最後に開いたフォルダが開かれてしまう。
my $TemplateDir = "C:\\home\\etc\\templates\\";

my $target_dir = $ARGV[0];

if($target_dir eq "")
{
	$target_dir = $TemplateDir;
}

my $top = Tkx::widget->new(".");
$top->g_wm_withdraw();

my $template_file = Tkx::tk___getOpenFile(
	-parent => $top,
	-initialdir => "$TemplateDir",
	-title => decode('cp932', $target_dir) . 'コピーするテンプレートを選択');
	
if($template_file)
{
	copy(encode('cp932', $template_file), $target_dir);
}

# EOF

%Vは作業ディレクトリを表す変数。

cf.
http://superuser.com/questions/136838/what-is-the-list-of-special-variables-available-when-writing-a-shell-command-for