Monday 14 December 2009

MsgBox / InputBox in JScript

Unfortunately JScript does not have several useful features available in VBScript. The most famous things are MsgBox and InputBox. The following code makes them available in JScript too.

To be the more compatible with VBScript it adds so-called "syntax sugar" -- several vb.XXX constants corresponding to vbXXX constants in VBScript. The full original code is available by the following link js::win32::VBScript.js at jsxt.

Source

var vb = {};


vb.Function = function(func)
{
    return function()
    {
        return vb.Function.eval.call(this, func, arguments);
    };
};


vb.Function.eval = function(func)
{
    var args = Array.prototype.slice.call(arguments[1]);
    for (var i = 0; i < args.length; i++) {
        if ( typeof args[i] != 'string' ) {
            continue;
        }
        args[i] = '"' + args[i].replace(/"/g, '" + Chr(34) + "') + '"';
    }

    var vbe;
    vbe = new ActiveXObject('ScriptControl');
    vbe.Language = 'VBScript';

    return vbe.eval(func + '(' + args.join(', ') + ')');
};


/**
 *
 * InputBox(prompt[, title][, default][, xpos][, ypos][, helpfile, context])
 *
 */
var InputBox = vb.Function('InputBox');


/**
 *
 * MsgBox(prompt[, buttons][, title][, helpfile, context])
 *
 */
var MsgBox = vb.Function('MsgBox');

Examples

Compare two codes below -- the first one is JScript and the second one is VBScript.
JScript
var title, res, msg;

title = 'VBScript Emulating';

// The resulting string
res = InputBox('Enter a string', title);

// The message to be displayed
if ( res ) {
    msg = 'You have entered: "' + res + '".';
} else {
    msg = 'Nothing has been entered.';
}

// Displaying of the message
MsgBox(msg, 0, title);
VBScript
Dim title, res, msg

title = "VBScript Emulating"

' The resulting string
res = InputBox("Enter a string", title)

' The message to be displayed
If res <> "" Then
    msg = "You have entered: " & Chr(34) & res & Chr(34) & "."
Else
    msg = "Nothing has been entered."
End If

' Displaying of the message
MsgBox msg, 0, title

10 comments:

  1. Thank you for posting this! This is excellent!

    ReplyDelete
  2. This is a great post and it works on my Windows Seven PC at work, but my Windows Seven PC at home (64-bit) gives me the error that the automation server couldn't be created.

    ReplyDelete
    Replies
    1. To launch this code with success on 64-bit systems you need to run the 32-bit version of the scripting host located at "%windir%\SysWOW64".

      Delete
  3. Nice find! One thing that may be important to realize is that this code will only work on the 32-bit version of cscript/wscript.

    ReplyDelete
    Replies
    1. Another thing to realize is that this solution will not work for strings that have the linefeed character or the carriage return in them. Also, it would be nice if the constants that are available for the MsgBox function VBScript were automatically defined for JScript. I actually used your awesome find, modified it, and posted a more in-depth solution on my site (of course with a link back to here).

      Delete
    2. Hi Chris,

      Thank you for your big interest in my finds. I kindly appreciate you for reporting the issue of strings having special characters in them. I am going to fix this later.

      Delete
  4. JScript have Popup method - analog MsgBox in VBScript

    ReplyDelete
    Replies
    1. You are totally right! I know this. But Popup and MsgBox differ in their features. The main goal of this emulation is to bring the functionalities of MsgBox to JScript, those are not implemented in Popup.

      Delete
  5. I rarely, rarelly respond to posts. But I feel I absolutely must as this little snippet is worth major gold! I just solved a MAJOR problem at the end of the day on a Friday with this. Thank you!

    ReplyDelete
  6. THank a lot for life-saving post.

    ReplyDelete