Can an AppleScript write to a Notes.app document?

I would like to write text to a "Note" (as opposed to a standard .txt or .rtf file), because Mac Notes automatically sync to an iCloud account, across devices.

(I understand that one can use various means to achieve the same effect with standard, i.e., non-proprietary, text documents, e.g., with a service like Dropbox. Notes.app is simply much more convenient.)

However, it appears that there is no way to write to a Note in AppleScript. I say this because, it doesn't seem that Notes are actually saved anywhere on the Mac as an .rtf (or similar) file. Per this question (but, note that the question was asked in 2013), I checked out the following directories:

  • ~/Library/Group Containers/group.com.apple.notes/

    • and
  • ~/Library/Containers/com.apple.Notes/Data/Library/Notes/

I found nothing that resembled a readable text document. I opened some of these files in TextEdit.app, and discovered that these files contain pure gobbledygook-text.

I'd imagine that I could effectively code an AppleScript to "write to a Notes document," by triggering mouse clicks and key presses. But, I am curious if writing to a Note can be done, in a "proper" fashion.


Solution 1:

Notes is scriptable, and the dictionary can be opened with Script Editor for details.

There's also a lengthy tutorial here.

Although I've not tested it with iCloud, you should be able to set the account, and create a note in that account.

Simplifying the example on that page, and using my Exchange account, I was able to create the note using this script:

tell application "Notes"
  tell account "Exchange"
    make new note at folder "Notes" with properties {name:"this is a test", body:"and more text"}
  end tell
end tell

To get the contents of an existing note, in order to modify it:

tell application "Notes"
  tell account "Exchange"
    tell folder "Notes"
      get body of note "this is a test"
    end tell
  end tell
end tell

And, to modify the content:

tell application "Notes"
  tell account "Exchange"
    tell folder "Notes"
      set body of note "this is a test" to "reset the body text"
    end tell
  end tell
end tell