Labs/Ubiquity/Ubiquity Source Tip Author Tutorial: Difference between revisions

Jump to navigation Jump to search
Line 29: Line 29:
= Hello World: The First Command =
= Hello World: The First Command =


== Just a Function: As Simple as it Gets ==
== Just a Message: 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.
Let's start with the standard programing trope: printing "Hello, World!".


In the command editor type the following:
In the command editor type the following:


<pre>
<pre>
function cmd_hello_world() {
CmdUtils.CreateCommand({
  displayMessage( "Hello, World!");
  names: ["say hello"],
}
  execute: function() {
    displayMessage( "Hello, World!");
  }
});
</pre>
</pre>


Now try Ubiq-ing "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.
Now try Ubiq-ing "say hello". 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
http://img367.imageshack.us/img367/7051/picture1ui2.png
Line 53: Line 56:
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.
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_</code>), and startup (<code>startup_</code>), but that's for a different tutorial.
<code>CmdUtils</code> is a namespace which contains all the functions you need to create commands.  Commands are created by making an object and passing it to <code>CmdUtils.CreateCommand</code>.  In Javascript, inline curly braces mean "new object", so this code:


Back to the example. The meat of the command is in the function <code>displayMessage()</code>, which displays the message in whichever way the operating system can.
<pre>
{
  names: ["say hello"],
  execute: function() { //etc }
}
</pre>
 
means "Make an object with two attributes, 'names' and 'execute'."  This object is then passed as the argument to <code>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, but Ubiquity would also recognize "greet" as a synonym or alias for the command.
 
=== execute ===
 
'execute' is always a function.  When the user executes your command, this is the function that will be run.  It can do 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 the name, instead of a space. That's because the Ubiquity natural language parser isn't yet smart enough to handle commands that are multiple words <i>and</i> arguments that are multiple words. It's something we'll be working on in the future.
In the example above, we simply call <code>displayMessage()</code>, which displays the given message in whichever way the operating system can.


== Using CreateCommand ==
== Using CreateCommand ==
1,007

edits

Navigation menu