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 26, 2020

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