MacOS – Is it possible to send a text file from terminal to the Notes App on MacOS

automationmacosnotes.appscriptterminal

I like how my notes in the Notes App gets synced via iCloud. I spend most of my time in the terminal during the day, so it's convenient for me to keep notes in a text file. At the moment I simply copy-paste my note to the Notes App, so I can review them in bed on my phone.

Can I skip manual copy-paste and somehow directly push my text file from the command line?

Best Answer

The following might do or at least get you started:

#!/usr/bin/osascript

on run argv
    set unixPath to item 1 of argv
    do shell script "sed 's/$/<br>/' '" & unixPath & "' > /tmp/tmp"
    set bodytext to read POSIX file "/tmp/tmp"
    do shell script "rm -f /tmp/tmp"

    tell application "Notes"
        tell account "iCloud"
            make new note at folder "Notes" with properties {name:unixPath, body:bodytext}
        end tell
    end tell
end run

Save in a convenient place, make it executable and pass the name of the file to be converted into a note as the first parameter.

PS: I didn't do a lot of testing on this, so handle with care. It has no error handling at all, so running it without an argument, or with the name of an non existing file will fail in interesting ways. Also, the HTML "conversion" is rather rudimentary, any leading spaces or tabs at the beginning of a line will be lost for instance. If a full HTML conversion is needed it might be easier to do this within a shell script first and only use Applescript for the note creation at the end.