Make your javascript a Windows .exe

August 31st, 2007. Tagged: .net, JavaScript

These days an average web developer needs to have a broad matrix of skills in order to do his/her job. HTML, CSS, JavaScript, AJAX, XML, some server side language, some linux skills, some apache, some database skills, standards, accessibility, SEO, the list goes on. Parts of the list are also multiplied by (or raised to the power of?) the number of browsers you want to support. Crazy, isn't it? We're such optimists trying to make stuff work in such an environment.

There's gotta be an easier way to write code that does something meaningful! Yes, there is, it's called JavaScript. You learn JavaScript, you learn it well, and you don't need to learn anything else. Isn't that cool? JavaScript is, practically, everywhere. Learn JavaScript and you can:

  • create rich and powerful web applications (the kind that runs inside the browser)
  • write server-side code such as ASP scripts or for example code that is run using Rhino (A JavaScript engine written in Java)
  • create rich media applications (Flash, Flex) using ActionScript which is based on ECMAScript, which is JavaScript
  • write scripts that automate administrative tasks on your Windows desktop, using Windows Scripting Host
  • write extensions/plugins for a plethora of desktop application such as Firefox or Dreamweaver
  • create web applications that store information off-line on user's desktop, using Google Gears
  • create Yahoo!, or Mac, or dunno-what-type-of widgets
  • create Windows apps (those that end in .exe) and libraries (.dll)

I'm sure the list above is not even complete.

OK, it's a joke that with one programming skill only you'll be employed for life, but it's a fun thought anyway. Off to the main topic of the post.

JScript

This is Microsoft's version of JavaScript (yep, the thing that annoys us *sometimes* in IE) and can also be used to create server side pages (ASP, ASP.NET) or desktop applications. Apparently JScript is now called JScript.NET and can be compiled to create .exe files. Let's see how.

The compiler

The compiler (program that creates programs) is an exe file called jsc.exe (JScriptCompiler) and is part of the .NET framework. Good news is that you can use it without installing any MS IDE (whatever Visual Studio is called these days), free of charge. Even better, maybe it's already there, on your machine. I searched my completely normal Windows XP machine that doesn't have any special MS tools and was able to find two copies of the compiler! You can search for "jsc.exe" and in case you don't already have it, you can read how to get it here.

So once you find your jsc.exe (found one o' mine in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727), then add this path to your environment path: Right-click My Computer - Advanced - Environment Variables - System Variables - Path - Edit

Now open command prompt (Start - Run - "cmd" - OK) and type "jsc"+ENTER. You should see a list of help options for the compiler. Cool!

First .exe (in years)

The last time I created an .exe file is probably yeeears ago, when I was this Visual Basic king, writing a desktop application that takes a directory of images and creates a web gallery (example)

OK, lets create a simple application.

cd ..
mkdir myapps
cd myapps

Create a file called hello.js with the following content:

var d = new Date();
var n = Math.random();
print('Hello, \\ntoday is ' + d + '\\nand this is random - ' + n);

Now let's compile!

C:\\myapps>jsc hello.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.

No error messages, so we'll take that as an OK ;) Let's check:

C:\\myapps>dir
 Volume in drive C has no label.
 Volume Serial Number is B96A-95DB

 Directory of C:\\myapps

08/31/2007  07:33 PM    <DIR>          .
08/31/2007  07:33 PM    <DIR>          ..
08/31/2007  07:34 PM             4,096 hello.exe
08/31/2007  07:33 PM               109 hello.js
               2 File(s)          4,205 bytes
               2 Dir(s)  40,287,092,736 bytes free

YES! An .exe was created! Without further ado, let's run it already!

C:\\myapps>hello
Hello,
today is Fri Aug 31 19:34:32 PDT 2007
and this is random - 0.5855108083158316

That's so cool, the compiled program works!

Making a DLL

Now, we're convinced that we have a good thing going here, so let's create a DLL, meaning create a library that other applications can use.

JScript.NET has the notion of namespaces and packages (which we usually fake on the web) and class-based objects (eww! well, it supports the prototype stuff as well). So if we simply wrap our code in a package and a class and we create a new file LibHello.js:

package LibHello {
    class Hello {
        function say() {
            var d = new Date();
            var n = Math.random();
            return 'Hello, \\ntoday is ' + d + '\\nand this is random - ' + n;
        }
    }
}

Let's compile this into a library, we need the /t:library option when compiling

C:\\myapps>jsc /t:library LibHello.js

This creates hello.dll and we have a library!

Consuming the lib

Finally, let's create an app that leverages the new library we just created.

Create consumer.js with the following:

import LibHello;
var h = new LibHello.Hello();
print(h.say());

Compile and run:

C:\\myapps>jsc consumer.js

C:\\myapps>consumer.exe
Hello,
today is Fri Aug 31 19:53:29 PDT 2007
and this is random - 0.45013379838789525

Nice and easy.

So what?

I didn't have time to experiment, but I'm pretty sure you can take tools such as jsmin or jslint and easily compile them into libraries that can be consumed from windows apps, or VBA scripts in Access, Powerpoint, etc. Imagine you're writing some documentation in Word, you select some JS code you just wrote and JSlint it. That would be nice.

BTW, remember how we used /t:library option to produce a .dll and not an .exe? Well, there's also the option /t:winexe which creates a windows application I mean with the window and everything and not a console app. OK, let's give it a shot, create win.js with the following:

import System.Windows.Forms; // this has a MessageBox class
import LibHello;

var h = new LibHello.Hello();
MessageBox.Show(
        h.say(),
        "Dude!",
        MessageBoxButtons.OK,
        MessageBoxIcon.Exclamation
);

Compile:

C:\\myapps>jsc /t:winexe win.js

Double click in windows explorer and you have a nice little unquestionably useful Windows application ;)

