Skip to content

Instantly share code, notes, and snippets.

@pqrth
Created April 15, 2018 00:03
Show Gist options
  • Save pqrth/2611f73ce03ffb9d67fcd595673a0ecf to your computer and use it in GitHub Desktop.
Save pqrth/2611f73ce03ffb9d67fcd595673a0ecf to your computer and use it in GitHub Desktop.
Exports macOS Notes' notes to a Markdown Directory which can be imported by Joplin
tell application "Notes"
set theMessages to every note
repeat with thisMessage in theMessages
# added identification of folder to create notebooks based on existing folder hierarchy
set myFolder to the container of thisMessage
set myFolder to the name of myFolder
set myTitle to the name of thisMessage
set myText to the body of thisMessage
set myCreateDate to the creation date of thisMessage
set myModDate to the modification date of thisMessage
set notebookPath to "basePath" & myFolder & "/"
do shell script "mkdir -p " & notebookPath
set myTitle to replace_chars(myTitle, "/", " ") of me as text
set myText to replace_chars(myText, "<div>", " ") of me as text
set myText to replace_chars(myText, "<div/>", " ") of me as text
set myText to replace_chars(myText, "<br>", "\n") of me as text
set mdFileName to myTitle & ".md"
set mdFilePath to notebookPath & mdFileName
set tFile to open for access mdFilePath with write permission
try
set eof of tFile to 0
write myTitle & "\n" & myText to tFile
close access tFile
on error
close access tFile
end try
set mdFilePath to quoted form of (notebookPath & mdFileName)
try
do shell script "pandoc " & mdFilePath & " --from html-native_divs-native_spans --to markdown -o " & mdFilePath
on error
set htmlFilePath to quoted form of (notebookPath & myTitle & ".html")
do shell script "mv " & mdFilePath & " " & htmlFilePath
end try
end repeat
end tell
# https://www.macosxautomation.com/applescript/sbrt/sbrt-06.html#1002
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
Copy link

ghost commented Oct 14, 2020

On Catalina this doesn't appear to work:

Notes got an error: Can’t make show id "x-coredata://7DD67AB2-771E-4213-9F6D-E3E75C083BF3/ICNote/p16686" into type specifier.

It appears to be caused by Catalina bugs with Notes:

https://talk.automators.fm/t/applescript-error-catalina/5880
https://hookproductivity.com/help2/integration/hook-and-macos-10-15-catalina-notes-app/


I then tried this in a Mojave VM. (iCloud Notes sync fine without serial number shenanigans, they just take a while to load in the VM.) It needed changing many things to fix getting most of the process working. (Such as changing some steps to an alternative shell script method, and also changing things like mdFilePath to quoted form of (POSIX path of mdFilePath).)

I didn't see how this code actually translated note creation and last edited dates into the exported files (either in the content or file properties), so I'm trying to hack together a process for Catalina to export macOS Notes to the full Joplin "RAW - Joplin Export Directory" format (which is a bunch of MD files with Joplin-specific strings at the bottom containing the creation and edited timestamps). If I succeed I will publish it on my github.

Copy link

ghost commented Oct 26, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment