トップ 履歴 一覧 カテゴリ ソース 検索 ヘルプ RSS ログイン

Script/TipsJScript

INDEX

JScriptのメモとか小技とか…

Microsoft JScript

jScriptでいろいろ

 JScript で VBScript の InputBox() を使う (1)

Windows スクリプト ファイル (.wsf) を使用して、VBScript 側で InputBox を表示する関数を作成し、それを JScript 側で呼び出して利用する。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<job id="InputBoxSample">
<script language="VBScript">
    ' InputBoxを利用して入力させる関数
    Function vbInputBox(strprompt)
        ' 必要に応じてタイトルやデフォルト値ももらう
        vbInputBox = InputBox(strprompt)
    End Function
</script>

<script language="JScript">
    // 定義した入力関数を使う
    var str = vbInputBox("文字を入力してね");
    if( typeof(str) == "undefined" ) {
        //「キャンセル」が押された時の処理を記述
        WScript.Echo("キャンセルが押されました。");
    } else {
        //「OK」が押された時の処理を記述
        WScript.Echo("入力文字は「" + str + "」です。");
    }
</script>
</job>

 JScript で Excel VBA の InputBox() を使う

Excel の VBA の InputBox関数を呼び出す。当然だが、Excel が入っていないと使えない。

1
2
3
4
5
6
7
8
9
10
11
var objExcel = WScript.CreateObject("Excel.Application");
var str = objExcel.InputBox("文字を入力してね");
objExcel.Quit();

if( str == false) {
    //「キャンセル」が押された時の処理を記述
    WScript.Echo("キャンセルが押されました。");
} else {
    //「OK」が押された時の処理を記述
    WScript.Echo("入力文字は「" + str + "」です。");
}

記述は簡単。しかし、一時的に Excel が立ち上がる(画面は表示されないがプロセスは作られる)。また、VBAのため、InputBoxのダイアログの形がVBSとは若干ことなる。さらに、タスクバーにタスクが作られないため、最上位に表示されないと他のウインドウを退かす必要がある。

 JScript で VBScript の InputBox() を使う (2)

Windows Script Control を利用して、VBScript の InputBox() を呼び出す。Windows 2000/XP はすでに入っているが、98/ME/NT では、インストールをする必要がある。

関数内部で、「"」は「""」に、「\r」や「\n」も「chr(10)」や「chr(13)」に VBScript の文法に合わせて変換しているので、呼び出す側は、VBScript を意識することなく JScript の関数として使用することができます。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/***** JScript で VBScript の InputBox() 関数を使う関数 *****/
function vbInputBox(){
    var scVB, VBarg, fvartype, i;
    try {
        scVB = new ActiveXObject("ScriptControl");
    } catch(e) {
        WScript.Echo("ScriptControl がない");
        WScript.Quit();
        //return(undefined);
    }
    VBarg = '';
    for (i = 0; i < Math.min(arguments.length, 7); i++){
        fvartype = typeof(arguments[i]);
        if (i > 0){VBarg += ', ';}
        if (fvartype == 'number' || fvartype == 'object' && arguments[i] instanceof Number){
            VBarg += arguments[i].toString();
        } else if (fvartype != 'undefined'){
            VBarg += '\"' + arguments[i].toString().replace(/[\"]/g, '\"\"') + '\"';
        } else if (i < 3 || i == 5){
            VBarg += '\"\"';
        }
    }
    VBarg = VBarg.replace(/[\r\n]+/g, '\" \& $&\"').replace(/[\r]/g, 'Chr\(13\) \& ').replace(/[\n]/g, 'Chr\(10\) \& ');
    scVB.Language = 'VBScript';
    return(scVB.Eval('InputBox\(' + VBarg + '\)'));
}
1
2
3
4
5
6
7
8
var str = vbInputBox("文字を入力してね");
if( typeof(str) == "undefined" ) {
    //「キャンセル」が押された時の処理を記述
    WScript.Echo("キャンセルが押されました。");
} else {
    //「OK」が押された時の処理を記述
    WScript.Echo("入力文字は「" + str + "」です。");
}

参考:スクリプトの開発 Tips (タブブラウザ Sleipnir オンラインデータベース)

最終更新時間:2008年11月17日 20時28分00秒 指摘や意見などあればSandBoxのBBSへ。