Changes

Jump to: navigation, search

Labs/Ubiquity/Ubiquity Source Tip Author Tutorial

551 bytes added, 01:34, 22 June 2009
Just a Function: As Simple as it Gets
= Hello World: The First Command =
== Just a FunctionMessage: As Simple as it Gets ==
Let's start with the standard programing trope: printing "Hello, World!". In Ubiquity, commands are simply functions with various attributes tacked on. We'll start by manually making a function to define a command, but we'll quickly move to using a more elegant method.
In the command editor type the following:
<pre>
CmdUtils.CreateCommand({ names: ["say hello"], execute: function cmd_hello_world() { displayMessage( "Hello, World!"); }});
</pre>
Now try Ubiq-ing "say hello-world". You'll see that "Hello, World!" is immediately displayed on the screen. If you are on Mac OSX with [http://en.wikipedia.org/wiki/Growl_(software) Growl] installed the message will appear as a Growl notification. If you are on Windows, then it will appears as a standard "toaster" notification in the bottom right-hand corner of the screen.
http://img367.imageshack.us/img367/7051/picture1ui2.png
If you don't have Growl installed on OSX, or aren't on a Windows XP/Vista or Ubuntu Hardy, then you won't get any sort of notification. That's something to be [http://labs.toolness.com/trac/ticket/19 worked on] in future released of Ubiquity.
There's not much in this command, so let's dive straight in. Any function that starts with <code>cmd_</code> automatically becomes a Ubiquity command. It's a little bit of namespace magic that makes development super-simple=== CmdUtils.CreateCommand ===
There are other prefixes that have other effects, like running code on page load (<code>pageLoad_CmdUtils</code>), is a namespace which contains all the functions you need to create commands. Commands are created by making an object and startup (passing it to <code>startup_CmdUtils.CreateCommand</code>). In Javascript, inline curly braces mean "new object", but that's for a different tutorial.so this code:
Back to the example<pre> { names: ["say hello"], execute: function() { //etc } }</pre> means "Make an object with two attributes, 'names' and 'execute'. The meat of the command " This object is in then passed as the function argument to <code>displayMessage()CmdUtils.CreateCommand</code>. === names === 'names' and 'execute' are the only two mandatory attributes for your command object. 'names' specifies what the command is called, and 'execute' specifies what it does. There are plenty of other attributes that you can specify, but they are all optional. 'names' is always an array (thus the square brackets). In the case of this command we provided only one name, "hello world". But we could have provided as many names as we wanted. For instance, if we had said:  names: ["say hello", "greet"] then "say hello" would be the normal name of the command, which displays but Ubiquity would also recognize "greet" as a synonym or alias for the message in whichever way command. === execute === 'execute' is always a function. When the user executes your command, this is the operating system function that will be run. It cando pretty much anything you want -- or at least, anything you know how to write in JavaScript.
You may be wondering why there is a hyphen in In the nameexample above, instead of a space. That's because the Ubiquity natural language parser isn't yet smart enough to handle commands that are multiple words we simply call <icode>anddisplayMessage()</icode> arguments that are multiple words. It's something we'll be working on , which displays the given message in whichever way the futureoperating system can.
== Using CreateCommand ==
1,007
edits

Navigation menu