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.
You can also watch this video at YouTube.
Watch more videos about related subjects: Automator (50 videos), Notes (34 videos).

Video Transcript

Hi, this is Gary with MacMost.com. Today let me show you how to can export your notes using a script.
MacMost is supported by more than 600 viewers just like you. Go to MacMost.com/patreon. There you can read more about the Patreon Campaign. Join us and get exclusive content and course discounts. 
Recently I did a video on how to export your Reminders using a script. This is important because there's really no way to export or even copy and paste reminders out of the Reminders app in Catalina. Now Notes is a little bit better. You can certainly export a single note. As a matter of fact you can do it as a pdf and get a really good copy of it. But you could only do it one at a time. So if you have a lot of notes it's very difficult if you want to export all your notes just to back them up. So here's a script that will easily export all of your notes as html files.
Here's the script in Script Editor. Script Editor is on your Mac and it just lets you write scripts. Kind of like Automator but all you're doing is actually scripting and it's more powerful because of that. Anything you do in Script Editor though you can actually do in Automator by using Action that's a script action. In this case I'm using Java Script not Apple Script. So I created a new document here and I switched to Java Script so I can use the more modern programming language rather than the old Apple Script. 
Here's what the script looks like. At the beginning I'm defining two variables that are apps. The first one is the current application. So Script Editor since I'm going to run this in Script Editor. Then there's the Notes app which I'm specifically saying I want the application Notes right here. In both I'm including Standard Additions which means I can actually control them. Now the first thing I want to do is choose which notes out of all the notes I've got. So I'm saying variable notes is going to equal the notes app and all notes in it which is a property called dot notes.Then  I'm going to use a choose from list for my main app which is Script Editor app.choose from list then I'm going to get that list here of notes but just the names. I'm going to have a prompt which isn't really that important. But I am going to allow multiple selections. So you can select one or many of the items listed. This is important because mostly we want to use this to export all notes.
Then I'm going to check to see if I actually got a value back. If not it means I click the cancel button so don't continue any further. Now I'm going to prompt again for choose folder. So choose a location of where to save these. I'm going to save that in a save where variable. I'm going to check to make sure I got a value for that. If not it's because I hit cancel so it won't do anything after this. Now I'm going to loop through all the notes. Not the notes I selected but all of the notes in the Notes app. For each one I'm going to compare it to my list of notes that I've selected.
So the prompt that I had asking for all the notes I'm going to compare it using index of. So it's going to look in those notes that I selected and get the index of. Now if the index of is not found it will return negative one. So if it's not one of the notes I selected in the list I'll get a negative one. I'm going to look for something greater than negative one which means it is in the notes that I've selected. This will be true for every single one if I chose to export all notes. But if I only chose to export some notes then this would only be true for those that I selected in the list.
Then I'm going to do a simple save file. So I'm going to create a file name that's going to be the location of where I selected up here. Then it's going to take the name of the note and I'm going append dot html to the end of it since they are going to be html files. Then I'm going to create a new file. Open it up with that path that file name there and set write permission to true. Now when Java Script does this in Script Editor it's going to start writing the file at the beginning but it won't clear it out. So you need to do this. You need to set the end of file to zero. So in other words clear the file out. So for doing it for the second time we want it to clear out the notes and create new files. Not overwrite what's in the note which may make a mess. It will write it out and close the file. That's all that we need to do.
So if we run this it's going to show me a list of all my notes. I can select them individually. I can use the command key to select multiple ones. I can Shift to select a range or I can do Command A to select them all. So Command A selects them all. I click OK and hit Return. It's going to ask for a location. Let's go and save it to the Desktop but I'm going to create a new folder since this is the first time I'm running it. Now I'll hit Create and then I'm in that folder now. I'll select Choose to choose that as the location. It will Export them all out. You saw it running there for a second. I'm going to Hide it and there's my exported Notes folder.
I go into that I could see all of those html files. I could look at each one. Hit the spacebar to QuickLook with it and there's that one. I'll arrow down to the next one and you could see each one there. Now note it's exporting as html. It's going to look a little different than what's in the Notes but it is going to preserve some of the formatting. As a matter of fact you'll even see some attachments but not all. You'll even see things like tables in there. So html is a good format to export to. It's not going to be perfect but it will create a good backup and a way to copy and paste out of that if you need to recover something in an old note.
One thing to note is that titles for each of these notes need to be good files names. So if you have dots or slashes or something in the titles here then you're going to get error messages. So make sure the first line of each note is something nice and clean because that's going to be the file name.
So in Script Editor if you go to Window, Library and then in Library you go to Notes there, double click on that, it will open up the Library for Notes. Switch to Java Script and click Notes Suite. This shows you all the different things that you could do in Notes inside of Java Script. So note here here's that Note Object that we're using in the script. That's where I got body from, right there. But you could see there's also plain test. So if you want to export as plain test and save as txt files instead of dot html that's how you can do it. 
Now if I wanted to I could Export this and save it out as an Application. So I'm going to put that on the Desktop. Save it out. Then I'm going to Hide Script Editor there. I'm going to try running this one here and it asks for permissions because this is the first time this application has been run. So I'll hit OK. I can still select everything. Then it's going to ask for a new location. Let me create a new folder and I'll save it out there. You could see it worked just as well as a stand alone application. It's just easier to have it sometimes as a script in Script Editor so that you can easily modify it and play around with it as your needs evolve.
Since these are html files when you double click on them they open up in Safari which is fine and easy enough to copy and paste or view them. This is a great way to ensure that your data is saved and go back to older notes you may have deleted. But it's not great for restoring notes if you've lost them all. Still it's good to know that your data is saved and you can access it in case of a problem. It's also a handy way to export a bunch of notes to clean out your Notes app with notes you don't need anymore but you want to make sure you have an archive of them before you delete them.

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: 49 Comments

    Eric
    5 years ago

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

    5 years 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
    5 years 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?

    5 years 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
    5 years ago

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

    5 years 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
    5 years 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!

    5 years ago

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

    Caroline
    5 years 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
    5 years 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
    5 years 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
    5 years 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."

    5 years ago

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

    5 years 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
    5 years 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
    5 years 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

    5 years 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
    5 years ago

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

    Robert E.
    5 years 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?

    5 years 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
    5 years 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
    5 years ago

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

    5 years 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 years 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 years 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 years ago

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

    5 years ago

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

    Curious
    5 years ago

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

    Curious
    5 years ago

    Forgot to include... this is error -1700.

    5 years ago

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

    Bruno
    5 years 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 ?

    5 years ago

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

    Ken R.
    5 years 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.

    5 years ago

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

    Ken R.
    5 years 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.
    5 years 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?

    5 years 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.

    Ramon Perez
    5 years ago

    I get an error and cannot go further:

    A “/” can’t go here.

    5 years ago

    Ramon: Try again. Maybe copy and paste my code. Experiment. Debug. Work with it.

    Ken
    5 years ago

    hi, this is very useful. but is there a way to export notes that are in certain note folder in notes.app?

    5 years ago

    Ken: If you look in Script Editor at the Library for Notes, you'll see that each note has a "container" property for the folder. I suppose you could use that and skip everything except the folder you want. Play around with that.

    Christ
    4 years ago

    fix “Error -43: File wasn’t found.” due to "/" in note name with that:

    var notename = notes[i].name().replace(/\//gi,'-')
    this.console.log("next:"+notename) // view / show log / message tab

    Mike Hodges
    4 years ago

    I get an Error: AppleEvent handler failed.

    Mike Hodges
    4 years ago

    Which characters in a Folder name or Note name are not allowed ?

    4 years ago

    Mike: Read through all the comments and you'll see some solutions.

    Mike Hodges
    4 years ago

    Gary: thank you for your script and your response. I have no "/"'s in any of the file names, but I do have commas, dots, and question marks in the file names. Is that the issue?

    4 years ago

    Mike: What I would do to find out is experiment and see. So do that -- experiment and see.

    Mike Hodges
    4 years ago

    I have over 1,000 Notes. I am going to take another tack and copy my Container files to a backup. Thanks for you attention and your site.

    Masoud
    4 years ago

    Improvement to the filename (if the first line has invalid characters or if it's too long):
    var efilename = notes[i].name().replace(/[^a-z0-9]/gi, '_').substring(0, 30);
    var filename = saveWhere+"/"+efilename+".html";

Comments are closed for this post.