tags:

views:

224

answers:

5

Hello,

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 inorder 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

+1  A: 

This question might be of interest to you: http://stackoverflow.com/questions/109399/can-you-do-desktop-development-using-javascript

lomaxx
Not helped, I am not looking for compiler ornew language script
Roman Dorevich
+1  A: 

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.

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

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.

Mike
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.
Roman Dorevich
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)
Roman Dorevich
+3  A: 

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
Do you know how to do InputData from a message box ?
Roman Dorevich
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)
Roman Dorevich
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
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 ?
Roman Dorevich
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
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 ?)
Roman Dorevich
'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
+1  A: 

Try this link. I found it invaluable.

Gutzofter
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)
Roman Dorevich