Mac OS X Programming

Evernote Backup using Apple Script

Today I’ve created a small Apple Script to export all my Evernote notes into a folder and compress the notes to a tar.gz archive for storing on my NAS. Feel free to use this script and customize for your own environment.

[UPDATE] Fixed an issued causing the export to fail if empty notebooks are present. Thanks to Darryl for the solution in the comments.

(*
#######################################################################
## (C) 2015 Michael Miklis (michaelmiklis.de)
##
##
## Filename:      Backup-Evernote.scpt
##
## Version:       1.0
##
## Release:       Final
##
## Requirements:  -none-
##
## Description:   Export all Evernote note into tar.gz file
##
## This script is provided 'AS-IS'.  The author does not provide
## any guarantee or warranty, stated or implied.  Use at your own
## risk. You are free to reproduce, copy & modify the code, but
## please give the author credit.
##
####################################################################*)


set archivePath to "/Users/michael/Desktop"
set userName to "myuser@domain.com"


with timeout of (30 * 60) seconds
	tell application "Evernote"
		set exportPath to archivePath & "/" & userName
		
		set allNotebooks to every notebook
		
		repeat with currentNoteBook in allNotebooks
			set notebookName to (the name of currentNoteBook)
			
			set allNotes to every note in notebook notebookName
			
			set exportFolder to exportPath & "/" & notebookName
			
			do shell script "if [ ! -e '" & exportFolder & "' ]; then mkdir -p '" & exportFolder & "'; fi;"
			
			set exportFile to exportFolder & ".enex"

			if (length of allNotes > 0) then
				export allNotes to exportFolder format HTML
				export allNotes to exportFile format ENEX
			end if
			
		end repeat
	end tell
end timeout

set currentTimestamp to do shell script "date +%Y-%m-%d_%H-%M-%S"
set archiveFile to archivePath & "/" & currentTimestamp & "_" & userName & ".tar.gz"
log archiveFile

do shell script "tar -cvzf '" & archiveFile & "' '" & exportPath & "'"

tell application "Finder"
	do shell script "rm -rf '" & exportPath & "'"
end tell

tell application "Finder"
	mount volume "afp://mynas.domain.local/Backup/Evernote"
	do shell script "cp '" & archiveFile & "' '/Volumes/Evernote'"
	do shell script "umount '/Volumes/Evernote'"
end tell

display dialog "Backup completed" buttons {"Ok"} default button "Ok"
You may also like
Mac OS X: Modify “Compress” Command in Finder
VBScript: List all installed applications
Edit Synology Photo Station Album Permissions via Script
5 Comments
  • ryan
    2015-12-18T17:49:36+00:000000003631201512 at 17:49
    Reply

    it exports only the same three notebooks every time, i wonder why. I have 24 notebooks.

    • 2016-01-04T14:14:18+00:000000001831201601 at 14:14
      Reply

      Hello Ryan,

      that’s a good question. I don’t have an answer to this – I also have several notebooks and all are exported without problems. Maybe your notebooks contain some special characters that will make problems as each notebook will be created as a dedicated folder. Are the exported three notebooks are the first in an alphabetical sequence?

      with kind regards

      Michael

    • Darryl
      2016-01-09T03:21:50+00:000000005031201601 at 03:21
      Reply

      Ryan,

      You may have empty Notebooks. The script fails if it hits an empty Notebook. If that is the case, adding the following check around the export lines should do the trick. The script will create empty backup folders for empty Notebooks, which I like, as I know it didn’t miss that Notebooks for some reason.

      if (length of allNotes > 0) then
      export allNotes to exportFolder format HTML
      export allNotes to exportFile format ENEX
      end if

      Michael: Thanks for the script.

    • 2017-03-23T11:54:47+00:000000004731201703 at 11:54
      Reply

      That’s cleared my thoughts. Thanks for cotnuibrting.

  • Bernd Biermann
    2015-11-19T13:53:46+00:000000004630201511 at 13:53
    Reply

    Thank you for the script! I’ve been searching for Evernote backup solution on Mac for a long time (there are bunch of them on Windows, like Backupery, but no robust solution for Mac). The script works as expected on my El Capitan.

Leave Your Comment

Your Comment*

Your Name*
Your Webpage