Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tutorial for using JavaScript on a Desktop

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

like image 751
Boris Raznikov Avatar asked Oct 17 '09 18:10

Boris Raznikov


1 Answers

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();
like image 88
brianpeiris Avatar answered Sep 20 '22 14:09

brianpeiris