Introduction to webOS SDK Development

Introduction to webOS SDK Development

This entry is part 1 of 5 in the series Introduction to webOS SDK Development

This tutorial series will guide you through the process of getting started with webOS app development. In this tutorial, you will learn how to install the webOS development environment and create a simple application. In subsequent parts of this series, we will create a usable application that allows you to browse and display the latest articles on the tuts+ network.

About webOS

Today, web applications are common thanks to advances in modern web browsers. On mobile devices, however, most applications run locally and are built with a compiled programming language such as Objective-C or Java. Because they are built for a specific mobile device, they are very difficult to port to a web site or another mobile platform.

Palm’s operating system for mobile devices (e.g. Palm Pre and Pixi) is appropriately called webOS. One of the distinguishing features of webOS is that it makes all device applications web applications created with HTML, CSS and Javascript by using the Mojo Javascript Framework. Even the native Palm apps on the device are web applications. With the help of HTML5, webOS apps can store data locally, draw 2D graphics, and do CSS transitions/animations. They can also call web services through AJAX to access remote or local data.

The Mojo Javascript Framework loosely follows the Model-View-Controller (MVC) pattern. Views are written in HTML and CSS, while Controllers that control the application logic are written in Javascript. Mojo also includes a set of common UI elements, such as Lists, Buttons, Containers, Dialogs, Indicators, and Pickers. To access lower layers of the operating system, Mojo comes with a set of Javascript APIs to interface with device services (e.g. Alarms, Contacts, Calendars, Photos) or hardware components (e.g. GPS, Display, Camera, Power Supply).

In short, if you know HTML, CSS, and Javascript, you can easily create applications for webOS!

Getting Started

To begin building apps for webOS, you will need to download and install the Palm webOS Software Development Kit. The webOS SDK is available for OS X, Linux, and Windows. You will need to follow the latest instructions from Palm to setup your webOS development environment.

After you have the SDK installed, you can use any text-editor or IDE to create your source code files. There is a webOS development plugin available for Eclipse, but I personally prefer to use ActiveState’s excellent Komodo Edit with a 3rd party plugin for webOS development. In this tutorial, we are going to use the command-line method.

Creating Your First webOS Application

To get started with a hello world app, type the following from your command line / terminal:

palm-generate helloWorld

This command will create a directory named “helloWorld” that will include the necessary directory structure for our app.

Let’s have a look at that structure for a moment:

  • app: Contains the assistants and views of your application. More on these later.
  • images: Contains images used in your application.
  • stylesheets: Contains the css-stylesheet for your application.
  • appinfo.json: Contains application information.
  • framework_config.json: Contains framework configuration information.
  • index.html: Main/start file.
  • sources.json: Contains a list of source files for each scene
  • icon.png: The icon that represents your application in the launcher and app catalog

Now let’s take a closer look at appinfo.json. Open the file in your favorite text editor:

{
  "id": "com.yourdomain.helloworld",
  "version": "1.0.0",
  "vendor": "My Company",
  "type": "web",
  "main": "index.html",
  "title": "helloWorld",
  "icon": "icon.png"
}

The most important parameters in this file are:

  • “id”: This is your application’s id and has to be unique across all available applications. It’s advised to use your own domain name (if you own one).
  • “vendor”: Use your company name (if you have one) or your own name.

Next take a look at index.html. It’s a standard HTML file and sets the stage for your application, doing things like loading the mojo framework and the main stylesheet.

<!DOCTYPE html>
<html>
<head>
    <title>Hello World!</title>
    <!-- Initialize the Mojo Framework -->
    <script src="/usr/palm/frameworks/mojo/mojo.js" type="text/javascript" x-mojo-version="1"></script>
    <!-- Apply our main stylesheet -->
    <link href="/stylesheets/helloworld.css" media="screen" rel="stylesheet" type="text/css">
</head>
</html>

To test our application we are going to use the webOS Emulator on an x86 build of webOS running with VirtualBox. Not all hardware features are supported on the emulator. The unavailability of both the device camera and screen multitouch capability are examples of simulator limitations, but special commands can help us overcome these limitations. For example, it is possible to issue a command to simulate an incoming call or text message.

The following operating system commands will start the webOS emulator:

  • Linux: Type “palm-emulator” in the command prompt
  • Mac OS X: In the Applications folder, double-click the Palm Emulator icon
  • Windows: Start > All Programs > Palm > SDK > Palm Emulator

Go ahead and run an instance of the emulator. Play around with the functionality a bit.

In order to package our application so it can be run on webOS, type the following in the command line from the directory that holds the “helloWorld” folder:

palm-package helloWorld

The result is our installable package file: com.yourdomain.helloworld_1.0.0_all.ipk. Note that the filename contains the id and version from the appinfo.json file.

Now, install the application with the following command:

palm-install com.yourdomain.helloworld_1.0.0_all.ipk

Launch the application in the emulator:

webOS SDK Tutorial

Extend the Application

We now have our first application up an runnning, but it’s not very exciting. Let’s add some more content.

To fill our stage with life we go ahead and create the first scene. Scenes are the same as different pages on a website. Scenes can be pushed onto the stage, popped off the stage, or swapped with another scene.

palm-generate -t new_scene -p "name=first" helloWorld

This creates two files: the view in app/views/first/first-scene.html and the assistant for the scene at app/assistants/first-assistant.js.

Open first-scene.html and edit it to contain the following:

<div id="main" class="palm-hasheader">
    <div class="palm-header">First Scene</div>
    <div class="palm-text">Welcome to my World</div>
</div>

Notice the classnames that start with “palm-”; these styles are included in the Mojo Framework, but can be expanded or changed in your own stylesheet.

Now we edit stage-assistant.js to display our new scene when the application starts:

function StageAssistant() {
    /* this is the creator function for your stage assistant object */
}
StageAssistant.prototype.setup = function() {
    /* this function is for setup tasks that have to happen when the stage is first created */
    Mojo.Controller.stageController.pushScene("first");
};

package the application and reinstall it. Here is the result:

webOS SDK Tutorial

Let’s add one more scene and call that when the user presses a button on the first scene:

palm-generate -t new_scene -p "name=second" helloWorld

Now we need to add a button to the first scene. Change first-scene.html to the following:

<div id="main" class="palm-hasheader">
    <div class="palm-header">First Scene</div>
    <div class="palm-text">Welcome to my World</div>
    <div x-mojo-element="Button" id="button1"></div>
</div>

To actually see that button, we need to setup the button widget in the first scene’s assistant setup code. Let’s edit first-assistant.js to do that:

FirstAssistant.prototype.setup = function() {
    /* this function is for setup tasks that have to happen when the scene is first created */
    /* use Mojo.View.render to render view templates and add them to the scene, if needed. */
    /* setup widgets here */
    /* add event handlers to listen to events from widgets */
    this.controller.setupWidget("button1",
        this.attributes = {},
        this.model = {
            label : "OK",
            disabled: false
        }
    );
    this.handleButton1=this.handleButtonPress1.bindAsEventListener(this);
    Mojo.Event.listen(this.controller.get("button1"), Mojo.Event.tap, this.handleButton1);
}

Note that we reference the button through its id: “button1″. We also added functionality to actually do something when we press the button. For that, we first create an event handler, this.handleButton1. We’ll create the necessary function handleButtonPress1 next. The next line, Mojo.Event.listen, sets the button up to listen for taps and call our event handler if a tap is received.

Let’s create our event handler that handles button presses. Our event handler just pushes our second scene to the stage:

FirstAssistant.prototype.handleButtonPress1 = function(event){
    Mojo.Controller.stageController.pushScene("second");
}

At last, we edit second-scene.html to contain something useful:

<div id="main" class="palm-hasheader">
    <div class="palm-header">Second Scene</div>
    <div class="palm-text">This is the second scene</div>
</div>

Ok, now let’s package and reinstall. This should be the final result:

webOS SDK Tutorial

Wrap Up

You’ve just been given a whirlwind tour of developing applications for webOS. In the next part of this series, we are going to build an app to display and read the latest articles from the tuts+ network. Leave comments and feedback below to let us know what you think of this series!

