Last active
May 10, 2024 14:47
-
-
Save bespoyasov/5307959a18590b7e99c661b0eab7bc3d to your computer and use it in GitHub Desktop.
Export content from the Apple Notes app.
This file contains hidden or 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
/** | |
* Open Script Editor, change the current language to JavaScript, and paste the following code. | |
* You can also save the script as an app for faster access and run. | |
* | |
* By default, password-protected notes' content will be empty. | |
* For exporting protected notes as well, unlock them first. | |
* | |
* Based on: https://macmost.com/export-all-of-the-notes-on-your-mac-using-a-script.html, | |
* extends it with support for writing non-ASCII characters. | |
*/ | |
const editor = Application.currentApplication(); | |
editor.includeStandardAdditions = true; | |
const notesApp = Application("Notes"); | |
notesApp.includeStandardAdditions = true; | |
const notes = notesApp.notes; | |
const wantedNotes = editor.chooseFromList(notes.name(), { | |
withPrompt: "Which Notes?", | |
multipleSelectionsAllowed: true, | |
}); | |
if (wantedNotes) { | |
const destination = editor.chooseFolder().toString(); | |
if (destination) { | |
for (let i = 0; i < notes.length; i++) { | |
const note = notes[i]; | |
if (!wantedNotes.includes(note.name())) continue; | |
const content = createHtmlBody(note); | |
const escaped = escapeFileName(note); | |
const filename = `${destination}/${escaped}.html`; | |
str = $.NSString.alloc.initWithUTF8String(content); | |
str.writeToFileAtomicallyEncodingError( | |
filename, | |
true, | |
$.NSUTF8StringEncoding, | |
null | |
); | |
} | |
} | |
} | |
function createMetadata(note) { | |
return { | |
createdAt: note.creationDate(), | |
modifiedAt: note.modificationDate(), | |
}; | |
} | |
function escapeFileName(note) { | |
return note.name().replaceAll("/", " — ").replaceAll(":", " - "); | |
} | |
function createHtmlBody(note) { | |
const body = note.body(); | |
const metadata = createMetadata(note); | |
return `<html> | |
<head> | |
<meta charset="utf-8"> | |
</head> | |
<body> | |
${body} | |
<br> | |
<table> | |
${Object.entries(metadata) | |
.map(([key, value]) => `<tr><td>${key}</td><td>${value}</td></tr>`) | |
.join("")} | |
</table> | |
</body> | |
</html>`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment