ben432rew
ben432rew

Reputation: 1902

I need to know what a user does with MsgBox. Very basic VBScript

I used to have a script (msgbox.vbs) that was this:

Set objArgs = WScript.Arguments
messageTitle = objArgs(0)
messageText = objArgs(1)
MsgBox messageText, 1, messageTitle

I'm new to coding, and I've never coded in VBScript before, but I needed to change this script so that I can read from my JavaScript script that calls this script whether a user clicked OK or Cancel in the dialogue box that appeared when the MsgBox dialog box gets brought up. So I tried this:

Set objArgs = WScript.Arguments
messageTitle = objArgs(0)
messageText = objArgs(1)
retValue = MsgBox (messageText, 1, messageTitle)

if retValue == 1 Then
WScript.Quit 11
ElseIf retValue == 2 Then
WScript.Quit 22
Else
End If

I figured I'd try and get the return value from MsgBox from what I learned here and then write an if then else statement to get it to quit with an error code that I can read from my callback function in JS that reads the quit code.

From the first script, I was getting an error code of 0, and the dialog box was coming up beautifully. Now, with my new code, I am getting an error code of 1 and the dialog box no longer appears :(

I miss the dialog box, I need it. I also need to know which button the user clicks so I can get on with the rest of my program.

Thanks in advance for any help, I really appreciate it!

Here's the Javascript that calls the .vbs if you're curious (just an edited version of the NPM found here):

/*
 By Tomas Pollak <[email protected]>.
 MIT License.
*/

var join = require('path').join,
    spawn = require('child_process').spawn;

var Dialog = module.exports = {

  info: function(str, title, callback){
    this.show('info', str, title, callback);
  },

  warn: function(str, title, callback){
    this.show('warning', str, title, callback);
  },

  show: function(type, str, title, callback){
    if (!str || str.trim() == '')
      throw('Empty or no string passed!');

    if (typeof title == 'function') {
      callback = title;
      title = null;
    }

    var cmd = [],
        title = title ? title : 'Important';

    var str = (str+'').replace(/([.?*+^$[\]\\(){}<>|`-])/g, "\$1");

    cmd.push('cscript');
    cmd.push(join(__dirname, 'msgbox.vbs'));
    cmd.push(title) && cmd.push(str);

    this.run(cmd, callback);

  },

  run: function(cmd, cb){
    var bin = cmd[0],
        args = cmd.splice(1),
        stdout = '', stderr = '';

    var child = spawn(bin, args);

    child.stdout.on('data', function(data){
      stdout += data.toString();
    })

    child.stderr.on('data', function(data){
      stderr += data.toString();
    })

    child.on('exit', function(code){
      cb && cb(code, stdout, stderr);
    })
  }

}

Upvotes: 2

Views: 271

Answers (1)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38765

The eq operator in VBScript is =, not ==, so change

if retValue == 1 Then

to

if retValue = 1 Then

(everywhere!).

Update wrt comment:

You should use the MsgBox Constants; passing

vbOKCancel - 1 - Display OK and Cancel buttons

instead of a magic number like 1 would avoid misleading other people.

Upvotes: 4

Related Questions