My first "SaveAs" package


#1

i am writing my first Atom package. I want to save my current editing file to another location. What i have written basically works. i can save the file. What i don’t like is that Atom will close the original file and open the new saved file.

I want to open my project folder and do with the files what needs to be done and simply with one click copy/save current open file to another location. Even after doing that i want to have my original project folder and original files open in Atom.

Can anyone help me with this?

Thank you.

Code:

  DownloadToController() {
    let editor
    editor = atom.workspace.getActiveTextEditor()

    var remote = require('electron').remote;
    var files = remote.dialog.showOpenDialog(remote.getCurrentWindow(), {properties: ['openDirectory']});
    if(files && files.length) {
      try{
        editor.saveAs(files[0]+'\\'+editor.getTitle())
        atom.notifications.addSuccess('File saved to: '+ files[0] + '\\' + editor.getTitle())
        }
        catch(err){
          atom.notifications.addSuccess('Open a file you want to save')
        }
        return files[0];
    }
    atom.notifications.addSuccess('No folder selected')
    return null;
  }

Picture1: Before saving the file
Picture2: After saving the file


#2

I’m guessing that editor.saveAs() will always leave you with the file open at the new location. You should probably use the File API. It would look something like this:

file = new File(files[0]+'\\'+editor.getTitle())
file.write(editor.getText()).then( () => 
  atom.notifications.addSuccess('File saved to: '+ files[0] + '\\' + editor.getTitle())
)

#4

Thak you for the answer. I tried out your suggestion. But i could not make it work :frowning:

Picture


#5

Please share your code as it currently stands.


#6

This is how i did it

DownloadToController() {
let editor
editor = atom.workspace.getActiveTextEditor()

var remote = require('electron').remote;
var files = remote.dialog.showOpenDialog(remote.getCurrentWindow(), {properties: ['openDirectory']});
if(files && files.length) {
    file = new File(files[0]+'\\'+editor.getTitle())
    file.write(editor.getText()).then( () =>
    atom.notifications.addSuccess('File saved to: '+ files[0] + '\\' + editor.getTitle()))
    return files[0];
}
atom.notifications.addSuccess('No folder selected')
return null;

}


#7

I apologize, I didn’t explain fully enough. The File class has to be imported. This init.coffee script works:

{File} = require 'atom'

atom.commands.add 'atom-text-editor', 'custom:fake-file', ->
  target = new File(atom.project.getPaths()[0] + "\\text.txt")
  target.write("Oh no.").then( () -> console.log "Success.")

#8

I was sick and could not answer.
Sorry for the late answer, but big thx.

its working :slight_smile: