rdkn
rdkn

Reputation: 574

Programmatically access Apple Notes contents

Is it possible to programmatically access Apple Notes (that is the preinstalled app in macos and ios) contents

Upvotes: 10

Views: 6157

Answers (2)

Simon E.
Simon E.

Reputation: 58490

Apparently it's possible to use JavaScript for Automation or "JXA" (which is built on top of AppleScript) to do this.

I have not yet tested this code, but here's an example from the apple-notes-jxa library which uses the promise-based osa2 npm package:

var osa = require("osa2");
osa(function (name, folderId) {
    // Create an object for accessing Notes
    var Notes = Application("Notes");
    // Search inside a specific folder
    var folder = Notes.folders.byId(folderId);
    // Find a note by it's name
    var notes = folder.notes.where({
        name: name,
    });
    // Was it found?
    if (!notes.length) {
        throw new Error("Note " + name + " note found");
    }
    return {
        body: notes[0].body(),
        creationDate: notes[0].creationDate(),
        id: notes[0].id(),
        modificationDate: notes[0].modificationDate(),
        name: notes[0].name(),
    };
})(name, folderId).then(note => console.log(note));

Or you can use the apple-notes-jxa library itself like this:

const Notes = require('apple-notes-jxa');

Notes.accounts()
  .then(accounts => console.log(accounts));

Although be aware that JXA is not widely popular. Here are some reasons why.

Upvotes: 0

user7946504
user7946504

Reputation:

Notes on macOS is scriptable with AppleScript

To log out all the notes, open Script Editor and create a new script with the following, then hit the play button:

tell application "Notes"
  repeat with theNote in notes
    set theNoteName to name of theNote
    log theNoteName

    set theNoteBody to body of theNote
    log theNoteBody

    log "----"
  end repeat
end tell

Alternatively, create a text file with the contents above and save it as notes.scpt, then from Terminal run:

osascript notes.scpt

Upvotes: 15

Related Questions