Export All Of The Notes On Your Mac Using a Script

In Mac Notes you can only export one note at a time as a PDF file. With the help of a script, you can quickly and easily export all of your notes to html files as a backup or archive. You can also save notes this way to clear out your Notes app.



Here is the code:

// set things up
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var notesApp = Application('Notes');
notesApp.includeStandardAdditions = true;

// choose which notes
var notes = notesApp.notes;
var whichNotes = app.chooseFromList(notes.name(), { withPrompt: "Which Notes?", multipleSelectionsAllowed: true });


if (whichNotes) {

	// choose save location
	var saveWhere = app.chooseFolder().toString();
	
	if (saveWhere) {
	
		// loop through all notes
		for(var i=0; i<notes.length; i++) {
		
			// is this note one to be exported?
			if (whichNotes.indexOf(notes[i].name()) > -1) {
			
				// save file as html
				var filename = saveWhere+"/"+notes[i].name()+".html";
				var file = app.openForAccess(Path(filename), { writePermission: true });
				app.setEof(file, { to: 0 });
				app.write(notes[i].body(), {to: file});
				app.closeAccess(file);
			}
		}
	}
}

Note: If you have more than one Notes account, or use “On My Mac” notes in addition to iCloud notes, then this could fail because the .notes property gives an array of objects, not a single object. You can substitute this line for the “var notes =” line to narrow down by account.

var notes = notesApp.accounts.byName('iCloud').notes;

Then just change the ‘iCloud’ to ‘On My Mac” to get those notes instead.

