6

I need to do some scripts in java script. I am working on it but couldn't find a few solutions to a few problems.

First of all I need a GOOD tutorial, but not for an internet page but for a DESKTOP script.

Things couldn't find out like : 1) I wanted a simple message box in order to debug my program, I used:

var name = prompt("What is your name","Type Name Here");

When running it I get error of "Object expected"

2) Couldn't find how to open a file

Boris Raznikov
  • 2,373
  • 10
  • 34
  • 56

3 Answers3

16

Based on your comments, I guess that you are attempting to run a JavaScript file directly on Windows. Double-clicking on a .js file in windows will (probably) run it in Windows Script Host.
The prompt() function will not work this way, since WSH provides a completely different API than browser-embedded engines.

The following code should accomplish your intentions. However if you want anything more than a simple popup, HTAs are the only way to do complex GUIs with JScript on the desktop.

var fso, ws, ts;
fso = new ActiveXObject('Scripting.FileSystemObject');
ws = WScript.CreateObject('WScript.Shell');

var ForWriting= 2;
ts = fso.OpenTextFile('foo.txt', ForWriting, true);
ts.WriteLine(new Date().getTime());
ts.Close();

ws.Popup('Wrote to file!');

var ForReading= 1;
ts = fso.OpenTextFile('foo.txt', ForReading, false);
var fileContents = ts.ReadLine();
ts.Close();

ws.Popup('The file contained: ' + fileContents);

WScript.Quit();
brianpeiris
  • 10,735
  • 1
  • 31
  • 44
  • Do you know how to do InputData from a message box ? – Boris Raznikov Oct 17 '09 at 23:04
  • When trying to use : var theResponse = window.prompt("Welcome?","Enter your name here."); I get error of window is undefined (I am in desktop application) – Boris Raznikov Oct 17 '09 at 23:33
  • 1
    JScript does not support an prompt-like popup but VBScript does. There is a tutorial about calling a VBScript "InputBox" from JScript here: http://wsh2.uw.hu/ch08c.html That tutorial also describes how to show a prompt via Internet Explorer. – brianpeiris Oct 18 '09 at 00:17
  • OK, I saw the link and it is great, but how can you do it in application that it is not a web, but a desktop application. How can you tell the interpertor that now you are using vbscript ? – Boris Raznikov Oct 18 '09 at 09:12
  • 2
    The tutorial I linked to spells it out completely. Just copy the code under "Listing 8-2" into a file with the extension ".wsf" and run it. – brianpeiris Oct 18 '09 at 09:44
  • Yes, it worked thank you. I would like to understand why you needed the xml and why wsf and not vbs or js (is wsf is the combination of the two script languagr ?) – Boris Raznikov Oct 18 '09 at 13:06
  • 1
    'wsf' stands for Windows Script File. It is a container for scripts and it provides you with a few more features compared to bare script files. Read all about it here: http://msdn.microsoft.com/en-us/library/15x4407c%28VS.85%29.aspx – brianpeiris Oct 18 '09 at 15:22
6

I have to ask: why is JavaScript the right tool for the job? Why not use a scripting language intended to be used this way, such as Python, Ruby, Lua, ... etc?

If you are using Microsoft's JScript (and it sounds like you are), look to the MSDN web site for help. The page here looks fairly good. Google can also help with that.

Assuming you don't mind using Java, you could also use the Mozilla Rhino shell. But it doesn't look like there is a standard way of reading from the console in JavaScript. (presumably since this is not something typically required in a JavaScript application...) The built in JavaScript functions in the shell seem fairly basic, but you can read a file.

There area also examples of using Rhino, which may be helpful. You can interface with the Java API to do whatever else you need to do.

Edit: I wrote this answer a long time ago; today I would use node.js. See their learning page.

mpontillo
  • 13,559
  • 7
  • 62
  • 90
  • My knowledge in scriptin is very narrow. I wrote like 2,3 scripts my all life. I needed it more 2 days ago and I looked in vbs and js. I will look more in python. My needed script is for something small. – Boris Raznikov Oct 17 '09 at 23:06
  • When trying to use : var theResponse = window.prompt("Welcome?","Enter your name here."); I get error of window is undefined (I am in desktop application) – Boris Raznikov Oct 17 '09 at 23:33
  • http://web.archive.org/web/20130920034605/http://msdn.microsoft.com/en-us/library/72bd815a(VS.80).aspx, https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2010/x85xxsf4(v=vs.100)?redirectedfrom=MSDN – Jan Feb 23 '22 at 16:59
1

The latest prerelease of Opera acts as a runtime for JS applications.

They have tutorials describing how to use it.

I used: var name = prompt("What is your name","Type Name Here");

When running it I get error of "Object expected"

Presumably your runtime doesn't implement prompt that in a way that is compatible with those arguments.

2) Couldn't find how to open a file

This depends on the runtime you use. JS itself doesn't have anything built in to read files (or display a prompt). You need an environment that provides those objects.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I am not looking for web browser comipler application. I am looking for tutorual for desktop application – Boris Raznikov Oct 17 '09 at 18:34
  • 2
    That **is** a tutorial on writting desktop applications using JS — just ones that use Opera as the runtime (and you have to have some sort of runtime (or compiler) in play, otherwise you just have text). The article is very clear that "you can run any widget you like, without ever opening the browser". – Quentin Oct 17 '09 at 18:49
  • I am on indows, so what to use inorder to not getting the object error – Boris Raznikov Oct 17 '09 at 18:53
  • Windows is an operating system. Not a JavaScript runtime. You can use Opera as a JS runtime environment on Windows. (If you want to ask about Windows Scripting Host, then (a) it uses JScript rather than JavaScript and (b) ask a question about WSH instead of "desktop JS".) – Quentin Oct 17 '09 at 19:00