Skip to content

Overriding Default Commands

Ben Meyer edited this page Apr 13, 2015 · 21 revisions

Place your command overriding code in the file:

~/voicecode/commando/user/commands.coffee

Or if that file is getting too busy, create another file in the same directory, maybe named: 'overrides.coffee'

Changing A Command

Sometimes you want to change the default commands to do something your own way. A good example would be for German keyboard-layout users, since that layout switches the 'Z' and 'Y' keys. If you wanted to change the 'dizzle' command to press command-Y instead of command-Z you could do the following:

CoffeeScript

Commands.override "dizzle", (input, original)  ->
  @key "Y", ['command']

JavaScript

Commands.override("dizzle", function(input, original) {
  this.key("Y", ['command']);
});

Wrapping A Command

Other times you want to keep the default action, but "wrap" your own code around the defaults. To do this you could do something like:

CoffeeScript

Commands.override "snake", (input, original)  ->
  console.log "starting the snake command"
  original.call(@)
  console.log "finished the snake command"

JavaScript

Commands.override("snake", function(input, original) {
  console.log("starting the snake command");
  original.call(this);
  console.log("finished the snake command");
});

Adding your own logic

Sometimes you want to extend an existing command. You could completely over-write the command's 'action' function, but then if the underlying command changed in a future update, that update would not trickle into your extended version. Instead, you can override the command and call the 'original' command from within your overridden function. For example, imagine if whenever I used the "shackle" command to select an entire line of text, I also wanted to perform a "copy" keystroke afterwards, but only if I am in the Mail application. I could do this:

Commands.override "shackle", (input, original)  ->
  original.call(@)
  if @currentApplication() is "Mail"
    @key "C", ['command']

You can get as fancy as you want with these overrides so that VoiceCode gets tailored to your specific workflows and use-cases.

Changing a command's name

Occasionally you may have some reason to change the name of a built-in command. To achieve this, simply do:

Commands.changeName "old name", "new name"

Clone this wiki locally