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
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.
If you have Windows, then you already have Windows Script Host. You can use that to execute javascript programs within Windows.
https://docs.microsoft.com/en-us/previous-versions/9bbdkx3k(v=vs.85)
https://en.wikipedia.org/wiki/Windows_Script_Host
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With