Comments: 37 Responses to “Export All Of The Notes On Your Mac Using a Script”

    Eric
    7 months ago

    Great tutorial Gary, thank you. Question regarding locked and/or pinned notes. How does this script affect those if at all?

    7 months ago

    Eric: I don’t think it will work with locked notes, but pinned notes are just regular notes that appear at the top. But try it.

    Eric
    7 months ago

    I’ll give it a try and post results. Side question; any particular way to export just the titles of the notes and not the body of them? I’m possibly looking to take an “inventory” of what notes I have. I was able to Cmd+A to select all titles, but the function is not being allowed. Thoughts?

    7 months ago

    Eric: A note title is just the first line of the note. So use the same script but modify it to only add the first line of each note’s text.

    Caroline
    7 months ago

    Oooh, just tried it, and got this message when I ran it: Error -10000: AppleEvent handler failed. What have I done wrong?🤔

    7 months ago

    Caroline: Not sure. Are you using the latest versions? Do you have any special characters in the Note titles? Does it show you which line has the error (run it in Script Editor).

    Caroline
    7 months ago

    Gary: thank you very much for replying so fast. No luck so far. Latest version of everything. I’ve been through every single one of my notes and removed all punctuation and special characters from the titles. Numerals are ok, aren’t they? Have run it again in ScriptEditor and still getting the same message: Error -10000: AppleEvent handler failed.
    Oh well, the notes will be on TimeMachine & Backblaze so not disaster I guess!

    7 months ago

    Caroline: When you run it in Script Editor, does it show you which line has the error?

    Caroline
    7 months ago

    Gary: I don’t think so! In ‘Result’, all it shows is ‘Error -10000: AppleEvent handler failed’ and in the bottom of the window it says Error: Error: AppleEvent handler failed.
    Should I be looking somewhere else in the window? As you can see, I’m a Script Newbie!

    another Eric
    7 months ago

    Hi Gary. I’ve just tried running the script and get the same error as Caroline. There is no indication given as to which line is in error.

    another Eric
    7 months ago

    Just tried again with the Notes application running in the background and now get this …

    app = Application(“Notes”)
    app.notes.name()
    –> Error -10000: AppleEvent handler failed.
    Result:

    Paul
    7 months ago

    I ran the script twice with the same results. The script exported 140 notes to html files out of 869 notes. After 140, this is the message: “Error -43: File wasn’t found.”

    7 months ago

    another Eric: Did you read my note above the comments?

    7 months ago

    Paul: That happens when you have a note that uses a character in the title that can’t be used in a filename.

    Robert
    7 months ago

    Hi Gary! I hope my questionis related to the topic!
    Can I restore all my Notes from Time Machine backup?
    Thanks in advance!

    Robert
    7 months ago

    The 1000 error occurs on the assignment stmt (var notes = notesApp.notes). You can correct by using the more explicit assignment stmt Gary lists above (var notes = notesApp.accounts.byName(‘iCloud’).notes).

    I’d suggest running with the log window displayed (and clicking the “Replies” tab) as this script takes a while to run (I had > 400 notes). I assume this is the cost of IPC traffic to the Notes app.

    Also, you have to be careful on note name to avoid Error 43 File wasn’t found. HTH

    7 months ago

    Robert: Not really. If you needed to do a full restore then the notes would come along with, assuming you weren’t using iCloud and just On My Mac notes. But you can’t grab individual notes or anything, at least not easily.

    another eric
    7 months ago

    Hi Gary. I have just revisited this page; read your comment; and tried again with the suggested change. That works. Thanks.

    Robert E.
    7 months ago

    Hi Gary!
    What if I copied “NoteStore.sqlite” file from library and kept it somewhere for safe keeping?
    In a disaster,could I just take the copy and replace it in library?

    7 months ago

    Robert: I’m not sure that just replacing that file would work, especially if these are cloud-stored notes. Exporting with a script like this may be a better option if you are afraid you may accidentally delete one or something. Or, if the information is that critical, then maybe store them in a vault like using 1Password or something.

    Dvir
    6 months ago

    Hi Gary!
    Thank you very much for this great post.
    I’m familiar with programing but not with javascript, if I want that the date of creation would be added to the exported note, where and how should I add the line “creationDate”?
    Thanks a lot!

    Dvir
    6 months ago

    I also have notes in Hebrew and it changes the Hebrew characters to ?, is there a way handle it?

    6 months ago

    Dvir: You can try just appending the line to the notes[i].body() with a + and see how that works. As for getting Hebrew to work, I’m at a loss. I’m not sure how to do that offhand.

    Dean Alan Rowell
    5 months ago

    Garry, I’m sorry but I got this to work once changing for error “Error -10000: AppleEvent handler failed.” I tried this again and get “Error -43: File wasn’t found.” Yesterday I upgraded the MacBookpro Catalina to next upgrade 10.15.5 (19F96). Any thoughts. I make the selection (410+Note members) and it appears to be processing and cant seem to find a folder to fill tho I provided one and thought I selected it. Dean

    5 months ago

    Dean: Sounds like a character in the name of a Note isn’t supported. I mention this and there are some comments above that mention it too.

    Destiny
    5 months ago

    Is it possible to save a note attachment in a specific folder using AppleScript?

    5 months ago

    Destiny: Not sure off the top of my head. But it is easy to do with just drag and drop.

    Curious
    4 months ago

    Hello! I tried running the script and got this error: “Error: Error: Can’t convert types”. What should I try?

    Curious
    4 months ago

    Forgot to include… this is error -1700.

    4 months ago

    Curious: Look at the previous comments to see some of the problems and fixes for errors.

    Bruno
    4 months ago

    Works great even with Catalina 10.15.5 (although the AppleScript bug with Apple Notes). The only thing is to remove the character / from the titles of all Notes before starting this javascript.
    Thanks Gary !
    Is there any script like this for exporting to Evernote app ?

    4 months ago

    Bruno: TO Evernote? I’m not sure. Not an Evernote user, sorry.

    Ken R.
    4 months ago

    Hi Gary … very nice tutorial, thanks for putting it together!
    Question: is it possible to output PDF’s instead of html or text? If so, what changes to the script need to made. Thank you.

    4 months ago

    Ken: If you want to export a PDF, you can always go to the Note and choose File, Export as PDF.

    Ken R.
    4 months ago

    Gary: I know about that method … but that is one note at a time only. I like the bulk export possible with your script, but would prefer PDF format as I’m going use the output in another app, and PDF would be preferable. That said, I can use text format, its just limiting as I can’t imbed graphics (guitar chord diagrams) into it like I can with a PDF. Thanks for the quick response … cheers!

    Ken R.
    4 months ago

    Never mind the export as text file comment … tried it, yes outputs a text file, but it’s filled with html stuff like ” etc. so that won’t work. I looked through the JavaScript library and didn’t find anything about outputting text format … so maybe that is simply not possible?

    4 months ago

    Ken: I don’t think it will be possible. Maybe if you have a lot of notes filled with things like guitar diagrams you may want to look at other solutions for those documents instead of using Notes.

Comments Closed.