Series NavigationIntroduction to webOS SDK Development: Part 2»

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://mobilecoder.wordpress.com Error 454

    Great tutorial! Spread that WebOS love!

  • William Babb

    This is very well presented. As an old programmer from the days of PL/1 and Fortran IV on OS/370; I’m impressed. That said, however, I do find something missing. As I said, my experience, though long and deep, is very much out of date and some of the terms used are rather strange to me. It would be helpful if there were a sort of “Modern language terms glossary 101″ available, perhaps as a side note link that one could save or print for reference.

    Overall though, I commend you for your efforts and am encouraged to try to work the example and learn something new.

    Thanks very much.

    Bill Babb
    IBM Retiree
    Twitter & Facebook: Oldbill70

  • Subrat

    This is a nice tutorial for beginner..

  • http://ozeta.org Philip

    Thanks so much for this incredible tutorial, i hope there will be more parts. I felt like an idiot using Ares, this feels like i could make a lot more comprehensive applications.

  • http://wedesignapps.com/forum Christopher Chance

    First to comment !!!
    I really love how Mobiletuts is shaping up. Excellent content and unique tutorials. Envato should start an app store review site next.

  • Musk

    Thanks! This tutorial is very timely for me. Your layout makes it clear what to do and I like that it goes into a little more detail than some other basic webOS tutorials by adding the second scene. I especially like your code examples with syntax highlighting. I also like that you use the command line for the tutorial, as it helps clarify exactly what is happening.

    That said, I found that once I had an understanding of what each command does, it really helped to work in a development environment like Eclipse. Once I went beyond the tutorial and tried experimenting on my own, it became frustrating trying to juggle multiple files in a text editor and figure out what I was missing when something didn’t work (a frequent occurrence). The Eclipse environment lets you see your app’s directory structure visually and work with the files in a single view. Since JavaScript and web programming are new to me, the clearer view of my files was a big help.

    Palm has a great plugin for Eclipse and a tutorial on how to install it here: http://bit.ly/Or89j. I’m sure you know all this but perhaps it’ll help some other fledgling developers out there.

  • b41

    Thank you.

  • KRISHNA

    Very well put together tutorial. Very efective and informative at the same time. Thanks a lot for the effort and looking forward to the next tutorial in the series.

  • http://www.jorenrapini.com Joren Rapini

    Awesome tutorial, I hope it helps put webOS on the map some more, competition is good!

  • Crandel

    Excellent introduction. Thank you.

  • http://newbiestechtutorials.blogspot.com/ Jamila

    Awesome tutorial , am new to the mobile world but reading your tuts is the best way to start!
    Thanks a lot!

  • http://twelveblackcodemonkeys.com Levi Wallach

    Thank you for all the work in putting these together. There were a couple of things missing from this tutorial that I’d like to mention. One is that I don’t see a mention of actually creating the second scene, just editing it. Second, it would be nice, given that this is geared toward relative newbies, if you went into a bit more debth about how the event was linked to the event handler and that in turn to the function it executes. I got the idea, but it was probably because I’ve seen these in C# and with working similar WebOS tutorials in the past. Then again, you might explain this stuff more deeply in the coming tutorials.

    • Markus Leutwyler
      Author

      Thanks for your feedback, Levi

      The second scene is actually created, look at that part of the tutorial:

      Let’s add one more scene and call that when the user presses a button on the first scene:
      palm-generate -t new_scene -p “name=second” helloWorld

      Good point about the events, i did not explain that anywhere.

  • https://launchpad.net/~martinhacks Martin Hacks

    Hey like your tutorial, but for some reason it won’t work for me. It looks like there is a mistake in the code (I cannot however tell where) or maybe a version mismatch (I used the 1.4.5 SDK on Ubuntu Linux). This is weird because I even copy/pasted your code and it still wouldn’t work. This is the code I used for the first-assistant.js:

    FirstAssistant.prototype.setup = function() {

    this.model = {
    buttonLabel : “Button”
    };
    this.controller.setupWidget(‘button1′, {}, this.model);

    //add a listener
    this.controller.listen(‘button1′, Mojo.Event.tap, this.tapped.bind(this));
    };

    FirstAssistant.prototype.tapped = function(event) {
    Mojo.Log.info(“button tapped”);
    Mojo.Controller.stageController.pushScene(“second”);
    }

    Fetched this code from the palm developer homepage and it compiled and ran.

    Cheers,
    Martin Hacks

  • MAMISHO

    hola escribo desde España y me gracias por el tuto, pero tengo una duda no puedo pasar de la escena 1 a la escena2 tengo este codigo.

    function MainAssistant(argFromPusher) {
    }
    var nombre=” “;
    var direccion=” “;
    var numero;
    var piso;
    var letra=” “;
    var email=” “;
    MainAssistant.prototype = {
    setup: function() {
    Ares.setupSceneAssistant(this);
    this.controller.setupWidget
    (“button3″,
    this.attributes = {},
    this.model =
    {
    label : “OK”,
    disabled: false
    }
    );

    this.handleButton1=this.handleButtonPress1bindAsEventListener(this);
    Mojo.Event.listen(this.controller.get(“button3″), Mojo.Event.tap, this.handleButton1);
    },
    cleanup: function() {
    Ares.cleanupSceneAssistant(this);
    },
    button1Tap: function(inSender, event) {
    //this.controller.get (‘textField1′). mojo.setValue (“”);
    //this.controller.get (‘textField1′).focus();
    //event.stopPropagation(event);
    this.controller.get(‘textField1′).mojo.getValue(nombre);
    this.controller.get(‘textField2′).mojo.getValue(direccion);
    this.controller.get(‘textField3′).mojo.getValue(letra);
    this.controller.get(‘textField4′).mojo.getValue(email);
    // this.controller.get(‘integerPicker1′).mojo.getValue(numero);
    Mojo.Controller.getAppController().showBanner(“Guardado el numero”,
    {source: ‘notification’});
    //this.controller.get(‘integerPicker2′).mojo.getValue(piso);

    },
    button2Tap: function(inSender, event) {
    this.controller.get(‘textField1′).mojo.setValue(” “);
    this.controller.get(‘textField2′).mojo.setValue(” “);
    this.controller.get(‘textField3′).mojo.setValue(” “);
    this.controller.get(‘textField4′).mojo.setValue(” “);
    }
    /*
    button3Tap: function(inSender, event) {
    Mojo.Controller.getAppController().showBanner(“Activada la scene 1″,
    {source: ‘notification’});

    },*/

    };
    MainAssistant.prototype.handleButtonPress1 = function(event){
    Mojo.Controller.stageController.pushScene(“scene1″);
    };

    soy un novato en esto y no se que ocurre porque no me pasa de escena.
    estoy programando en el entorno ARES.

  • http://www.engiguide ajay

    where to put this code

    FirstAssistant.prototype.handleButtonPress1 = function(event){
    Mojo.Controller.stageController.pushScene(“second”);
    }

    in first-assistant.j or second-assistant.js ,

    i can see the first screen but not able to nevigate to second screen … :(
    please put source code if possible

  • Ridwan Shafi

    I downloaded VirtualBox(v 3.2) and SDK from develper.palm.com. I followed every step and copy-pasted the code but still I don’t get the stage-assistant page ,plz plz help

  • vishnu

    its quite old stuff. what about Enyo.

  • Anthony

    Excellent tuts. Just one thing, similar problem to Ridwan Shafi above, these tuts seem to be for the older versions of palm sdk for eg 1.4.5 which I have switched back to, as after trying the newest (hp sdk 3.05) I found that stage-assistant is not generated and therefor the instructions dont work.

    I’m sure this is probably a minor thing but, as with many programming probs, its only minor when you know about it !

    So, any amended instructions with regard to the latest hp sdk 3.05 would be more than welcome.

    Thanks.

  • Anthony

    Correction to my comment above as it seems there is a difference in commands from the older palm sdk versions to the latest hp sdk version. ‘palm-generate’ without arguments will just supply a basic ‘ENYO’ single pane project with a slightly different directory structure, whereas ‘palm-generate -t new_app’ + folder name will give you a new MOJO app with the folder structure as appears in Eclipse, which is probably more familiar.

    So the above tutorial is actually usable with the latest HP SDK.

    Running ‘palm-generate -l’ will give you a short list of the various options, of which the above MOJO one and possibly ‘palm-generate -t new_scene -p “name=first”‘ + folder name are the common ones .