Tell your friends about this post: Facebook, Twitter, Google+

89 Responses

  1. [...] YouTube Make your javascript a Windows .exe » This Summary is from an article posted at phpied.com on Friday, August 31, 2007 This article’s contents are copywritten by the author of phpied.com . Please click "View Original Article…" below to view the article. Summary Provided by Technorati.comView Original Article at phpied.com » 10 Most Recent News Articles About Yahoo [...]

  2. [...] Contact the Webmaster Make your javascript a Windows .exe » This Summary is from an article posted at phpied.com on Friday, August 31, 2007 This article’s contents are copywritten by the author of phpied.com . Please click "View Original Article…" below to view the article. Summary Provided by Technorati.comView Original Article at phpied.com » 10 Most Recent News Articles About UN Studio [...]

  3. [...] Make your JavaScript a Windows .exe Can I add Windows developer to my resume? :-) Right under my nose, all that time, who’d a thunk it? (tags: JavaScript Windows) [...]

  4. the problem with jsc.exe if that it gonna produce a .NET exe
    so anyone wanting to use that exe would need to have .NET installed

    a nice alternative that can also compile exe from javascript is JSDB :)
    http://www.JSDB.org

    it reuse the SpiderMonkey engine, does not need to have .NET installed,
    and can easyly generate an exe

    copy /b jsdb.exe+myapp.zip myapp.exe

    in myapp.zip
    your main entry point will be main.js

    so basically to have a nice exe you could do

    main.js
    —-
    var d = new Date();
    var n = Math.random();
    writeln(‘Hello, \ntoday is ‘ + d + ‘\nand this is random – ‘ + n);
    —-

    then
    zip -9 -j -u myapp.zip main.js

    then
    copy /b jsdb.exe+myapp.zip myapp.exe

    then
    upx -9 myapp.exe

    :)

  5. Nice, thanks Zwetan, never heard of JSDB before…

  6. good text thank you

  7. This is great!!
    All seems to work however I wanted to use the dll as a component in a web page. So the source code is protected and other reasons. However i could not use the “regsvr32″. After some reading I found out about “regasm”.
    Finally I ran the ASP page but got an error with no real details??
    “Server object, ASP 0177 (0×80070002)”

  8. Another possible alternative is ScriptCryptor from http://www.abyssmedia.com/scriptcryptor/
    It support built-in WScript object and create standalone win32 executables with customizable resources like Version Info, Icon…
    But its not freeware…

  9. Thanks zwetan…
    i got it which i want to use in my desktop and web application.
    its also open source http://www.jsdb.org

  10. Thanks! Using JSC I create my first exe application, but I have question: can I show input or prompt fields in my application, if yes then how and what is script to do that?

  11. Hi Arvis, I think you should check the docs for this System.Windows.Forms package. There’s gotta be prompt, not only MessageBox ;)

  12. Ok, I try ListBox.Show() and TextBox.Show(), but then, when I complite my script I get error: “error JS1246: Type ‘System.Windows.Forms.ListBox’ does not have such a static member”! What it mean and how can I fix this error?

  13. THANKS!!!

  14. I use ExeScript from http://www.scriptcode.com/ to convert scripts to exe. It encrypts file content to protect it from modification by other users.

  15. Nice article.
    I tested the example shown below and it works fine.

    import System.Windows.Forms;
    System.Windows.Forms.MessageBox.Show(“Welcome! Press OK to continue.”);
    MessageBox.Show(“Great! Now press OK again.”);

    However when I double click on the executable in Windows Explorer it produces a DOS cmd window.
    How can I supress teh DOS cmd window when a User double clicks the executable?

  16. Using JSC I create my first exe application, but I have question: can I show input or prompt fields in my application, if yes then how and what is script to do that? Thank you.

  17. [...] You are wrong, again. You can code in JavaScript for the server, create Windows executable files (.exe), create  plugins and extensions for a plethora of applications, and actually even Flash’s [...]

  18. Very good coded :) thanks

  19. The tips are very imformative really, thanx

  20. hi,

    This is a very nice article.

  21. Thanks! Using JSC I create my first exe application, but I have question: can I show input or prompt fields in my application, if yes then how and what is script to do that?

  22. [...] for various extensions, you can script Photoshop operations with JavaScript if you feel like it. Or compile Windows executables. You see where I’m going with this. JavaScript is [...]

  23. i think you need to reference your consumer.js
    >jsc /reference:LibHello.dll consume.js

  24. [...] for various extensions, you can script Photoshop operations with JavaScript if you feel like it. Or compile Windows executables. You see where I’m going with this. JavaScript is [...]

  25. Well, I recommend ExeScript or ExeScript Pro. Nice IDE and compilation utility, small size, full WScript support, debug, NET is NOT required

  26. [...] Ausführlicher Artikel hier Bewerte diesen Beitrag:  Loading [...]

  27. Thanks! Using JSC I create my first exe application, but I have question: can I show input or prompt fields in my application

  28. thank you ;) Thanks! Using JSC I create my first exe application, but I have question: can I show input or prompt fields in my application

  29. php, javascript, thank you because of your support on issues such as. I wish you continued success.

  30. Thanks! Using JSC I create my first exe application

  31. Hi everybody. Thanks! Using JSC I create my first exe application, but I have question: can I show input or prompt fields in my application

  32. thanks great post, always fallow your blog!

  33. nice article thank you özel güvenlik firması

  34. thank you your greatings

  35. beko klima servis, bakım ve montaj servisi

  36. What should the environment variable PATH Be changed to?
    GIVE EVERYTHING NEEDED OR TO KNOW OR TELL WHERE TO FIND IT OR DON’T TRY TO HELP AT ALL!

  37. Never mind, i got it to go and .bat ed it to compile easier.

  38. Excellent tips .I really appreciate all these points, and I agree completely…
    85

  39. However when I double click on the executable in Windows Explorer it produces a DOS cmd window.
    How can I supress teh DOS cmd window when a User double clicks the executable?

  40. http://www.heartratemonitorguides.com/heart-rate-monitor-watches.php Thanks for that awesome posting. It saved MUCH time :-)

  41. RE: How can I supress teh DOS cmd window when a User double clicks the executable?

    Compile using the /t:winexe switch — like the article says

  42. How can I supress teh DOS cmd window when a User double clicks the executable?

  43. Never mind, i got it to go and .bat ed it to compile easier.

  44. thanks great post, always fallow your blog!

  45. [...] 「make your javascript a windows . exe」 [...]

  46. Very good post with useful information. I really appreciate the fact that you approach these topics from a stand point of knowledge and information. Please keep on posting.

  47. Lol, I was about to be so sad when I first started reading your article.

    I’m going to learn C++, JavaScript (already learning MooTools), PHP (know quite a bit of this one), and Python. Really, I see programming as farming or carpentry in terms of tools.

    You need different tools for different types of problems and the discipline of knowing when to use each one. ;)

  48. cursos Dreamweaver…

    Make your javascript a Windows .exe / Stoyan’s phpied.com…

  49. Hi there, this is a great article, I had no idea you could do this with Javascript.

    Based on your article I wrote something similar about making simple EXEs with PHP and Bamcompile –

    http://pojomcbooty.tumblr.com/post/10683603233/windows-registry-scripting-with-php-compiled-to-exe

    I was wondering if you know if there’s a way in either of these languages to hardcode in some other credentials to “run as”? At the moment I have to use Autohotkey for such scripting but I find it a bit flaky to be honest. If I could do it in JS or PHP I would much prefer to do so..

    Thanks

  50. Thank you very nice on good shares were explained.

  51. Make your javascript a Windows .exe / Stoyan’s phpied.com…

  52. Very good post with useful information. I really appreciate the fact that you approach these topics from a stand point of knowledge and information. Please keep on posting. ss

  53. I think that is one of the such a lot vital information for me. And i’m happy studying your article. However want to statement on some common issues, The website taste is wonderful, the articles is in reality excellent : D. Good task, cheers

  54. I simply could not leave your site before suggesting that I really enjoyed the usual information an individual supply for your visitors? Is going to be again steadily to inspect new posts

  55. ipad…

    [...]Make your javascript a Windows .exe / Stoyan’s phpied.com[...]…

  56. durucambalkonsistemleri…

    [...]Make your javascript a Windows .exe / Stoyan’s phpied.com[...]…

  57. Where can i get infos on making GUIs with JScript?

  58. I?ve been exploring for a little for any high-quality articles or weblog posts on this kind of house . Exploring in Yahoo I ultimately stumbled upon this site. Reading this info So i am happy to exhibit that I have a very just right uncanny feeling I discovered exactly what I needed. I so much indubitably will make certain to do not omit this web site and provides it a glance regularly.

  59. [...] eredeti bejegyzés a JScript-ről szól: ha már tudunk JavaScript-ben programozni, miért ne ezzel végeznénk a [...]

  60. gazetekolik

  61. C:\Users\jay\Desktop\myapps>jsc hello.js
    ‘jsc’ is not recognized as an internal or external command,
    operable program or batch file.

  62. respect to labor
    Ahsap Kapi

  63. This is an awesome post. Please share with us the new tutorials about the coding and the features like the avatars and their updates.
    jerseys hockey

  64. pfgrafik…

    [...]Make your javascript a Windows .exe / Stoyan’s phpied.com[...]…

  65. ingilizce eğitim seti kursu ders çalışma konusunda en iyi ingilizce dil sitesi. Kelime cümleler ve test ile ingilizce türkçe çeviri metin ve sözlük sunmaktadır.

    ingilizce

  66. I believe that is one of the so much significant info for me. And i am happy reading your article. However should commentary on few general things, The site taste is ideal, the articles is actually excellent : D. Good activity, cheers

  67. usefull article. thanks for job

  68. perde modelleri

  69. Very good post with useful information. I really appreciate the fact that you approach these topics from a stand point of knowledge and information. Please keep on posting. ss

  70. I believe that is one of the so much significant info for me

  71. Lol, I was about to be so sad when I first started reading your article.

  72. That is very interesting, You are an overly professional blogger. I’ve joined your feed and look ahead to in quest of more of your wonderful post. Also, I’ve shared your web site in my social networks

  73. thanks

  74. yes good

  75. Thanks for choice web sites

  76. thank you . Web site veryfull

  77. Verif nice a post hank

  78. nice admin thanks

  79. thansk admin

  80. wow

  81. thansk wow admin ofr nice post

  82. işitme cihazı

  83. and the expressions for shares of fine thank you.
    http://www.solmazmetal.com.tr/abkant-bukum

  84. I know this site offers quality depending articles and extra
    information, is there any other web site which presents these kinds of things in
    quality?

  85. Excellent article and steps and info , thanks for sharing this with us

    -Raju

  86. woow thanks admin nice post

  87. […] Quelle: Ausführlicher Artikel hier […]

  88. However when I double click on the executable in Windows Explorer it produces a DOS cmd window.
    How can I supress teh DOS cmd window when a User double clicks the executable?

    kaydırak

  89. yes good

Leave a Reply