7

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

3 Answers 3

17

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();
Sign up to request clarification or add additional context in comments.

7 Comments

Do you know how to do InputData from a message box ?
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)
JScript does not support an prompt-like popup but VBScript does. There is a tutorial about calling a VBScript "InputBox" from JScript here: wsh2.uw.hu/ch08c.html That tutorial also describes how to show a prompt via Internet Explorer.
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.
'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: msdn.microsoft.com/en-us/library/15x4407c%28VS.85%29.aspx
|
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.

3 Comments

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.
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)
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.

4 Comments

I am not looking for web browser comipler application. I am looking for tutorual for desktop application
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".
I am on indows, so what to use inorder to not getting the object error
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".)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.