Changes

Jump to: navigation, search

Labs/Ubiquity/Ubiquity 0.1 Author Tutorial

1,727 bytes added, 22:51, 9 September 2008
documentation for how to set defaults on command arguments and nountypes
</pre>
Caveat: There is a [http://labs.toolness.com/trac/ticket/31 known issue] where the parser does not recognize modifiers that are longer than (In earlier prototypes, modifier arguments could only accept a single -word. For instancevalue, "yelp near sf" will be parsed correctly, whereas "yelp near san francisco" will not. We'll be working to fix but this in later versions of Ubiquityhas now been fixed.)
The modifiers argument takes a dictionary, where the key is the name of the argument and the value is the noun-type of the argument. In later releases we may include further options, like the ability to specify an argument as required/optional, etc.
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