Core:save-all and make-runner:run by pressing a key (f5)


#1

is there something like

atom.commands.add 'atom-text-editor', 'custom:save-all-and-run', ->
    command('core:save-all')
    command('make-runner:run')

for the “init.coffee”?


#2

There are packages that let you run something. Nothing in core though.


#3

which packages?


#4

Sorry, I don’t use any. Search in https://atom.io/packages or maybe someone else can help.


#5

It looks like you’re looking for the atom.commands.dispatch function:


#6

i tried this code:

atom.commands.add ‘atom-text-editor’, ‘custom:save-all-and-run’, ->
editor = @getModel()
atom.commands.dispatch(’.platform-linux atom-text-editor’, ‘core:save-all’)
atom.commands.dispatch(’.platform-linux atom-text-editor’, ‘make-runner:run’)

and got an
Uncaught TypeError: currentTarget.webkitMatchesSelector is not a function


#7

atom.commands.dispatch requires a DOM Node and not a selector string.
Also, it’s window:save-all, not core:save-all (there is core:save-as and core:save though)
Try:

atom.commands.add 'atom-text-editor', 'custom:save-all-and-run', ->
  editor = atom.views.getView(atom.workspace.getActiveTextEditor())
  return unless editor
  atom.commands.dispatch(editor, 'window:save-all')
  atom.commands.dispatch(editor, 'make-runner:run')

#8

yea, now it works
thanks


#9

@deprint
Hi, I am trying to write a custom keymap that saves, then runs the script via a package called atom-runner, I have tried the following code, but nothing happens. However when using the key binding resolver, the function is called successfully.

// in the init.coffee
atom.commands.add 'atom-text-editor', 'custom:save-then-run', ->
  editor = atom.views.getView(atom.workspace.getActiveTextEditor())
  return unless editor
  atom.commands.dispatch(editor, 'core:save')
  atom.commands.dispatch(editor, 'run:file')

// in the keymap.cson 
'.platform-linux atom-text-editor':
  'ctrl-b': 'custom:save-then-run'

#10

does the function work in the dev console? ctrl-shift-i -> Console


#11

Your code looks right to me. So it works when you have the Keybinding Resolver open? How do you get it to not work again?

It won’t because it’s CoffeeScript.


#12

When the keybinding resolver is open it does not work
When the keybining resolver is closed it does not work
But it’s says the function has run successfully. But actually nothing happens!


#13

@DamnedScholar @deprint
Hi guys,
I restarted my computer and:
When file is not saved:

  1. ‘core:save’ works the first time I hit ctrl-b
  2. however ‘run:file’ does not. When I click ctrl-b again, ‘run:file’ works

When file is already saved (I click ctrl-s ):

  1. ‘run:file’ works the first time I hit ctrl-b

I’m not sure, but I think atom executes ‘run:file’ first then ‘core:save’, so I always have to click ctrl-b twice!


#14

EDIT:
When file is not saved:

  1. ‘core:save’ works the first time I hit ctrl-b
  2. however ‘run:file’ executes on the old file (the last saved file). When I click ctrl-b again, ‘run:file’ works

So atom must be executing ‘run:file’ first then ‘core:save’


#15

Good investigation. Based on your experiences, I can give you the answer.

Close. What’s actually happening is that core:save makes a call to an asynchronous function when Atom writes to the file. This is a feature of Node and is in use because you don’t want the execution of the whole program to wait around for you to save one file. As soon as the async call goes out, Atom moves on to the next thing in its queue: the next line of your code. At that point, you can’t tell which one will finish first until you see the order in which they tell you that they’re done. It seems like run:file is a much lighter command than core:save, so it always finishes first.

What you can do is make use of the API to save the file, which will give you a Promise that you can use to tell Atom when to run your file. Like this:

atom.commands.add 'atom-text-editor', 'custom:save-then-run', ->
  editor = atom.views.getView(atom.workspace.getActiveTextEditor())
  return unless editor
  atom.workspace.getActiveTextEditor().save().then(
    atom.commands.dispatch(editor, "run:file")
  )

#16

@DamnedScholar
Hi, Tried the code, but still those scenarios are occurring!


#17

@DamnedScholar SOLVED IT THANK YOU

My knowledge on javascript is limited but I read up about promises on this page and modified your script a little bit! Thank you very much!

FINAL WORKING SCRIPT

// in the init.coffee
atom.commands.add 'atom-text-editor', 'custom:save-then-run', ->
  editor = atom.views.getView(atom.workspace.getActiveTextEditor())
  return unless editor
  atom.workspace.getActiveTextEditor().save().then(() =>
    atom.commands.dispatch(editor, "run:file")
  )


// in the keymap.cson 
'.platform-linux atom-text-editor':
  'ctrl-b': 'custom:save-then-run'

#18

Sorry, I forgot the arrow. In CS, -> will also work since it essentially translates to () =>.