Changes

Jump to: navigation, search

Labs/Ubiquity/Ubiquity 0.1 Author Tutorial

1,927 bytes removed, 22:44, 10 September 2008
Specifying Defaults for your Arguments
The translate command is a good place to learn more about modifiers and the <code>noun_type_language</code>.
== Specifying Defaults for your Arguments ==
Having a sensible default for an argument can make a command much easier to use, because if the default is what the user wants, then the user doesn't have to type anything extra.
 
If a command specifies a default value for one of its arguments, that default value will be suggested whenever the user doesn't provide a value for the argument. A default value can be provided for a direct object like so:
 
<pre>
CmdUtils.CreateCommand({
name: "echo",
takes: {"your shout": noun_arb_text},
DODefault: "Hallooooooo!",
// etc. etc. etc...
});
</pre>
 
And for a modifier like so:
 
<pre>
CmdUtils.CreateCommand({
name: "replace",
takes: {"what": noun_arb_text},
modifiers: {with: noun_arb_text, in: noun_arb_text},
modifierDefaults: {with: "cheese"}
// etc. etc. etc...
});
</pre>
 
Aside from giving defaults to individual command arguments, you can also give a custom nounType a default() method which returns an argument suggestion. For example, this default() method of the URL nounType suggests the URL of the currently loaded page:
 
<pre>
var noun_type_url = {
_name: "url",
suggest: function(fragment) {
//etc etc etc...
},
default: function() {
var url = Application.activeWindow.activeTab.document.URL;
return CmdUtils.makeSugg(url);
}
}
</pre>
 
(Notice it returns a single suggestion, not a list. Currently only one default can be provided. Support for multiple default suggestions is planned for later inclusion.)
 
Arguments actually provided by the user are always used if available. If an argument is not provided, the default defined by the command argument will be used. If no default is defined by the command argument, only then will the default provided by the nounType be used. Finally, if no argument is provided by the user and no default is defined by either the command or the nounType, the argument will be left blank.
= Twitter: Putting It All Together =
1,007
edits

Navigation menu