RubyスクリプトのGUIをHTAに
rubyで作成した簡易ツールをVisualurubyでGUI化して使用していたのだけれど、ActiveScriptRubyが2.0以降Visualurubyを同梱しなくなってしまったので、その代替策としてGUI部分をHTA(HTML Application)に置き換えてみる実験。
HTA上で選択したファイルパスをWSHでrubyスクリプトへ渡し、rubyスクリプトは文字列をそのまま返すので、HTAの表示エリアに表示させるプログラム。
test.hta
<html> <head> <title>外部APP起動 テスト</title> <script type="text/jscript"> var cmd = 'ruby' // pathは通しておくこと var rubyscript = 'test.rb' // このhtaファイルと同じ場所に置く function exeApp(argument){ var wsh = new ActiveXObject("WScript.Shell"); //データをRubyスクリプトの引数で渡す場合 //var result = wsh.exec(cmd + ' ' + rubyscript + ' ' + argument ); //データをRubyスクリプトの標準入力へ渡す場合 var result = wsh.exec(cmd + ' ' + rubyscript ); result.StdIn.WriteLine(argument); //Writeメソッドで改行なし result.StdIn.Close(); //rubyスクリプト標準出力読み取り var str = ''; while (!result.StdOut.AtEndOfStream) { str = str + result.StdOut.ReadLine(); } //DOMでidタグ'result'のテキスト書き換え document.getElementById("result").innerText=str; } </script> </head> <body> <!--実行ボタンが押されたら、選択したファイルパスをexeAppの引数として渡す--> <form name="form01"> <input type="file" name="fileselect" size=50> <input type="button" value="実行" onclick="exeApp(document.form01.fileselect.value)"> </form> <hr> <div id="result">Rubyスクリプトが返す文字列をここに表示</div> </body> </html>
test.rb
# coding: windows-31j # 引数の文字列を標準出力へ出力する #print ARGV[0] # 標準入力の文字列を標準出力へ出力する lines = [] while(line = STDIN.gets) do lines << line.chomp end lines.each do |line| print line + "<BR>" end