Skip to content

javascript – Automator – Cyrillic symbols and file writing

  • by


I was quite surprised the fact that Apple Automator can make backups for my notes. So I wrote a small script for that. But there are two unsolved issues.

  1. In for each files are creating but there are empty
  2. The Cyrillic in files is writing like ?????
  3. Is it possible somehow to get folder name to structure it with files like in Apple Note (Optional)

Could you please help me with this?

var app = Application.currentApplication()
app.includeStandardAdditions = true
 
function writeTextToFile(text, file, overwriteExistingContent) {
    try {
        var fileString = file.toString()
        var openedFile = app.openForAccess(Path(fileString), { writePermission: true })
 
        if (overwriteExistingContent) {
            app.setEof(openedFile, { to: 0 })
        }
 
        app.write(text, { to: openedFile, startingAt: app.getEof(openedFile) })
 
        app.closeAccess(openedFile)

        return true
    }
    catch(error) {
        try {
            app.closeAccess(file)
        }
        catch(error) {
            console.log(`Couldn't close file: ${error}`)
        }
 
        return false
    }
}

var notesApp = Application('Notes');
notesApp.includeStandardAdditions = true;
var notes = notesApp.notes;

for(var i = 0; i < notes.length; i++) {
  try {
    var desktopString = app.pathTo("desktop").toString()
    var file = `${desktopString}/copy/${notes[i].name()}.txt`
    var text = notes[i].body();

    writeTextToFile(text, file, true)
  }
  catch(error) {
    var desktopString = app.pathTo("desktop").toString()
    var file = `${desktopString}/ERROR.log`
    writeTextToFile(error.message, file, true)
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *