How to trigger another action upon file save


#1

Hello there,

newbie question. The concrete idea is that each time a markdown file is saved, it gets automatically saved as pdf.

I’m using the markdown-pdf package from travs and would simply save the Ctrl-Shift-C action on top each save action.

Is there an easy way to pipe these two things together.
The initial part is covered in listen for a save event, there are still two generic blanks for me:

  • how to call the pdf action
  • how to activate this beauty (i.e. how to enable my code)

Caveat: Never wrote a plugin, theme, extension for atom so it would be great also to point me towards relevant tutorials or similar.

Thanx a bunch in advance!
–vzk


#2

Of course. However, you can’t use onDidSave(), since markdown-pdf doesn’t expose a way to convert a file based on its path. The simplest solution given Atom’s API is to just override the entire core:save keybinding with a command that does the normal save thing and also checks to see if the file should be converted.

atom.commands.add "atom-text-editor", "custom:save-and-convert-if-markdown", ->
  editor = atom.workspace.getActiveTextEditor()
  editor.save()

  if editor.getPath().endsWith("md")
    atom.commands.dispatch editor.element, "markdown-pdf:convert"