13

I'm looking for way to write Javascript programs / scripts on desktop, not inside browser. I want it to run like Python - from the command line, to be able to read files, write files etc. All solutions I find mentioned (Rhino, spidermonkey, V8) are for embedding. Has anyone done simple implementation for just writing standalone programs with full capabilities of OS access etc.?

For windows preferably, or maybe Linux

zaharpopov
  • 16,882
  • 23
  • 75
  • 93

5 Answers5

8

If you have Windows, then you already have Windows Script Host. You can use that to execute javascript programs within Windows.

https://learn.microsoft.com/en-us/previous-versions/9bbdkx3k(v=vs.85)

https://en.wikipedia.org/wiki/Windows_Script_Host

Hans Kesting
  • 38,117
  • 9
  • 79
  • 111
8

Hans mentioned the Windows Script Host already, but there's a cool compiler for JScript and you probably already have it installed (it comes with .NET). You can write your JavaScript using any of the .NET libraries and compile it to a Windows .exe file. See this link for an introduction to compiling with jsc. You should be able to find more from there.

This doesn't let you run from the command line like a script without compiling first, but you might be interested in it anyway since it gives you access to so many libraries.

Here's a barebones test program. jsc is already on my path, type jsc in a command prompt to see if it's on yours.

test.js:

import System.io;
import System;

function test()
{
   Console.WriteLine("test");
};

function test2(arg)
{
   Console.WriteLine(arg);
};

test();
test2("argtest");

Compiling and running:

C:\test>jsc test.js
Microsoft (R) JScript Compiler version 8.00.50727
for Microsoft (R) .NET Framework version 2.0.50727
Copyright (C) Microsoft Corporation 1996-2005. All rights reserved.


C:\test>dir
 Volume in drive C has no label.

 Directory of C:\test

03/05/2010  09:19 AM    <DIR>          .
03/05/2010  09:19 AM    <DIR>          ..
03/05/2010  09:26 AM             5,120 test.exe
03/05/2010  09:23 AM               178 test.js
               2 File(s)              - bytes
               3 Dir(s)               - bytes free

C:\test>test.exe
test
argtest

C:\test>

There seem to be some severe limitations that I ran into immediately in making that test program. For example, functions appear to be more strongly typed than in the browser environment. I couldn't call test() and pass an argument unless I defined that parameter as part of the function definition. If you're familiar with browser-hosted JavaScript you know that you can define a function with any number of parameters and call it with any number of arguments.

So it appears that writing applications in JavaScript on the desktop will be quite a different experience if there are a lot more disparities with how you're used to using it.

Jonathon Faust
  • 12,396
  • 4
  • 50
  • 63
  • That's really awesome. Note that you may have to edit your path. Also, this was in my .NET Framework 2.0 but not 3.5. – JasonFruit Mar 05 '10 at 14:51
  • so it available only with old .NET and uses .NET system modules? – zaharpopov Mar 05 '10 at 15:57
  • @zaharpopov .NET 2 included the jsc.exe compiler, .NET 3.5 did not. I don't know what you mean by .NET system modules. You can use imports as I showed in the example, but they don't have to be from the System namespace. I just needed System to use Console.WriteLine. As for the future of the JScript compiler, not sure anyone knows...it doesn't look like it's ever been very popular. JavaScript has always been looked down upon by "real" developers. – Jonathon Faust Mar 05 '10 at 16:18
  • Looked down upon by "real" developers? It's almost 2016, gay marriage is legal, we can do full face transplants, SpaceX is planning on putting a man on Mars, when will developers stop being bigots? I thought we were a progressive bunch, yet we have more fanboys and flamewars and general hate via languages than even the iPhone/Android crowd. Let people program in whatever language they want. – Nick Steele Nov 19 '15 at 03:17
4

There is Node.js that allows you to write server side JavaScript. Node.js works on Linux, Mac and Windows.It also has a really good REPL so you can start it up on from your terminal to write JavaScript and see how it works.

Norbu Tsering
  • 609
  • 5
  • 14
AutomatedTester
  • 22,188
  • 7
  • 49
  • 62
2

The simplet way to develop desktop applications is to use Node Webkit -- https://github.com/rogerwang/node-webkit

Good luck!

denysonique
  • 16,235
  • 6
  • 37
  • 40
0

Seed works on the GNOME platform. Adobe AIR would be another approach. See this question: Can you do Desktop Development using JavaScript?.

Community
  • 1
  • 1
ntownsend
  • 7,462
  • 9
  • 38
  • 35