Last active
August 20, 2023 11:46
-
-
Save iqiancheng/37fcc5b80422fbf6f9289362e1aa8f02 to your computer and use it in GitHub Desktop.
Apple Notes Exporter 苹果电脑上便签导出脚本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess, os | |
class NotesExporter: | |
@staticmethod | |
def export_notes_to_md(output_directory): | |
applescript = """ | |
on clean_filename(filename) | |
set cleaned_filename to "" | |
repeat with c in filename | |
set targetUnicodeArray to {47, 92, 124, 63, 58, 42, 34, 60, 62} | |
set char_code to id of c | |
if not (char_code is in targetUnicodeArray) then | |
set cleaned_filename to cleaned_filename & c | |
else | |
set cleaned_filename to cleaned_filename & "_" | |
end if | |
end repeat | |
return cleaned_filename | |
end clean_filename | |
tell application "Notes" | |
set allFolders to every folder | |
repeat with aFolder in allFolders | |
set folderName to the name of aFolder | |
if folderName is not "Recently Deleted" then | |
set theNotes to every note of aFolder | |
repeat with aNote in theNotes | |
try | |
set noteTitle to the name of aNote | |
set noteTitle to my clean_filename(noteTitle) -- Clean the filename | |
set noteContent to the body of aNote | |
set mdContent to "# " & noteTitle & "\\n\\n" & noteContent | |
set createTimestamp to the creation date of aNote | |
set mdFileName to noteTitle & ".md" | |
-- set mdFileName to createTimestamp & ".md" | |
set outputPath to "%s" & mdFileName | |
do shell script "echo " & quoted form of mdContent & " > " & quoted form of outputPath | |
on error errMsg | |
-- Handle error here if needed | |
-- display dialog "An error occurred: " & errMsg | |
end try | |
end repeat | |
end if | |
end repeat | |
end tell | |
""" % output_directory | |
subprocess.run(["osascript", "-e", applescript]) | |
if __name__ == "__main__": | |
exporter = NotesExporter() | |
output_directory = "notes/applenotes/" # 修改为你想要保存Markdown文件的目录 | |
# create output directory if not exists | |
if not os.path.exists(output_directory): | |
os.makedirs(output_directory) | |
exporter.export_notes_to_md(output_directory) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess, os | |
class NotesExporter: | |
@staticmethod | |
def export_notes_to_md(output_directory): | |
applescript = """ | |
on clean_filename(filename) | |
set cleaned_filename to "" | |
repeat with c in filename | |
set targetUnicodeArray to {47, 92, 124, 63, 58, 42, 34, 60, 62} | |
set char_code to id of c | |
if (char_code >=48 and char_code <= 90) or (char_code >=97 and char_code <= 122) or char_code ≥ 255 then | |
set cleaned_filename to cleaned_filename & c | |
else | |
set cleaned_filename to cleaned_filename & "_" | |
end if | |
end repeat | |
return cleaned_filename | |
end clean_filename | |
tell application "Notes" | |
set allFolders to every folder | |
repeat with aFolder in allFolders | |
set folderName to the name of aFolder | |
if folderName is not "Recently Deleted" then | |
set theNotes to every note of aFolder | |
repeat with aNote in theNotes | |
try | |
set noteTitle to the name of aNote | |
set noteTitle to my clean_filename(noteTitle) -- Clean the filename | |
set noteContent to the body of aNote | |
set mdContent to "# " & noteTitle & "\\n\\n" & noteContent | |
set createTimestamp to the creation date of aNote | |
-- set mdFileName to noteTitle & ".md" | |
set mdFileName to createTimestamp & ".md" | |
set outputPath to "%s" & mdFileName | |
do shell script "echo " & quoted form of mdContent & " > " & quoted form of outputPath | |
on error errMsg | |
-- Handle error here if needed | |
-- display dialog "An error occurred: " & errMsg | |
end try | |
end repeat | |
end if | |
end repeat | |
end tell | |
""" % output_directory | |
subprocess.run(["osascript", "-e", applescript]) | |
if __name__ == "__main__": | |
exporter = NotesExporter() | |
# must be absolute path | |
output_directory = "macos_notes/" # 修改为你想要保存Markdown文件的目录 | |
# create output directory if not exists | |
if not os.path.exists(output_directory): | |
os.makedirs(output_directory) | |
exporter.export_notes_to_md(output_directory) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment