1,007
edits
| Line 29: | Line 29: | ||
= Hello World: The First Command = | = Hello World: The First Command = | ||
== Just a | == Just a Message: As Simple as it Gets == | ||
Let's start with the standard programing trope: printing "Hello, World!" | 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 | CmdUtils.CreateCommand({ | ||
names: ["say hello"], | |||
} | execute: function() { | ||
displayMessage( "Hello, World!"); | |||
} | |||
}); | |||
</pre> | </pre> | ||
Now try Ubiq-ing "hello | 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. | ||
=== CmdUtils.CreateCommand === | |||
<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: | |||
<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. | |||
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 == | ||
edits