none
JScript - StdIn has not been declared? RRS feed

  • Question

  • Greetings fellow scripters,

    I'm trying to learn a little bit about scripting in windows (besides batch scripting), so I found that JScript is a potential alternative to VBScript. However, I find myself fairly confused about all of these different implementations of JScript.

    For starters, there appears to be some implementation in IE which is heavily mixed in and further confused with JavaScript (two very different things, I believe). Furthermore, there appears to be two separate versions available on Windows its self with three different ways of interpreting or compiling it (jscript, wscript, and jsc).

    Anyhow, I've been using jsc for the majority of the time, and Visual Studio to debug the compiled application (doesn't really seem like scripting anymore...), which brings me to my current issue: attempting to ReadLine() (in order to get the program to pause for a moment, from this example) causes the compiler to say that variable StdIn has not been declared.

    Ok, I say, I'll just declare StdIn near the top of my code and (import? define?) the StdIn object:

    var StdIn = new ActiveXObject("WScript.StdIn");

    Ok, the compiler seems happy. I debug my program now, when visual studio gets stuck on that exact line, saying it cannot create the object.

    Alright, I'll just follow the example more closely:

    var StdIn = WScript.StdIn;

    No dice, says the compiler. WScript has not been declared. After a little bit of googling, there appears to be more confusion as to why people are trying to "import" the WScript object: I guess because it doesn't exist yet!

    If someone could help clear up the confusion on both fronts for me, that would be greatly appreciated.

    Wednesday, April 30, 2014 1:57 PM

Answers

  • Here is the minimal example:


    var stdOut = WScript.StdOut;
    var stdIn = WScript.StdIn;
    stdOut.Write("Press ENTER to continue: ");
    stdIn.ReadLine();
    

    Save the above lines as test.js.

    Go to a command prompt, change to the directory where you saved test.js, and type:


    cscript //nologo test.js

    That's it.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by TGP1994 Wednesday, April 30, 2014 7:08 PM
    Wednesday, April 30, 2014 3:40 PM
    Moderator

All replies

  • JScript is JavaScript.

    If you're asking about a Windows Script Host (WSH) script, you don't have to declare stdin because it's part of the host.

    Be specific: What are you trying to do? Tell what you want to do, not how you think it needs to be done.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 30, 2014 2:19 PM
    Moderator
  • JScript is JavaScript.


    Ah, now there's part of the confusion :)

    If you're asking about a Windows Script Host (WSH) script, you don't have to declare stdin because it's part of the host.


    Ok... So if I understand you correctly, I'm actually programming in J(ava)Script on windows for WSH. Simply trying to call ReadLine fails as well, as it is not defined according to the compiler.

    Be specific: What are you trying to do? Tell what you want to do, not how you think it needs to be done.

    which brings me to my current issue: attempting to ReadLine() (in order to get the program to pause for a moment, from this example)

    I know I put up a pretty big wall of text back there, sorry about that.

    EDIT: Well, I think I've learned about J*script. It sounds like JScript and Javascript are more just versions of ECMAScript.

    • Edited by TGP1994 Wednesday, April 30, 2014 3:36 PM Adding some additional information on what I've learned about... scripts.
    Wednesday, April 30, 2014 3:30 PM
  • Here is the minimal example:


    var stdOut = WScript.StdOut;
    var stdIn = WScript.StdIn;
    stdOut.Write("Press ENTER to continue: ");
    stdIn.ReadLine();
    

    Save the above lines as test.js.

    Go to a command prompt, change to the directory where you saved test.js, and type:


    cscript //nologo test.js

    That's it.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by TGP1994 Wednesday, April 30, 2014 7:08 PM
    Wednesday, April 30, 2014 3:40 PM
    Moderator
  • Ok, I think... cscript seizes up on another codeblock, though:

    var userName = WshNetwork.UserName;
    print("Current user: " + userName);

    Saying that an object was expected.

    I think this problem may still stem from more confusion: Why are there about three different interpreters or compilers? In one place on the MSDN, Microsoft tells us to use jsc.exe, but in other places, we're told to use jscript or wscript. It seems like there are more differences than simply compiling the scripts, is that correct?


    EDIT: Whoops, ok, I guess we're not supposed to use print() anymore and just use stdOut.Write? What's going on here?
    • Edited by TGP1994 Wednesday, April 30, 2014 3:49 PM
    Wednesday, April 30, 2014 3:48 PM
  • There is no print method in JScript when executed by WSH.

    stdOut.Write would execute the Write method of the WScript.StdOut object.

    This works only when you execute your script using cscript.exe, so in general you can write output using WScript.Echo.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 30, 2014 3:52 PM
    Moderator
  • Ok, I think... cscript seizes up on another codeblock, though:
    var userName = WshNetwork.UserName;
    print("Current user: " + userName);
    Saying that an object was expected.

    I forgot to mention, you will also need to instantiate the WshNetwork object. Your example would look like this:


    var wshNetwork = new ActiveXObject("WScript.Network");
    WScript.Echo("Current user: " + wshNetwork.UserName);
    


    -- Bill Stewart [Bill_Stewart]

    Wednesday, April 30, 2014 4:46 PM
    Moderator
  • Alright, so perhaps I'm confusing two different implementations of J/avaScript here?

    At any rate, thank you Bill. Things are working much better now!

    Wednesday, April 30, 2014 5:23 PM