NotesExporter.workflow

    1. set exportFolder to (choose folder) as string
    2. -- Simple text replacing
    3. on replaceText(find, replace, subject)
    4. set prevTIDs to text item delimiters of AppleScript
    5. set text item delimiters of AppleScript to find
    6. set subject to text items of subject
    7. set text item delimiters of AppleScript to replace
    8. set subject to "" & subject
    9. set text item delimiters of AppleScript to prevTIDs
    10. return subject
    11. end replaceText
    12. -- Get an HTML file to save the note in. We have to escape
    13. -- the colons or AppleScript gets upset.
    14. on noteNameToFilePath(noteName, tag)
    15. global exportFolder
    16. set strLength to the length of noteName
    17. if strLength > 250 then
    18. set noteName to text 1 thru 250 of noteName
    19. end if
    20. tell application "Finder"
    21. set convertedFolderPath to exportFolder & tag & ":"
    22. if (exists (folder convertedFolderPath)) then
    23. set outputFolder to convertedFolderPath
    24. else
    25. make new folder at exportFolder with properties {name:tag}
    26. set outputFolder to the result as string
    27. end if
    28. end tell
    29. set fileName to (outputFolder & replaceText(":", "_", noteName) & ".html")
    30. return fileName
    31. end noteNameToFilePath
    32. tell application "Notes"
    33. repeat with theNote in notes of default account
    34. --repeat with theNote in notes in folder "New Folder" of default account
    35. set noteLocked to password protected of theNote as boolean
    36. set modDate to modification date of theNote as date
    37. set creDate to creation date of theNote as date
    38. set noteID to id of theNote as string
    39. set oldDelimiters to AppleScript's text item delimiters
    40. set AppleScript's text item delimiters to "/"
    41. set theArray to every text item of noteID
    42. set AppleScript's text item delimiters to oldDelimiters
    43. if length of theArray > 4 then
    44. -- the last part of the string should contain the ID
    45. -- e.g. x-coredata://39376962-AA58-4676-9F0E-6376C665FDB6/ICNote/p599
    46. set noteID to item 5 of theArray
    47. else
    48. set noteID to ""
    49. end if
    50. if not noteLocked then
    51. -- file name composed by id and note title to overcome overwriting files
    52. set fileName to (name of theNote as string) as string
    53. set theContainer to container of theNote
    54. -- export the folder containing the notes as tag in bear
    55. -- the try catch overcome a 10.15.7 bug with some folders
    56. try
    57. if theContainer is not missing value then
    58. set tag to name of theContainer
    59. set theText to ("" & theText & "
    60. #" & tag & "#") as string
    61. end if
    62. end try
    63. set filepath to noteNameToFilePath(fileName, tag) of me
    64. set noteFile to open for access filepath with write permission
    65. set theText to body of theNote as string
    66. write theText to noteFile as Unicode text
    67. close access noteFile
    68. tell application "Finder"
    69. set modification date of file (filepath) to modDate
    70. end tell
    71. end if
    72. end repeat
    73. end tell

    解决乱码问题:

    1. find . -name \*.html -type f | \
    2. (while read file; do
    3. iconv -f UTF-16 -t UTF-8 "$file" > "${file}.bak";
    4. mv "${file}.bak" "$file"
    5. sed -i "" 's#<h1><br></h1>##g' $file
    6. done);