Execute command from CoffeeScript


#1

I want to bind multiple commands to one keystroke.

I understand that I need to edit my init.coffee file, but I have no idea how to find the CoffeeScript equivalent of the commands I want to execute.

It would be nice if there were a helper so I could just

executeCommand("git-plus:add-and-commit")

or something similar.


#2

I discovered atom.commands.dispatch!

This gets me 90% of the way there. I just need to figure out how to pass the current ActiveTextEditor as the target


#3

Yep, atom.commands.dispatch is what you want. You can get the current editor with atom.workspace.getActiveTextEditor().


#4

It seems maybe the current editor is not what I should be passing as target in this case, as that just throws “undefined is not a function

Seems to work if I pass this as target, which feels all sorts of dirty and wrong, but gets the job done.


#5

Here is what I am working with:

atom.commands.add 'atom-text-editor', 'custom:save-and-commit', ->
  editor = atom.workspace.getActiveTextEditor()
  editor.save()
  atom.commands.dispatch(this, 'git-plus:add-all-and-commit')

#6

Argh, accidentally edited my post.

atom.commands.add 'atom-text-editor', 'custom:save-and-commit', ->
  atom.workspace.getActiveTextEditor()?.save()
  workspace = atom.views.getView(atom.workspace)
  atom.commands.dispatch(workspace, 'git-plus:add-all-and-commit')

I think you should be able to use the text editor too though. You probably need to also use the view for that. So you could use atom.views.getView(atom.workspace.getActiveTextEditor()) as the target.


#7

THANK YOU SO MUCH! :+1: