# Hello World (JavaScript)

Susanna Huhtanen
ihmis.suski@gmail.com 2012
Tiffany Antopolski
tiffany.antopolski@gmail.com
A basic "hello, world" application
Mario Blättermann
mario.blaettermann@gmail.com 2011, 2013, 2016, 2018

# How to build, install and create a tar.xz of a Hello World program

          This tutorial will demonstrate how to:

            create a small "Hello, World" application using JavaScript and GTK+
            make the .desktop file
            how to set up the build system

# Create the program

## Skript zum Ausführen der Anwendung

This needs to be the first line of your script:

#!/usr/bin/gjs

It tells the script to use Gjs. Gjs is a JavaScript binding for GNOME.

## Zu importierende Bibliotheken

const Lang = imports.lang;

imports.gi.versions.Gtk = '3.0' const Gtk = imports.gi.Gtk;

In order for our script to work with GNOME, we need to import GNOME libraries via GObject Introspection. Here we import the language bindings and GTK+, the library which contains the graphical widgets used to make GNOME applications.

## Erstellen des Hauptfensters der Anwendung

const Application = new Lang.Class({ //A Class requires an explicit Name parameter. This is the Class Name. Name: 'Application',

//create the application _init: function() { this.application = new Gtk.Application();

//connect to 'activate' and 'startup' signals to handlers. this.application.connect('activate', Lang.bind(this, this._onActivate)); this.application.connect('startup', Lang.bind(this, this._onStartup)); },

//create the UI _buildUI: function() { this._window = new Gtk.ApplicationWindow({ application: this.application, title: "Hello World!" }); },

//handler for 'activate' signal _onActivate: function() { //show the window and all child widgets this._window.show_all(); },

//handler for 'startup' signal _onStartup: function() { this._buildUI(); } });

GtkApplication initializes GTK+. It also connects the x button that's automatically generated along with the window to the "destroy" signal.

We can start building our first window. We do this by creating a variable called _window and assigning it a new Gtk.ApplicationWindow.

We give the window a property called title. The title can be any string you want it to be. To be on the safe side, it's best to stick to UTF-8 encoding.

Now we have a window which has a title and a working "close" button. Let's add the actual "Hello World" text.

## Label for the window

// Add a label widget to your window this.label = new Gtk.Label({ label: "Hello World" }); this._window.add(this.label); this._window.set_default_size(200, 200);

A text label is one of the GTK+ widgets we can use, on account of having imported the GTK+ library. To use it, we create a new variable called label, and assign it a new Gtk.Label. Then we give it properties inside the curly braces {}. In this case, we're setting the text that the label will hold. Finally, we create and run the application:

//run the application let app = new Application(); app.application.run(ARGV);

Gtk.ApplicationWindow can only hold one widget at a time. To construct more elaborate programs you need to create a holder widget like Gtk.Grid inside the window, and then add all the other widgets to it.

## hello-world.js

Die vollständige Datei:

#!/usr/bin/gjs

const Lang = imports.lang;

imports.gi.versions.Gtk = '3.0' const Gtk = imports.gi.Gtk;

class Application {

//create the application constructor() { this.application = new Gtk.Application();

//connect to 'activate' and 'startup' signals to handlers. this.application.connect('activate', this._onActivate.bind(this)); this.application.connect('startup', this._onStartup.bind(this)); }

//create the UI _buildUI() { this._window = new Gtk.ApplicationWindow({ application: this.application, title: "Hello World!" }); this._window.set_default_size(200, 200); this.label = new Gtk.Label({ label: "Hello World" }); this._window.add(this.label); }

//handler for 'activate' signal _onActivate() { //show the window and all child widgets this._window.show_all(); }

//handler for 'startup' signal _onStartup() { this._buildUI(); } };

//run the application let app = new Application(); app.application.run(ARGV);

## Anwendung im Terminal ausführen

To run this application, first save it as hello-world.js. Then open Terminal, go to the folder where your application is stored and run:

    $ gjs hello-world.js

# Die Datei .desktop.in

Running applications from the Terminal is useful at the beginning of the application making process. To have fully working application integration in GNOME 3 requires a desktop launcher. For this you need to create a .desktop file. The .desktop file describes the application name, the used icon and various integration bits. A deeper insight into the .desktop file can be found here. The .desktop.in file will create the .desktop.

Before continuing, resave hello-world.js as hello-world. Then run this in the command line:

    $ chmod +x hello-world

The example shows you the minimum requirements for a `.desktop.in` file.

[Desktop Entry] Version=1.0 Encoding=UTF-8 Name=Hello World Comment=Say Hello Exec=@prefix@/bin/hello-world Icon=application-default-icon Terminal=false Type=Application StartupNotify=true Categories=GNOME;GTK;Utility;

Save this as hello-world.desktop.in. Now let's go through some parts of the `.desktop.in` file.

## Name

Der Anwendungsname.

## Comment

Eine kurze Beschreibung der Anwendung.

## Exec

Specifies a command to execute when you choose the application from the menu. In this example exec just tells where to find the hello-world file and the file takes care of the rest.

## Terminal

Specifies whether the command in the Exec key runs in a terminal window.

To put your application into the appropriate category, you need to add the necessary categories to the Categories line. More information on the different categories can be found in the menu specification.

In this example we use an existing icon. For a custom icon you need to have a .svg file of your icon, stored in /usr/share/icons/hicolor/scalable/apps. Write the name of your icon file to the .desktop.in file, on line 7. More information on icons in: Installing Icons for Themes and on freedesktop.org: Specifications/icon-theme-spec.

# The build system

To make your application truly a part of the GNOME 3 system you need to install it with the help of autotools. The autotools build will install all the necessary files to all the right places.

For this you need to have the following files:

## autogen.sh

#!/bin/sh

set -e

test -n "$srcdir" || srcdir=`dirname "$0"` test -n "$srcdir" || srcdir=.

olddir=`pwd` cd "$srcdir"

# This will run autoconf, automake, etc. for us autoreconf --force --install

cd "$olddir"

if test -z "$NOCONFIGURE"; then "$srcdir"/configure "$@" fi

After the autogen.sh file is ready and saved, run:

    $ chmod +x autogen.sh

## Makefile.am

# The actual runnable program is set to the SCRIPTS primitive. # # Prefix bin_ tells where to copy this bin_SCRIPTS = hello-world # # List of files to be distributed EXTRA_DIST = \ $(bin_SCRIPTS) # # # The desktop files desktopdir = $(datadir)/applications desktop_DATA = \ hello-world.desktop

## configure.ac

# This file is processed by autoconf to create a configure script AC_INIT([Hello World], 1.0) AM_INIT_AUTOMAKE([1.10 no-define foreign dist-xz no-dist-gzip]) AC_CONFIG_FILES([Makefile hello-world.desktop]) AC_OUTPUT

## Lies mich

Information users should read first. This file can be blank.

When you have the hello-world, hello-world.desktop.in, Makefile.am, configure.ac and autogen.sh files with correct information and permissions, create a README file with installation instructions. Below is a sample of what suitable README instructions may look like:

To build and install this program, run these commands from a terminal:

./autogen.sh --prefix=/home/$USER/.local make install

------------- When running the first command $USER will be replaced by your username.

Running the first command above creates the following files:

aclocal.m4 autom4te.cache config.log config.status configure hello-world.desktop install-sh missing Makefile.in Makefile

Running "make install", installs the application in /home/your_username/.local/bin and installs the hello-world.desktop file in /home/your_username/.local/share/applications

You can now run the application by typing "Hello World" in the Overview.

---------------- To uninstall, type:

make uninstall

---------------- To create a tarball type:

make distcheck

This will create hello-world-1.0.tar.xz