Developers can easily add new commands. Here is an example of a command called say hello
that displays Hello World!
message.
CmdUtils.CreateCommand({
names: ["say hello"],
execute: function() {
displayMessage( _("Hello, World!") );
}
});
The names
and execute
are the mandatory attributes of an command. Execute specifies behavior when the enter
is pressed. You can also specify preview
that is displayed and updated as the user types in the command. Most commands also takes parameters that are specified using arguments
attribute. Each argument is of specific Noun Type
and takes an Argument Role
.
CmdUtils.CreateCommand({
names: ["echo"],
arguments: [{role: "object",
nountype: noun_arb_text,
label: "your shout"}],
preview: function( pblock, arguments ) {
pblock.innerHTML = _("Will echo: ") + arguments.object.text;
},
execute: function( arguments ) {
var msg = arguments.object.text + "... " + arguments.object.text + "......";
displayMessage( msg );
}
});
The Noun Type
specifies the kind of input. It can be for example text, date, time, url, contact, geolocation etc. The complete list is specified in file nountypes.js.
The Argument Role
specifies semantic role of the argument so that arguments can be parsed from a natural language. The roles can be object, goal, source, location, time, etc. See the complete list of semantic roles.
Imagine an email command with two arguments: message
and person
. The user can type in the arguments in an arbitrary position:
By specifying the argument roles, the parser can correctly identify the arguments:
arguments: [{role: "object",
nountype: noun_arb_text,
label: "message"},
{role: "goal",
nountype: noun_type_contact,
label: "recipient"}]
See a detailed tutorial for more information how to develop commands.
Once you have Ubiquity installed, you can look at the API documentation in a form of an annotated source code. The API for specifying commands is available in file nountypes.js.