Tuesday, January 20th, 2009

Seed the desktop world with lots of JavaScript

Category: JavaScript

javascript

  1. #!/usr/bin/env seed
  2.  
  3. // Import libraries that are used by the program
  4. Seed.import_namespace("Gtk");
  5.  
  6. // Initialize GTK+
  7. Gtk.init(null, null);
  8.  
  9. // Create the main application window and set the title
  10. var window = new Gtk.Window({title: "Hello World"});
  11. // Make the program terminate when the window is closed
  12. window.signal.hide.connect(Gtk.main_quit);
  13.  
  14. // Create a button with a label
  15. var button = new Gtk.Button({label: "Click Me!"});
  16.  
  17. // Make the button display text when it is pressed
  18. // It passes an inline anonymous function to the signal handler
  19. button.signal.clicked.connect(function(w) {
  20.   Seed.print("Hello world!");
  21. });
  22.  
  23. // Add the button to the window and display everything
  24. window.add(button);
  25. window.show_all();
  26.  
  27. // Start the main GTK+ loop and initiate the program
  28. Gtk.main();

The revolution is happening. JavaScript continues to grow from onmouseover handler for cheesy scripts to powering the Web client, desktop, and server. Above is code for powering the Linux client.

Ryan Paul of Ars Technica has a great piece on building desktop Linux applications with JavaScript that delves into various JavaScript layers that you have available, including Seed the GObject JavaScriptCore bridge:

Developers who build GTK+ applications with Seed will have to use other GObject-based libraries from the GNOME stack in order to produce full applications. For example, native file access and basic remote data retrieval can be done with the Gio library.

By default, Seed has access to all of the libraries for which GIR data is available. The gobject-introspection-repository package in the PPA includes type metadata for a big chunk of the stack, including Cairo, Clutter, Avahi, GTK+, GConf, OpenGL, GooCanvas, WebKit, Poppler, Pango, libsoup, libnotify, and libxml2.

Seed exposes the APIs of those libraries in a JavaScript-friendly way with good support for type conversion. Object properties are accessible as regular JavaScript attributes, JavaScript functions can be connected to signals as callbacks, you can use JavaScript’s key/value associative arrays to pass named parameters into constructors, and you can subclass widgets and create new GObject types that implement their own signals. This means that using GObject libraries in JavaScript is very clean and seamless.

Individual scripts can be executed with the Seed runtime, just like Python and Ruby scripts. This makes it possible to build applications entirely with JavaScript. Seed provides several functions for importing modules and displaying information at the console. The following example will display a window with a single button. When the button is pressed, it will display some text at the console.

Posted by Dion Almaer at 6:12 am
5 Comments

+++--
3.9 rating from 18 votes

5 Comments »

Comments feed TrackBack URI

Is there any real world application that anyone can think of where this would be useful?

Comment by diilbert — January 20, 2009

Well most of real world applications DO use graphic interface.

Comment by ywg — January 20, 2009

Linux Equivalent of HTA’s?

Comment by TNO — January 20, 2009

Hmm. I’d usually choose something like Adobe AIR and keep the apps working on Windows and Mac as well. But I guess if you need more access to the system than AIR provides, and you don’t mind being Linux-only, this would be good.

Comment by Nosredna — January 20, 2009

See also QtScript.

Comment by codedread — January 20, 2009

Leave a comment

You must be logged in to post a comment.