2

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.

  • According to the Library in the Script editor, it seems Notes is completely scriptable. One of the properties: body (text) : the HTML content of the note . So it appears to be scriptable using HTML markup. Macosautomation has an older article, but the Notes app is still included in the Script Library as of macOS 10.12 – bjbk Aug 18 '17 at 2:12
  • 1
    Notes stores its data in an SQLite format 3 database and why it looked like gobbledygook-text in TextEdit.app when you opened the files at those locations. If you really want to look at the contents, then make a copy of the folder and open the .storedata file (the copy, not the original) with Liya. – user3439894 Aug 18 '17 at 3:19
6

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
  • 1
    I tested this with my iCloud account and it works just fine. Nice one, @Kent – bjbk Aug 18 '17 at 2:18
  • Do you know if it's possible to edit an already-existent note? Or, do you have to create a brand new note each time? – rubik's sphere Aug 18 '17 at 2:33
  • Yep. I modified the answer. You'll probably want to set a variable with the original text, and make modifications before setting the body to a new value; but, that's not difficult. – Kent Aug 18 '17 at 2:44
  • I can successfully get body of note, but every time that I try to set body of note, nothing happens (even when the replacement text is proper HTML). Does it work for you? – rubik's sphere Aug 18 '17 at 3:57
  • 1
    I'm stumped. I'm on Sierra 10.12.6 ; but, that shouldn't matter. The only difference is that you have the iCloud enabled and I don't. But, I don't see why changing "On My Mac" would cause Notes to spin out of control. – Kent Aug 18 '17 at 6:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .